password policy with regular expression

Password policy to match minimum 8 characters password with alphabets in combination with numbers or special characters

package regexp;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

 * Helper for password policy
 * @author ntallapa
public class PwdPolicyVerifier {

	private Pattern pattern;
	private Matcher matcher;

	 * password policy to match 8 characters with alphabets in combination with numbers or special characters
	 * (?=.*[a-zA-Z]) means one or more alphabets, it can be small or CAPS
	 * (?=.*[0-9@#$%]) means one or more numerals or special characters
	 * {8,} means password length should be minimum 8
	private String pwd_policy = "((?=.*[a-zA-Z])(?=.*[0-9@#$%]).{8,})";

	public PwdPolicyVerifier() {
		pattern = Pattern.compile(pwd_policy);

	 * Verify the validity of the given client password
	 * @param password
	 * @return boolean if test string passed the password policy or not
	public boolean verify(String password) {
		matcher = pattern.matcher(password);
		if (matcher.matches()) {
			System.out.println(password + " matched the regexp");
		} else {
			System.out.println(password + " didnt match the regexp");
		return matcher.matches();


	// some test cases
	public static void main(String[] args) {
		PwdPolicyVerifier pv = new PwdPolicyVerifier();


welcome1 matched the regexp
welcomeA didnt match the regexp
Welcome@ matched the regexp
12341234 didnt match the regexp
####$$$$ didnt match the regexp
Welcome@12 matched the regexp
WELCOME12 matched the regexp

how to print leaf nodes in a binary search tree

The logic here is simple. Recursively go through left and then right elements, when the base condition is met (i.e, when r==null), add a check to see if the node’s left and right wings are null and if they are null print the value of the node.

Input binary search tree:

    // print leaf nodes
    public void leafNodes(BinaryNode r) {
    	if(r!= null) {

    		if(r.left == null && r.right == null) {

Note: This logic is same is recursive post order traversal, but instead of just printing the node value after left&right traversals we check to see if left&right children are null and then print the node value.

find intersection of elements in two arrays

Assume two arrays, ‘A’ of size ‘m’ and ‘B’ of size ‘n’

case 1: Two arrays are unsorted

Here we pick one of the array and load it into hash implemented data structure, i.e, HashSet and then proceeds further to find intersection of elements.

Since hashed data structure’s complexity is O(1), the total complexity to find intersection of elements the complexity would become

Algorithm Time Complexity: O(m) + O(n)*O(1)
Algorithm Space Complexity: O(m)

	public void intersect1(int[] a, int[] b) {
		HashSet<Integer> hs = new HashSet<Integer>(); 
		for (int i = 0; i < b.length; i++) {
		for (int i = 0; i < a.length; i++) {
			if(hs.contains(a[i])) {
				System.out.println(a[i]+" is present in both arrays");

best algorithm when compared to all others provided one implements appropriate hashcode method
when the size of the data structure grows too high, it might lead to hash collisions

Case 2: Two arrays are sorted

For every element in array A, do a binary search in array B (instead of linear search as shown in case-1), so here for every value in ‘A’ we go through log(n) interations in ‘B’ to find out if element in ‘A’ exists in ‘B’

Algorithm Time Complexity: O(m)*O(logn)

		public void intersect(int[] a, int[] b) {
			for(int i=0; i<a.length; i++) {
 				int val = binarySearch(b, a[i], 0, a.length);
 				if(val == b[j]) {
 					System.out.println(a[i]+" is present in both arrays");

case 3: Two arrays are unsorted

Brute force algorithm: For every element in array A, traverse through all the elements of array B and find out if the element exists or not.
Algorithm Time Complexity: O(m)*O(n)

In Java

		public void intersect(int[] a, int[] b) {
			for(int i=0; i<a.length; i++) {
				for(int j=0; j<b.length; j++) {
					if(a[i] == b[j]) {
						System.out.println(a[i]+" is present in both arrays");

works well for smaller arrays
works for unsorted arrays
order/complexity is directly proportional to the product of size of arrays, this can take huge time for arrays of larger lengths

Joins and Implementation of relationships in sql

We have three types of relationships in SQL.

  • one-to-one
  • one-to-many
  • many-to-many
  • To implement these relationships

    One-to-one: Use foreign key to the referenced table
    Student: sid, fname, lname, addressId
    Address: addressId, adress, city, sid

    One-to-many: Use foreign key on the “many” side of the relationship linking back to the “one” side.
    Teacher: tid, fname, lname
    Classes: cid, cname, tid    
    Note: Here table ‘Teacher’ is one side and table ‘Classes’ is on many side

    Many-to-many: Use junction table.
    The junction table stores the primary keys of each table that is involved in the relationship.
    Student: sid, fname, lname
    classes: cid, cname, tid
    student_classes: cid, sid


    Here is a very good chart of all joins (and its variations) in SQL


    Mostly technology with occasional sprinkling of other random thoughts


    Amir Amintabar's personal page

    101 Books

    Reading my way through Time Magazine's 100 Greatest Novels since 1923 (plus Ulysses)

    Seek, Plunnge and more...

    My words, my world...

    ARRM Foundation

    Do not wait for leaders; do it alone, person to person - Mother Teresa

    Executive Management

    An unexamined life is not worth living – Socrates


    A topnotch site


    Just another site

    coding algorithms

    "An approximate answer to the right problem is worth a good deal more than an exact answer to an approximate problem." -- John Tukey

    %d bloggers like this: