<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4640943833280351629</id><updated>2012-02-16T07:11:26.040-08:00</updated><category term='Design Pattern'/><category term='Actionscript'/><category term='Flex'/><category term='Technical'/><category term='Interview '/><category term='Interview'/><category term='AIR'/><title type='text'>Flex Ramblings</title><subtitle type='html'>Flex, AIR, Actionscript, PHP, Mysql...An illustration of technologies and whole lot of other things...</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default?start-index=101&amp;max-results=100'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>144</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-3071880330693720922</id><published>2011-11-09T04:02:00.000-08:00</published><updated>2011-11-09T04:03:59.968-08:00</updated><title type='text'>Producer Consumer problem in Java</title><content type='html'>&lt;script type="syntaxhighlighter" class="brush: html"&gt;&lt;![CDATA[import java.util.*;import java.io.*;public class ProdCons {  protected LinkedList list = new LinkedList();  protected int MAX = 10;  protected boolean done = false; // Also protected by lock on list.  /** Inner class representing the Producer side */  class Producer extends Thread {    public void run() {      while (true) {        synchronized(list) {            while (list.size() == MAX) // queue "full"            try {              System.out.println("Producer WAITING");              list.wait();   // Limit the size            } catch (InterruptedException ex) {              System.out.println("Producer INTERRUPTED");            }          list.addFirst(justProduced);          list.notifyAll();  // must own the lock          System.out.println("Produced 1; List size now " + list.size());        }      }    }  }  class Consumer extends Thread {    public void run() {      while (true) {        Object obj = null;        synchronized(list) {          while (list.size() == 0) {            try {              System.out.println("CONSUMER WAITING");              list.wait();  // must own the lock            } catch (InterruptedException ex) {              System.out.println("CONSUMER INTERRUPTED");            }          }          obj = list.removeLast();          list.notifyAll();          int len = list.size();          System.out.println("List size now " + len);        }      }    }  }  ProdCons(int nP, int nC) {    for (int i=0; i&lt;nP; i++)      new Producer().start();    for (int i=0; i&lt;nC; i++)      new Consumer().start();  }  public static void main(String[] args)  throws IOException, InterruptedException {    // Start producers and consumers    int numProducers = 4;    int numConsumers = 3;    ProdCons pc = new ProdCons(numProducers, numConsumers);    // Let it run for, say, 10 seconds    Thread.sleep(10*1000);     // End of simulation - shut down gracefully    synchronized(pc.list) {      pc.done = true;      pc.list.notifyAll();    }  }}]]&gt;&lt;/script&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-3071880330693720922?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/3071880330693720922/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=3071880330693720922' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3071880330693720922'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3071880330693720922'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/11/producer-consumer-problem-in-java.html' title='Producer Consumer problem in Java'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-6088059899374337638</id><published>2011-11-06T11:07:00.001-08:00</published><updated>2011-11-06T11:07:47.014-08:00</updated><title type='text'>Next largest palindrome</title><content type='html'>&lt;p align="justify"&gt;Q: A number is given as a palindrome, write a function to return the next largest palindrome.&lt;/p&gt;  &lt;p align="justify"&gt;Algorithm:&lt;/p&gt;  &lt;p align="justify"&gt;Case I: The given number is a palindrome..ex: 1234321 then simply increment the centre digit by 1 i.e the answer is 1235321&lt;/p&gt;  &lt;p align="justify"&gt;Case II: The given number is not a palindrome:&lt;/p&gt;  &lt;p align="justify"&gt;Subcase 1: The number to the left of the middle digit is greater than the number on the right ex:2194135 (here 219 &amp;gt; 135) then,reverse the left side numbers and replace the right side numbers with them, therefore the answer will be 2194912.&lt;/p&gt;  &lt;p align="justify"&gt;Subcase 2:The number to the right of the middle digit is greater than the number on the left ex:1354219 then,   &lt;br /&gt;then increment the middle digit by 1 and then reverse the left side numbers and replace the right side numbers with them. Therefore the answer would be 135 5(middle digit incremented) 531(reversed), resulting in 1355531, which is the next palindrome.    &lt;br /&gt;Note: in case the middle digit is 9 then incrementing it would make it 10, so put 0 instead of 9 and increment the first right and left neighbouring digit of 9 by 1.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-6088059899374337638?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/6088059899374337638/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=6088059899374337638' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/6088059899374337638'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/6088059899374337638'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/11/next-largest-palindrome.html' title='Next largest palindrome'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-6432593718858958968</id><published>2011-11-04T11:39:00.000-07:00</published><updated>2011-11-04T11:42:06.362-07:00</updated><title type='text'>Subsets that match a given sum in an array</title><content type='html'>Q: Find subsets that match a given sum in an array&lt;script type="syntaxhighlighter" class="brush: html"&gt;&lt;![CDATA[public class Question {	public static ArrayList&lt;ArrayList&lt;Integer&gt;&gt; getSubsets(ArrayList&lt;Integer&gt; set, int index) {		ArrayList&lt;ArrayList&lt;Integer&gt;&gt; allsubsets;		if (set.size() == index) { // Base case - add empty set			allsubsets = new ArrayList&lt;ArrayList&lt;Integer&gt;&gt;();			allsubsets.add(new ArrayList&lt;Integer&gt;()); 		} else {			allsubsets = getSubsets(set, index + 1);			int item = set.get(index);			ArrayList&lt;ArrayList&lt;Integer&gt;&gt; moresubsets = new ArrayList&lt;ArrayList&lt;Integer&gt;&gt;();			for (ArrayList&lt;Integer&gt; subset : allsubsets) {				ArrayList&lt;Integer&gt; newsubset = new ArrayList&lt;Integer&gt;();				newsubset.addAll(subset); //				newsubset.add(item);				moresubsets.add(newsubset);			}			allsubsets.addAll(moresubsets);		}		return allsubsets;	}			public static ArrayList&lt;ArrayList&lt;Integer&gt;&gt; findSumSubsets(ArrayList&lt;ArrayList&lt;Integer&gt;&gt; sets, int sum)	{		ArrayList&lt;ArrayList&lt;Integer&gt;&gt; sumSubsets = new ArrayList&lt;ArrayList&lt;Integer&gt;&gt;();		for (ArrayList&lt;Integer&gt; list : sets) {			int s =0;			for (Integer i : list) {				s += i;			}			if(s == sum)				sumSubsets.add(list);		}		return sumSubsets;	}		public static void main(String[] args) {		ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();		for (int i = 0; i &lt; 3; i++) {			list.add(i);		}		ArrayList&lt;ArrayList&lt;Integer&gt;&gt; subsets = getSubsets(list, 0);		ArrayList&lt;ArrayList&lt;Integer&gt;&gt; sumsubsets = findSumSubsets(subsets,3);		System.out.println(sumsubsets.toString());					}}]]&gt;&lt;/script&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-6432593718858958968?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/6432593718858958968/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=6432593718858958968' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/6432593718858958968'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/6432593718858958968'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/11/subsets-that-match-given-sum-in-array.html' title='Subsets that match a given sum in an array'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-7633279316424668321</id><published>2011-11-04T09:48:00.001-07:00</published><updated>2011-11-04T10:43:51.479-07:00</updated><title type='text'>Common Element in a binary search tree</title><content type='html'>&lt;p align="center"&gt;Q: Given two BST's A, B, Find All elements of A which are present in B&lt;/p&gt;  &lt;p align="center"&gt;OR&lt;/p&gt;  &lt;p align="center"&gt;You are given the root nodes of two BST and you have to print the common elements in them.&lt;/p&gt;  &lt;script type="syntaxhighlighter" class="brush: html"&gt;&lt;![CDATA[public class CommonNode {	class Node	{		int data;		Node left;		Node right;	};	void search(Node node1, Node root2){		if(root2 == null)			return;		if(node1.data == root2.data){			System.out.println("Common node:"+node1.data);			return; //common elem found		}		else{			if(node1.data &lt; root2.data){				root2 = root2.left;				search(node1, root2);			}			else{				root2 = root2.right;				search(node1, root2);			}		}	}	void common(Node root1, Node root2){		if(root1 != null){			common(root1.left, root2);			search(root1, root2);			common(root1.right, root2);		}	}}]]&gt;&lt;/script&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-7633279316424668321?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/7633279316424668321/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=7633279316424668321' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/7633279316424668321'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/7633279316424668321'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/11/common-element-in-binary-search-tree.html' title='Common Element in a binary search tree'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-9044837179920969987</id><published>2011-11-04T09:30:00.001-07:00</published><updated>2011-11-04T09:31:27.645-07:00</updated><title type='text'>Max loss of the stock</title><content type='html'>&lt;p&gt;Given an array of stock prices from day 0 to N-1 of a company X, we have to find the max loss that is possible. Loss occurs if stock is bought at higher price and sold at lower price.   &lt;br /&gt;Ex: 1 2 3 7 5 8 9 4 6 10 12     &lt;br /&gt;Max Loss is 9 - 4 = 5 (Possible losses are 8-4 = 4, 7-5 = 2 etc). Max difference between stock price is 12 - 1 = 11 but max loss is 9 -4 = 5&lt;/p&gt;  &lt;b&gt;Code:&lt;/b&gt;&lt;script type="syntaxhighlighter" class="brush: html"&gt;&lt;![CDATA[public class StockLoss { public static int maxLoss(int a[]) {     int max=a[0],maxloss=0;     for(int i=1;i&lt;a.length;i++)     {             if(maxloss &lt; max-a[i])             maxloss=max-a[i];             if(max &lt; a[i])             max=a[i];     }     return maxloss; }  public static void main(String[] args) {  int a[] = {1, 2, 3, 7, 5, 8, 9, 4, 6, 10, 12 };  System.out.println("Max Loss :"+StockLoss.maxLoss(a)); }}]]&gt;&lt;/script&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-9044837179920969987?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/9044837179920969987/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=9044837179920969987' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/9044837179920969987'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/9044837179920969987'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/11/max-loss-of-stock.html' title='Max loss of the stock'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-9110269481228352265</id><published>2011-11-04T08:59:00.001-07:00</published><updated>2011-11-04T09:02:16.354-07:00</updated><title type='text'>Pythogorous triplets</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div align="center"&gt;Q: &lt;span style="font-size: x-small;"&gt;How to find pythagorean triplets in an array?&lt;/span&gt;&lt;/div&gt;&lt;div align="center"&gt;OR&lt;/div&gt;&lt;div align="center"&gt;Q: Given an array, find all such triplets &lt;code&gt;i&lt;/code&gt;,&lt;code&gt;j&lt;/code&gt; and &lt;code&gt;k&lt;/code&gt;, such that a[i]&lt;sup&gt;2&lt;/sup&gt; = a[j]&lt;sup&gt;2&lt;/sup&gt;+a[k]&lt;sup&gt;2&lt;/sup&gt;&lt;/div&gt;&lt;div align="left"&gt;&lt;i&gt;Pythagorean triplet&lt;/i&gt; is a set {a,b,c} such that a&lt;sup&gt;2&lt;/sup&gt; = b&lt;sup&gt;2&lt;/sup&gt; + c&lt;sup&gt;2&lt;/sup&gt;. Example: for array &lt;code&gt;[9, 2, 3, 4, 8, 5, 6, 10]&lt;/code&gt; the output of the algorithm should be &lt;code&gt;{3, 4, 5}&lt;/code&gt; and &lt;code&gt;{6, 8, 10}&lt;/code&gt;&lt;/div&gt;&lt;div align="left"&gt;&lt;code&gt;&lt;span style="font-family: Arial;"&gt;&lt;b&gt;Algorithm:&lt;/b&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;div align="left"&gt;&lt;b&gt;Square each element&lt;/b&gt;. (This takes O(n) time). This will reduce the original task to "find three numbers in array, one of which is the sum of other two". &lt;/div&gt;&lt;ol&gt;&lt;li&gt;Sort the array in ascending order. This takes O(n log n). &lt;/li&gt;&lt;li&gt;     Now consider each element a[i]. If a[i]=a[j]+a[k], then, since numbers are positive and array is now sorted, k&amp;lt;i and j&amp;lt;i. To find such indexes, run a loop that increases &lt;code&gt;j&lt;/code&gt; from &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;i&lt;/code&gt;, and decreases &lt;code&gt;k&lt;/code&gt; from &lt;code&gt;i&lt;/code&gt; to &lt;code&gt;0&lt;/code&gt; at the same time, until they meet. Increase &lt;code&gt;j&lt;/code&gt; if &lt;code&gt;a[j]+a[k] &amp;lt; a[i]&lt;/code&gt;, and decrease &lt;code&gt;k&lt;/code&gt; if the sum is greater than &lt;code&gt;a[i]&lt;/code&gt;. If the sum is equal, that's one of the answers, print it, and shift both indexes. This takes O(i) operations.&lt;br /&gt;   &lt;/li&gt;&lt;li&gt;Repeat step 2 for each index &lt;code&gt;i&lt;/code&gt;. This way you'll need totally O(n&lt;sup&gt;2&lt;/sup&gt;) operations, which will be the final estimate. &lt;/li&gt;&lt;/ol&gt;Code:&lt;/div&gt;&lt;script class="brush: html" type="syntaxhighlighter"&gt;&lt;![CDATA[import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.List;import java.util.Map;public class Triplets {  public static List&lt;int[]&gt; pythagoreanTripplets(int[] input) {     List&lt;int[]&gt; answers = new ArrayList&lt;int[]&gt;();     Map&lt;Long, Integer&gt; map = new HashMap&lt;Long, Integer&gt;();     for (int i = 0; i &lt; input.length; i++) {         map.put((long)input[i] * (long)input[i], input[i]);     }     Long[] unique = (Long[]) map.keySet().toArray(new Long[0]);     Arrays.sort(unique);     for(int i =  1 ; i &lt; unique.length;i++)     {         Long halfC = unique[i]/2;         for(int j = i-1 ; j&gt;= 0 ; j--)         {             if(unique[j] &lt; halfC) break;             if(map.containsKey(unique[i] - unique[j]))             {                 answers.add(new int[]{map.get(unique[i] - unique[j]),map.get(unique[j]),map.get(unique[i])});             }         }     }     return answers; }  public static void main(String[] args) {    int a[] = {9, 2, 3, 4, 8, 5, 6, 10};  List&lt;int[]&gt; triplet = Triplets.pythagoreanTripplets(a);  for (int[] is : triplet) {   System.out.println("Triplet:");   for(int i=0;i&lt;is.length;i++)   {    System.out.print(is[i]+",");   }  } }}]]&gt;&lt;/script&gt;&lt;/div&gt;Time Complexity : Efficiency is(i) O(nlogn) : for sorting(ii) O(n^2) : for finding pairs adding equal to A[i]So total efficeincy is O(n^2)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-9110269481228352265?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/9110269481228352265/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=9110269481228352265' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/9110269481228352265'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/9110269481228352265'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/11/pythogorous-triplets.html' title='Pythogorous triplets'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-9154924165749045557</id><published>2011-10-16T06:29:00.001-07:00</published><updated>2011-10-16T08:57:36.647-07:00</updated><title type='text'>Merging sorted 1st half and 2nd half</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;Q: Given an integer array of which both first half and second half are sorted. Write a function to merge the two parts to create one single sorted array in place [do not use any extra space]. e.g. If input array is [1,3,6,8,-5,-2,3,8] It should be converted to: [-5,-2,1,3,3,6,8,8]&lt;br /&gt;&lt;br /&gt;The idea is to Insert all elements of second half into first half., like we do in Insertion Sort.&lt;br /&gt;&lt;br /&gt;we need to keep the arrays sorted while we doing the swaps&lt;br /&gt;&lt;script class="brush: java" type="syntaxhighlighter"&gt;&lt;![CDATA[public class Sorted { public static void sort() {  int[] a = new int[] {1,3,6,8,-5,-2,3,8};  print(a);  int rs = a.length / 2; //start position of 2nd array  int li = 0, ri = rs;   for (;;) {   if (a[li] &lt;= a[ri]) {    if (ri == rs) {     li++;    } else {     swap(a, li, ri - 1);     ri = rs;    }   } else {    ri++;   }   if (ri &gt;= a.length || ri &lt;= li) {    break;   }  }    print(a); }  public static void print(int a[]) {  for(int x : a)  {   System.out.print(x +",");  }    System.out.println(""); }  public static void swap(int a[],int x, int y) {  int temp = a[x];  a[x] = a[y];  a[y] = temp; }   public static void main(String[] args) {    Sorted.sort(); }}]]&gt;&lt;/script&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-9154924165749045557?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/9154924165749045557/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=9154924165749045557' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/9154924165749045557'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/9154924165749045557'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/10/merging-sorted-1st-half-and-2nd-half.html' title='Merging sorted 1st half and 2nd half'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-4316091616421334718</id><published>2011-09-27T12:08:00.001-07:00</published><updated>2011-09-27T12:08:53.643-07:00</updated><title type='text'>Design a web crawler</title><content type='html'>&lt;p&gt;Web crawler is a program that traverses the hyperlinks structure of web pages. We crawler visits each web page by following the hyperlinks on previous page it visits. while it is crawling web pages to extract hyperlinks, a web crawler also save each page it has visited.&lt;/p&gt;  &lt;p&gt;Crawler basic algorithm&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Remove a URL from the unvisited URL list&lt;/li&gt;    &lt;li&gt;Determine the IP Address of its host name&lt;/li&gt;    &lt;li&gt;Download the corresponding document&lt;/li&gt;    &lt;li&gt;Extract any links contained in it.&lt;/li&gt;    &lt;li&gt;If the URL is new, add it to the list of unvisited URLs&lt;/li&gt;    &lt;li&gt;Process the downloaded document&lt;/li&gt;    &lt;li&gt;Back to step 1&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;Single Crawler&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-rkb0DYH6UYE/ToIfLIk5hnI/AAAAAAAAFBE/9JO5rNzr59k/s1600-h/Picture2%25255B3%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Picture2" border="0" alt="Picture2" src="http://lh3.ggpht.com/-4nGcP1T1UL0/ToIfLy-Za8I/AAAAAAAAFBI/u1fwjFRm-XE/Picture2_thumb%25255B1%25255D.png?imgmax=800" width="480" height="343" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Multithreaded Crawler&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-DujotExdBuE/ToIfMoRmjkI/AAAAAAAAFBM/BCROyJ6CLeM/s1600-h/Picture3%25255B4%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Picture3" border="0" alt="Picture3" src="http://lh6.ggpht.com/-9h1dy4V-PPQ/ToIfNL0_E3I/AAAAAAAAFBQ/_g31JI2e2ls/Picture3_thumb%25255B2%25255D.png?imgmax=800" width="512" height="304" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Parallel Crawler&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-hPdK9BHRe74/ToIfNxz6EMI/AAAAAAAAFBU/wb_A-79DbR0/s1600-h/Picture4%25255B3%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Picture4" border="0" alt="Picture4" src="http://lh3.ggpht.com/-N13rnUP6SX8/ToIfOSu_eYI/AAAAAAAAFBY/jqLLZV358oY/Picture4_thumb%25255B1%25255D.png?imgmax=800" width="519" height="345" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Search Engine : architecture&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-Owphsk5YBq4/ToIfPAgpH6I/AAAAAAAAFBc/zchnW1YS61s/s1600-h/Picture5%25255B4%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Picture5" border="0" alt="Picture5" src="http://lh4.ggpht.com/-QqtaHotYeZg/ToIfPj3TdRI/AAAAAAAAFBg/cIz6-vlmzb4/Picture5_thumb%25255B2%25255D.png?imgmax=800" width="556" height="324" /&gt;&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;Search Engine : major components&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Crawlers       &lt;br /&gt;&lt;/strong&gt;Collects documents by recursively fetching links from a set of starting pages.Each crawler has different policies The pages indexed by various search engines are different &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;The Indexer       &lt;br /&gt;&lt;/strong&gt;Processes pages, decide which of them to index, build various data structures representing the pages (inverted index,web graph, etc), different representation among search engines. Might also build additional structure ( LSI ) &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;The Query Processor       &lt;br /&gt;&lt;/strong&gt;Processes user queries and returns matching answers in an order determined by a ranking algorithm&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Issues on crawler&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;General architecture&lt;/li&gt;    &lt;li&gt;What pages should the crawler download ?&lt;/li&gt;    &lt;li&gt;How should the crawler refresh pages ?&lt;/li&gt;    &lt;li&gt;How should the load on the visited web sites be minimized ?&lt;/li&gt;    &lt;li&gt;How should the crawling process be parallelized ?&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;Distributed crawler&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The crawler system should consist of a number of crawler entities, which run on distributed sites and interact in peer-to-peer fashion. Each crawler entity has to have the knowledge to its URL subset, as well as mapping from URL subset to network address of corresponding peer crawler entity.&lt;/p&gt;  &lt;p&gt;Whenever the crawler entity encounters a URL from a different URL subset, it should forward to the appropriate peer crawler entity based on URL subset to crawler entity lookup.&lt;/p&gt;  &lt;p&gt;Each crawler entity should maintain its own database, which only stores the URL’s from the URL subset assigned to the particular entity. The databases are disjoint and can be combined offline when the crawling task is complete.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;    &lt;p&gt;Crawler Entity Architecture&lt;/p&gt;     &lt;/p&gt;  &lt;p&gt;Each crawler entity consists of a number of crawler threads, a URL handling thread, a URL packet dispatcher thread and URL packet receiver thread. The URL set assigned to each crawler entity will be further divided into subsets for each crawler thread. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-BiyK6Cp3jtQ/ToIfQembD9I/AAAAAAAAFBk/jHyCN2Ivz6Q/s1600-h/Image1%25255B4%25255D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Image1" border="0" alt="Image1" src="http://lh3.ggpht.com/-PyBy9tCiSk0/ToIfRGfMYXI/AAAAAAAAFBo/L-kRkSKEQWM/Image1_thumb%25255B2%25255D.gif?imgmax=800" width="532" height="427" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Each crawler thread should have its own pending URL list, a DNS cache. Each thread picks up an element from URL pending list, check to see if the IP corresponding to URL is available in the DNS cache, else does a DNS lookup, generates a HTTP fetch requests, gets the page and finally extracts more URL’s from the fetched page and puts them in the job pending queue of the URL handling thread.&lt;/p&gt;  &lt;p&gt;URL handling thread will have a job queue. It will get a URL from the job queue, checks to see if the URL belongs to the URL set corresponding to the crawler entity, if it belongs to another entity it will put the URL on the dispatcher queue and get a new URL from the its job queue. If the URL belongs to its set, it firsts checks the URL-seen cache, if the test fails it queries the SQL database to check if the URL has been seen, and puts the URL in the URL-seen cache. It then puts the URL into URL pending list of one of the crawler threads. URLs are assigned to a crawler thread based on domain names. Each domain name will only be serviced by one thread; hence only one connection will be maintained with any given server. This will make sure that the crawler doesn’t overload a slow server.&lt;/p&gt;  &lt;p&gt;URL dispatcher thread will communicate the URL’s corresponding crawler entity.&lt;/p&gt;  &lt;p&gt;A URL receiver thread collects the URL’s received from other crawler entities i.e. communicated via dispatcher threads of those crawler entities and puts them on the job queue of the URL handling thread.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-4316091616421334718?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/4316091616421334718/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=4316091616421334718' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4316091616421334718'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4316091616421334718'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/09/design-web-crawler.html' title='Design a web crawler'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/-4nGcP1T1UL0/ToIfLy-Za8I/AAAAAAAAFBI/u1fwjFRm-XE/s72-c/Picture2_thumb%25255B1%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-6424795984038666011</id><published>2011-09-27T05:45:00.001-07:00</published><updated>2011-09-27T05:45:21.885-07:00</updated><title type='text'>Buffer Overflow</title><content type='html'>&lt;p&gt;Question: &lt;b&gt;What is a buffer overflow and how do you exploit it?&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;What is Buffer Overrun?&lt;/p&gt;  &lt;p&gt;Buffer Over-run refers to problem where we can make program to use more than allotted ‘buffer’.All buffer overruns cannot be exploited as security vulnerability&lt;/p&gt;  &lt;p&gt;What it could lead to?&lt;/p&gt;  &lt;p&gt;Buffer overflows can be triggered by inputs that are designed to execute code, or alter the way the program operates. You get an access violation (AV). Your site becomes unstable.The attacker injects code into your application, executes it, and makes everyone an administrator of your site.&lt;/p&gt;  &lt;p&gt;Types of Buffer Overrun&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Stack Overruns&lt;/li&gt;    &lt;li&gt;Heap Overruns&lt;/li&gt;    &lt;li&gt;Array Indexing Errors&lt;/li&gt;    &lt;li&gt;Format String&lt;/li&gt;    &lt;li&gt;Unicode and ANSI Buffer size mismatch&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;A colleague of mine created a&amp;#160; PPT on Buffer Overflow which we deal with here on daily basis. I removed some of the proprietary code and some other useful information.&lt;/p&gt;  &lt;div style="width: 425px" id="__ss_9442003"&gt;&lt;strong style="margin: 12px 0px 4px; display: block"&gt;&lt;a title="Buffer OverFlow" href="http://www.slideshare.net/rambabu.iitk/buffer-overflow-9442003"&gt;Buffer OverFlow&lt;/a&gt;&lt;/strong&gt;&lt;object id="__sse9442003" width="425" height="355"&gt;&lt;param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=bufferover-run-110927074332-phpapp02&amp;amp;stripped_title=buffer-overflow-9442003&amp;amp;userName=rambabu.iitk" /&gt;&lt;param name="allowFullScreen" value="true" /&gt;&lt;param name="allowScriptAccess" value="always" /&gt;&lt;embed name="__sse9442003" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=bufferover-run-110927074332-phpapp02&amp;amp;stripped_title=buffer-overflow-9442003&amp;amp;userName=rambabu.iitk" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"&gt;&lt;/embed&gt;&lt;/object&gt;    &lt;div style="padding-bottom: 12px; padding-left: 0px; padding-right: 0px; padding-top: 5px"&gt;View more &lt;a href="http://www.slideshare.net/"&gt;presentations&lt;/a&gt; from &lt;a href="http://www.slideshare.net/rambabu.iitk"&gt;Rambabu Duddukuri&lt;/a&gt;.&lt;/div&gt; &lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-6424795984038666011?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/6424795984038666011/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=6424795984038666011' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/6424795984038666011'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/6424795984038666011'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/09/buffer-overflow.html' title='Buffer Overflow'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-3420635659912253555</id><published>2011-09-26T13:39:00.001-07:00</published><updated>2011-09-26T13:39:59.864-07:00</updated><title type='text'>Max Black sub square matrix</title><content type='html'>&lt;p&gt;Imaging there is a square matrix with n x n cells. Each cell is either filled with a black pixel or a white pixel. Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels. &lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:277bd2f2-1564-4c04-ab84-a8abeae3338e" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 629px; height: 664px;" src="http://lh3.ggpht.com/-pkYxEMt2q2g/ToDUkVwRQJI/AAAAAAAAFAs/DaNgHgRih2E/transformedimage%25255B17%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:69f52322-8e26-443a-ba67-49097a488f76" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 627px; height: 696px;" src="http://lh3.ggpht.com/-YrjqKDoIpK8/ToDUlKdK2RI/AAAAAAAAFAw/7S_1T8_6C2A/transformedimage%25255B37%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:f8ba56c3-fc97-40dd-84ad-dc1e4a8eaa37" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 629px; height: 664px;" src="http://lh6.ggpht.com/-d_l7F_Xw6n8/ToDUmOCFlUI/AAAAAAAAFA0/SFfahEbQ-FM/transformedimage%25255B22%25255D.png?imgmax=800" /&gt;&lt;/div&gt;    &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:add71883-6991-47b3-9af2-85316ddf4837" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 627px; height: 696px;" src="http://lh3.ggpht.com/-GaLyS6NxjUc/ToDUm3AHWCI/AAAAAAAAFA4/V-tsqvgE9no/transformedimage%25255B43%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:895fc0c4-b21c-404c-bbf9-fb6bccb396f3" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 558px; height: 152px;" src="http://lh5.ggpht.com/-Z1FkdeMfwSk/ToDUnoe6xBI/AAAAAAAAFA8/KLNWW0iQxVo/transformedimage%25255B63%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:43812657-65e2-480d-82c2-5aa2f0c20591" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 566px; height: 548px;" src="http://lh3.ggpht.com/-EJHq98uiJoQ/ToDUorNQddI/AAAAAAAAFBA/QoiJ5BosvxM/transformedimage%25255B80%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-3420635659912253555?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/3420635659912253555/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=3420635659912253555' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3420635659912253555'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3420635659912253555'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/09/max-black-sub-square-matrix.html' title='Max Black sub square matrix'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/-pkYxEMt2q2g/ToDUkVwRQJI/AAAAAAAAFAs/DaNgHgRih2E/s72-c/transformedimage%25255B17%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-38115068806844875</id><published>2011-09-26T08:16:00.000-07:00</published><updated>2011-09-26T08:16:55.407-07:00</updated><title type='text'>Algorithm to Exhibit Did You Mean Feature of Google Search Engine</title><content type='html'>&lt;p&gt;&lt;font size="2"&gt;When people search at Google, they often type two words without space. For example, instead of typing &amp;quot;binary tree&amp;quot;, someone can type &amp;quot;binarytree&amp;quot;. Given an input, what is a good way of finding if it is a combination of two valid words e.g. In Directly You have Add Space between two such or if are using some data structure , where words would have been inserted , you would have maintained a boolean variable to defined the end of word (eod)      &lt;br /&gt;An Example Will be Let's say you have a phrase without any spaces - eg. &amp;quot;thisisawesome&amp;quot;. Given a dictionary, how would you add spaces in this string?&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;You can solve this problem using a trie datastructure. Trie data structure implementation can be found here in my previous blog. The rest is just searching the trie&lt;/font&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:38af0095-1834-44f3-a510-bd35d8f3cb55" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 611px; height: 691px;" src="http://lh6.ggpht.com/-iMu24v_lzK0/ToCXZVjEc_I/AAAAAAAAE-Q/tJAwvuktMTk/transformedimage%25255B51%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-38115068806844875?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/38115068806844875/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=38115068806844875' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/38115068806844875'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/38115068806844875'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/08/algorithm-to-exhibit-did-you-mean.html' title='Algorithm to Exhibit Did You Mean Feature of Google Search Engine'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/-iMu24v_lzK0/ToCXZVjEc_I/AAAAAAAAE-Q/tJAwvuktMTk/s72-c/transformedimage%25255B51%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-7628351089989783713</id><published>2011-09-25T11:55:00.001-07:00</published><updated>2011-09-26T03:46:18.312-07:00</updated><title type='text'>LRU Cache Implementation</title><content type='html'>  &lt;p&gt;   &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:bef3719d-824d-4909-838e-a66ab1b51460" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 782px; height: 782px;" src="http://lh6.ggpht.com/-ha7MHz2o9W4/ToBX4xrylJI/AAAAAAAAE9g/iBFeoii6jTs/transformedimage%25255B68%25255D.png?imgmax=800" /&gt;&lt;/div&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-7628351089989783713?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/7628351089989783713/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=7628351089989783713' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/7628351089989783713'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/7628351089989783713'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/09/lru-cache-implementation.html' title='LRU Cache Implementation'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/-ha7MHz2o9W4/ToBX4xrylJI/AAAAAAAAE9g/iBFeoii6jTs/s72-c/transformedimage%25255B68%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-3324652083146782006</id><published>2011-09-25T10:42:00.001-07:00</published><updated>2011-09-26T05:00:30.338-07:00</updated><title type='text'>Linked List is a palindrome or not</title><content type='html'>&lt;p&gt;Question: Write a program to check if the given linked list is a palindrome or not&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;METHOD 1 (using recursion)&lt;/strong&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:5661467c-70ce-428a-bff8-e703c9055d5d" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 596px; height: 1024px;" src="http://lh5.ggpht.com/-FGz98wL6zIE/ToBpQlmWBJI/AAAAAAAAE98/qi7awU-NphM/transformedimage%25255B169%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;br /&gt;  &lt;p&gt;&lt;strong&gt;METHOD 2 (Using Stack)&lt;/strong&gt;&lt;/p&gt;  &lt;br /&gt;  &lt;br /&gt;  &lt;ul&gt;   &lt;br /&gt;    &lt;li&gt;Get the middle of the linked list.      &lt;br /&gt;      &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;Push the elements into the stack till the middle of the linked list      &lt;br /&gt;      &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;Compare the top of the stack and second half.      &lt;br /&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;   &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:ae0a05ca-278e-40b6-aa11-ea40e9fd00fc" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 593px; height: 796px;" src="http://lh5.ggpht.com/-Glv7OQltD88/ToBpRzypULI/AAAAAAAAE-A/G2aAvO1onfk/transformedimage%25255B176%25255D.png?imgmax=800" /&gt;&lt;/div&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-3324652083146782006?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/3324652083146782006/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=3324652083146782006' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3324652083146782006'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3324652083146782006'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/09/linked-list-is-palindrome-or-not.html' title='Linked List is a palindrome or not'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/-FGz98wL6zIE/ToBpQlmWBJI/AAAAAAAAE98/qi7awU-NphM/s72-c/transformedimage%25255B169%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-1263850191859937522</id><published>2011-09-22T09:31:00.001-07:00</published><updated>2011-09-26T05:07:27.415-07:00</updated><title type='text'>Next bigger number</title><content type='html'>&lt;p&gt;Q : You have given a positive number. You have to find a number which is immediate bigger than that by using same digits available in the number. use same digits with same number of time, coming in positive integer and if a small number is not possible then we have to return -1.&lt;/p&gt;  &lt;p&gt;For example: (1) You have given a number 7585 , your output should be 7855 . (2) 7111, return –1&lt;/p&gt;  &lt;p&gt;Algorithm: &lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:b278d0c9-f6ed-4ec0-b7eb-b5478a23b5ee" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 593px; height: 178px;" src="http://lh5.ggpht.com/-tuZqAz_wvl4/ToBq4hhKjTI/AAAAAAAAE-E/TXIHqZlDJ1U/transformedimage%25255B42%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;br /&gt;  &lt;p&gt;&lt;font size="2"&gt;Code:&lt;/font&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:fec8f9ae-1505-4246-8e83-7ced695fc3bf" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 593px; height: 724px;" src="http://lh3.ggpht.com/-8zTLak3OEV4/ToBq6BjuvtI/AAAAAAAAE-I/GxgjA6kRj8U/transformedimage%25255B13%25255D.png?imgmax=800" /&gt;&lt;/div&gt; &lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-1263850191859937522?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/1263850191859937522/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=1263850191859937522' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/1263850191859937522'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/1263850191859937522'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/09/next-bigger-number.html' title='Next bigger number'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/-tuZqAz_wvl4/ToBq4hhKjTI/AAAAAAAAE-E/TXIHqZlDJ1U/s72-c/transformedimage%25255B42%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-7279527152714718858</id><published>2011-08-06T14:40:00.001-07:00</published><updated>2011-08-06T14:40:43.073-07:00</updated><title type='text'>Building Bridges:</title><content type='html'>&lt;p&gt;&lt;font size="2"&gt; Problem: Consider a 2-D map with a horizontal river passing through its center. There are n cities on the southern bank with x-coordinates a1….an and n cities on the northern bank with x coordinates b1….bn. The cities on each bank are also numbered 1 through n and these numbers do not correspond to the ordering of the x-coordinates. You can only build a bridge from a city on the south bank to a city on the north bank with the same number. No two bridges may cross each other. An example of a valid bridge building is shown in Figure 1. Give an algorithm for finding the maximum number of bridges that can be built.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Solution:     &lt;br /&gt;Consider the sequences A = N(a1),……..,N(an) and B = N(b1),…….,N(bn) where N(ai) is the number of the city with x-coordinate ai. The length of the longest common subsequence of A and B is the maximum number of bridges. Since A and B are non-repeating, the length of the LCS for A and B can be calculated in O(n log n) time. Make sure you understand why that is the case!&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/-bnsiCIT94O4/Tj2015vdspI/AAAAAAAAE9A/zszJHv7dwL8/s1600-h/image%25255B3%25255D.png"&gt;&lt;font size="2"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/-RQJGhAe-JXA/Tj202Ug93nI/AAAAAAAAE9E/QZorx5koFQA/image_thumb%25255B1%25255D.png?imgmax=800" width="517" height="246" /&gt;&lt;/font&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Proof that length of the LCS is the maximum number of bridges: We show that the maximum number of bridges cannot be more than the length of the LCS and that the maximum number of bridges cannot be less than the length of the LCS.     &lt;br /&gt;Firstly, assume the length of the LCS is m. Let c1,…….,cm be a longest common subsequence of A and B, corresponding to cities ai&lt;sub&gt;1&lt;/sub&gt;……ai&lt;sub&gt;m&lt;/sub&gt; in A and bj&lt;sub&gt;1&lt;/sub&gt;,,,,,,,, bj&lt;sub&gt;m&lt;/sub&gt; in B. Then for 0 &amp;lt; k&amp;lt; = m, we can draw a bridge from ai&lt;sub&gt;k&lt;/sub&gt; to bi&lt;sub&gt;k&lt;/sub&gt; . None of these bridges intersect. Therefore, we can draw at least as many bridges as the length of the LCS.      &lt;br /&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Now assume we can draw at most m bridges from cities CA = ai&lt;sub&gt;1&lt;/sub&gt;,…..ai&lt;sub&gt;m&lt;/sub&gt; to cities CB = bj&lt;sub&gt;1&lt;/sub&gt;,….., bj&lt;sub&gt;m&lt;/sub&gt; and WLOG assume CA is ordered by increasing x-coordinate. Then N(ai&lt;sub&gt;k&lt;/sub&gt; ) = N(bj&lt;sub&gt;k&lt;/sub&gt; ) since we can draw a bridge between them. Moreover, bj&lt;sub&gt;k&lt;/sub&gt; must have a higher x-coordinate than any of bj&lt;sub&gt;1&lt;/sub&gt;,…..bj&lt;sub&gt;k-1&lt;/sub&gt; and a lower x-coordinate than any of bj&lt;sub&gt;k+1&lt;/sub&gt;,…..,bj&lt;sub&gt;m&lt;/sub&gt; so that none of the bridges cross.Therefore CA is a subsequence of A and CB is a subsequence of B and we have found a common subsequence. Thus, the length of the LCS is at least the maximum number of bridges&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-7279527152714718858?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/7279527152714718858/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=7279527152714718858' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/7279527152714718858'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/7279527152714718858'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/08/building-bridges.html' title='Building Bridges:'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/-RQJGhAe-JXA/Tj202Ug93nI/AAAAAAAAE9E/QZorx5koFQA/s72-c/image_thumb%25255B1%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-6781875995968692548</id><published>2011-08-06T14:27:00.001-07:00</published><updated>2011-09-26T05:12:05.014-07:00</updated><title type='text'>Edit Distance</title><content type='html'>&lt;p&gt;&lt;font size="2"&gt;Problem: Given two text strings A = a1a2….an of length n and B = b1b2…bm of length m, you want to transform A into B with a minimum number of operations of the following types: delete a character from A, insert a character into A, or change some character in A into a new character. The minimal number of such operations required to transform A into B is called the edit distance between A and B. Give an algorithm for finding the edit distance from A to B.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Solution:      &lt;br /&gt;Recursion Relation: We recurse on m(i; j), the minimum number of operations to change A(1 : i) into B(1 : j). The relation is&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-cK8ItMPF4MU/Tj2xyl70CiI/AAAAAAAAE84/K3wQsQZZTVQ/s1600-h/image%25255B3%25255D.png"&gt;&lt;font size="2"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/-K0EuQLHBEKI/Tj2xzWndgXI/AAAAAAAAE88/A8FoaUF3zx8/image_thumb%25255B1%25255D.png?imgmax=800" width="544" height="65" /&gt;&lt;/font&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Running Time: m has nm elements and evaluating each element takes O(1) time for a total running time of O(nm).&lt;/font&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:72e18292-0a13-417d-ac29-525c89629468" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 574px; height: 642px;" src="http://lh4.ggpht.com/-emIT6hZiIu0/ToBr_pjJ10I/AAAAAAAAE-M/L2rOTTGRvj0/transformedimage%25255B31%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-6781875995968692548?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/6781875995968692548/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=6781875995968692548' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/6781875995968692548'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/6781875995968692548'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/08/edit-distance.html' title='Edit Distance'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/-K0EuQLHBEKI/Tj2xzWndgXI/AAAAAAAAE88/A8FoaUF3zx8/s72-c/image_thumb%25255B1%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-9146796278774198563</id><published>2011-08-06T14:22:00.001-07:00</published><updated>2011-09-26T12:04:59.232-07:00</updated><title type='text'>Balanced Partitions:</title><content type='html'>&lt;p&gt;&lt;font size="2"&gt;Suppose you are given an array of n integers {a1,……, an} between 0 and M. Give an algorithm for dividing these integers into two sets x and y such that the difference of the sum of the integers in each set, is minimized. &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;For example, given the set {2,3,2,7,9}, you can divide it into {2, 2, 7} (sums to 11) and {3; 9} (sums to 12) for a difference of 1.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Solution:      &lt;br /&gt;Recursion Relation: Consider just the set of the numbers {a1,…..,aj}. What sums can we make with that set or subsets of it? We can make&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="2"&gt;Any sums we could make with a subset of {a1,….,aj-1}&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;Any sums we could make with a subset of {a1,….,aj-1} + aj&lt;/font&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;font size="2"&gt;So: Let C&lt;sub&gt;ij&lt;/sub&gt; be 1 if a subset of {a1,…..,ai} adds to j and 0 otherwise. The recursion relation for C&lt;sub&gt;ij&lt;/sub&gt; is&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/-hxympFqPlHw/Tj2wh-AYnYI/AAAAAAAAE8o/BgMCHgcZvrI/s1600-h/image%25255B3%25255D.png"&gt;&lt;font size="2"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/-Vu9LvznirJk/Tj2wiUxIONI/AAAAAAAAE8s/aPZP9SkgvzE/image_thumb%25255B1%25255D.png?imgmax=800" width="543" height="94" /&gt;&lt;/font&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;We find the value of j, let it be b, closest to &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-NebDVbWKK1w/Tj2wi-5CaRI/AAAAAAAAE8w/F54PaMxIdcc/s1600-h/image%25255B6%25255D.png"&gt;&lt;font size="2"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/-vMSV4G3N98E/Tj2wjUA_83I/AAAAAAAAE80/tu5ZdI4aT2Y/image_thumb%25255B2%25255D.png?imgmax=800" width="163" height="53" /&gt;&lt;/font&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;such that C&lt;sub&gt;nj&lt;/sub&gt; = 1. The minimum difference is 2(T - b).       &lt;br /&gt;Running Time: We need only let j go to nM since the integers are bounded. Therefore, the size of C is n&lt;sup&gt;2&lt;/sup&gt;M and filling it in takes O(1) per entry for a total running time of O(n&lt;sup&gt;2&lt;/sup&gt;M).&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;A dynamic programming solution to this problem is provided in this video &lt;a href="http://people.csail.mit.edu/bdean/6.046/dp/"&gt;tutorial &lt;/a&gt;by Brian C. Dean. It is problem number 7.&lt;/p&gt;  &lt;p&gt;Algorithm:   &lt;br /&gt;Firstly this algorithm can be viewed as knapsack problem where individual array elements are the weights and half the sum as total weight of the knapsack.&lt;/p&gt;  &lt;p&gt;1.take a solution array as boolean array sol[] of size sum/2+1&lt;/p&gt;  &lt;p&gt;2. For each array element,traverse the array and set sol [j] to be true if sol [j - value of array] is true &lt;/p&gt;  &lt;p&gt;3.Let halfsumcloser be the closest reachable number to half the sum and partition are sum-halfsumcloser and halfsumcloser.&lt;/p&gt;  &lt;p&gt;4.start from halfsum and decrease halfsumcloser once everytime until you find that sol[halfsumcloser] is true&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:09330412-dd37-4847-94cc-1c2956398b16" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 623px; height: 690px;" src="http://lh3.ggpht.com/-WxaLLdRID3k/ToDM2c2lADI/AAAAAAAAFAo/yNroEa6sRHg/transformedimage%25255B49%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-9146796278774198563?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/9146796278774198563/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=9146796278774198563' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/9146796278774198563'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/9146796278774198563'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/08/balanced-partitions.html' title='Balanced Partitions:'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/-Vu9LvznirJk/Tj2wiUxIONI/AAAAAAAAE8s/aPZP9SkgvzE/s72-c/image_thumb%25255B1%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-2243833391990591240</id><published>2011-08-06T14:05:00.001-07:00</published><updated>2011-09-26T05:41:02.504-07:00</updated><title type='text'>Box Stacking</title><content type='html'>&lt;p&gt;&lt;font size="2"&gt;&lt;/font&gt;Problem:&amp;#160; You are given a set of boxes {b1,……,bn}. Each box bj has an associated width wj , height hj and depth dj . Give an algorithm for creating the highest possible stack of boxes with the constraint that if box bj is stacked on box bi, the 2D base of bi must be larger in both dimensions than the base of bj . You can of course, rotate the boxes to decide which face is the base, but you can use each box only once.     &lt;br /&gt;For example, given two boxes with h1 = 5;w1 = 5; d1 = 1 and h2 = 4;w2 = 5; h2 = 2, you should orient box 1 so that it has a base of 5x5 and a height of 1 and stack box 2 on top of it oriented so that it has a height of 5 for a total stack height of 6.&lt;/p&gt;    &lt;p&gt;&lt;font size="2"&gt;Solution:&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Recursion: Memorize over H(j,R), the tallest stack of boxes with j on top with rotation R.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/--xsC6KVtxoI/Tj2soWxIiqI/AAAAAAAAE8g/wh9VbM9g1qc/s1600-h/image%25255B3%25255D.png"&gt;&lt;font size="2"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/-kFC232x16Qk/Tj2so9gs6GI/AAAAAAAAE8k/ztPi6NM3r3Y/image_thumb%25255B1%25255D.png?imgmax=800" width="509" height="71" /&gt;&lt;/font&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Running Time: The size of H is O(n |R&lt;sub&gt;j&lt;/sub&gt;|) where R is the number of possible rotations for a box.For our purposes, |R| = 3 (since we only care about which dimension we designate as the \height&amp;quot;) so |H| = O(n). Filling in each element of H is also O(n) for a total running time of O(n&lt;sup&gt;2&lt;/sup&gt;).&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Code:&lt;/font&gt;&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&lt;div id="codeSnippetWrapper"&gt;&lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; stackHeight(ArrayList&amp;lt;Box&amp;gt; boxes) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (boxes == null) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; 0;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;         }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; h = 0;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (Box b : boxes) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;             h += b.height;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;         }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; h;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;     }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;     &lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; ArrayList&amp;lt;Box&amp;gt; createStackR(Box[] boxes, Box bottom) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; max_height = 0;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum14"&gt;  14:&lt;/span&gt;         ArrayList&amp;lt;Box&amp;gt; max_stack = null;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum15"&gt;  15:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; boxes.length; i++) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum16"&gt;  16:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (boxes[i].canBeAbove(bottom)) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum17"&gt;  17:&lt;/span&gt;                 ArrayList&amp;lt;Box&amp;gt; new_stack = createStackR(boxes, boxes[i]);&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum18"&gt;  18:&lt;/span&gt;                 &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; new_height = stackHeight(new_stack);&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum19"&gt;  19:&lt;/span&gt;                 &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (new_height &amp;gt; max_height) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum20"&gt;  20:&lt;/span&gt;                     max_stack = new_stack;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum21"&gt;  21:&lt;/span&gt;                     max_height = new_height;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum22"&gt;  22:&lt;/span&gt;                 }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum23"&gt;  23:&lt;/span&gt;             }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum24"&gt;  24:&lt;/span&gt;         }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum25"&gt;  25:&lt;/span&gt;         &lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum26"&gt;  26:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (max_stack == null) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum27"&gt;  27:&lt;/span&gt;             max_stack = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ArrayList&amp;lt;Box&amp;gt;();&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum28"&gt;  28:&lt;/span&gt;         }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum29"&gt;  29:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (bottom != null) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum30"&gt;  30:&lt;/span&gt;             max_stack.add(0, bottom);&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum31"&gt;  31:&lt;/span&gt;         }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum32"&gt;  32:&lt;/span&gt;         &lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum33"&gt;  33:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; max_stack;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum34"&gt;  34:&lt;/span&gt;     }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum35"&gt;  35:&lt;/span&gt;     &lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum36"&gt;  36:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; ArrayList&amp;lt;Box&amp;gt; createStackDP(Box[] boxes, Box bottom, HashMap&amp;lt;Box, ArrayList&amp;lt;Box&amp;gt;&amp;gt; stack_map) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum37"&gt;  37:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (bottom != null &amp;amp;&amp;amp; stack_map.containsKey(bottom)) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum38"&gt;  38:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; stack_map.get(bottom);&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum39"&gt;  39:&lt;/span&gt;         }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum40"&gt;  40:&lt;/span&gt;         &lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum41"&gt;  41:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; max_height = 0;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum42"&gt;  42:&lt;/span&gt;         ArrayList&amp;lt;Box&amp;gt; max_stack = null;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum43"&gt;  43:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; boxes.length; i++) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum44"&gt;  44:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (boxes[i].canBeAbove(bottom)) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum45"&gt;  45:&lt;/span&gt;                 ArrayList&amp;lt;Box&amp;gt; new_stack = createStackDP(boxes, boxes[i], stack_map);&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum46"&gt;  46:&lt;/span&gt;                 &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; new_height = stackHeight(new_stack);&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum47"&gt;  47:&lt;/span&gt;                 &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (new_height &amp;gt; max_height) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum48"&gt;  48:&lt;/span&gt;                     max_stack = new_stack;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum49"&gt;  49:&lt;/span&gt;                     max_height = new_height;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum50"&gt;  50:&lt;/span&gt;                 }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum51"&gt;  51:&lt;/span&gt;             }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum52"&gt;  52:&lt;/span&gt;         }        &lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum53"&gt;  53:&lt;/span&gt;         &lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum54"&gt;  54:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (max_stack == null) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum55"&gt;  55:&lt;/span&gt;             max_stack = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ArrayList&amp;lt;Box&amp;gt;();&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum56"&gt;  56:&lt;/span&gt;         }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum57"&gt;  57:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (bottom != null) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum58"&gt;  58:&lt;/span&gt;             max_stack.add(0, bottom);&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum59"&gt;  59:&lt;/span&gt;         }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum60"&gt;  60:&lt;/span&gt;         stack_map.put(bottom, max_stack);&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum61"&gt;  61:&lt;/span&gt;         &lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum62"&gt;  62:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; (ArrayList&amp;lt;Box&amp;gt;)max_stack.clone();&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum63"&gt;  63:&lt;/span&gt;     }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum64"&gt;  64:&lt;/span&gt;         &lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum65"&gt;  65:&lt;/span&gt;     &lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum66"&gt;  66:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; main(String[] args) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum67"&gt;  67:&lt;/span&gt;         Box[] boxes = { &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Box(1, 7, 4), &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Box(2, 6, 9), &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Box(4, 9, 6), &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Box(10, 12, 8),&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum68"&gt;  68:&lt;/span&gt;                         &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Box(6, 2, 5), &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Box(3, 8, 5), &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Box(5, 7, 7), &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Box(2, 10, 16), &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Box(12, 15, 9)};&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum69"&gt;  69:&lt;/span&gt;&amp;#160; &lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum70"&gt;  70:&lt;/span&gt;         &lt;span style="color: #008000"&gt;//ArrayList&amp;lt;Box&amp;gt; stack = createStackDP(boxes, null, new HashMap&amp;lt;Box, ArrayList&amp;lt;Box&amp;gt;&amp;gt;());&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum71"&gt;  71:&lt;/span&gt;         ArrayList&amp;lt;Box&amp;gt; stack = createStackR(boxes, null);        &lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum72"&gt;  72:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = stack.size() - 1; i &amp;gt;= 0; i--) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum73"&gt;  73:&lt;/span&gt;             Box b = stack.get(i);&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum74"&gt;  74:&lt;/span&gt;             System.out.println(b.toString());&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum75"&gt;  75:&lt;/span&gt;         }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum76"&gt;  76:&lt;/span&gt;     }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum77"&gt;  77:&lt;/span&gt;&amp;#160; &lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum78"&gt;  78:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Box {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum79"&gt;  79:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; width;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum80"&gt;  80:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; height;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum81"&gt;  81:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; depth;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum82"&gt;  82:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Box(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; w, &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; h, &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; d) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum83"&gt;  83:&lt;/span&gt;         width = w;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum84"&gt;  84:&lt;/span&gt;         height = h;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum85"&gt;  85:&lt;/span&gt;         depth = d;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum86"&gt;  86:&lt;/span&gt;     }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum87"&gt;  87:&lt;/span&gt;     &lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum88"&gt;  88:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;boolean&lt;/span&gt; canBeUnder(Box b) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum89"&gt;  89:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (width &amp;gt; b.width &amp;amp;&amp;amp; height &amp;gt; b.height &amp;amp;&amp;amp; depth &amp;gt; b.depth) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum90"&gt;  90:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; true;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum91"&gt;  91:&lt;/span&gt;         }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum92"&gt;  92:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; false;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum93"&gt;  93:&lt;/span&gt;     }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum94"&gt;  94:&lt;/span&gt;     &lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum95"&gt;  95:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;boolean&lt;/span&gt; canBeAbove(Box b) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum96"&gt;  96:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (b == null) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum97"&gt;  97:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; true;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum98"&gt;  98:&lt;/span&gt;         }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum99"&gt;  99:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (width &amp;lt; b.width &amp;amp;&amp;amp; height &amp;lt; b.height &amp;amp;&amp;amp; depth &amp;lt; b.depth) {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum100"&gt; 100:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; true;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum101"&gt; 101:&lt;/span&gt;         }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum102"&gt; 102:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; false;        &lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum103"&gt; 103:&lt;/span&gt;     }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum104"&gt; 104:&lt;/span&gt;     &lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum105"&gt; 105:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; String toString() {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum106"&gt; 106:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #006080"&gt;&amp;quot;Box(&amp;quot;&lt;/span&gt; + width + &lt;span style="color: #006080"&gt;&amp;quot;,&amp;quot;&lt;/span&gt; + height + &lt;span style="color: #006080"&gt;&amp;quot;,&amp;quot;&lt;/span&gt; + depth + &lt;span style="color: #006080"&gt;&amp;quot;)&amp;quot;&lt;/span&gt;;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum107"&gt; 107:&lt;/span&gt;     }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum108"&gt; 108:&lt;/span&gt; }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;/div&gt;&lt;/div&gt;&lt;/span&gt;&lt;/pre&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-2243833391990591240?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/2243833391990591240/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=2243833391990591240' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/2243833391990591240'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/2243833391990591240'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/08/box-stacking.html' title='Box Stacking'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/-kFC232x16Qk/Tj2so9gs6GI/AAAAAAAAE8k/ztPi6NM3r3Y/s72-c/image_thumb%25255B1%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-5133891715699534694</id><published>2011-08-06T13:55:00.001-07:00</published><updated>2011-09-26T11:48:46.784-07:00</updated><title type='text'>Making change : Coin denomination problem</title><content type='html'>&lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You are given n types of coins with values {v1,…..,vn} and a cost C. You may assume v1 = 1 so that it is always possible to make any cost. Give an algorithm for finding the smallest number of coins required to sum to C exactly.       &lt;br /&gt;For example, assume you coins of values 1, 5, and 10. Then the smallest number of coins to make 26 is 4: 2 coins of value 10, 1 coin of value 5, and 1 coin of value 1.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Recursion: We recurse on M(j), the minimum number of coins required to make change for cost j.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-ulgcC54Zovo/Tj2qQ-T_E5I/AAAAAAAAE8Y/tpfph6wD9b4/s1600-h/image%25255B3%25255D.png"&gt;&lt;font size="2"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/-uDHbHrEopGk/Tj2qRW-QTcI/AAAAAAAAE8c/4qGL5D1qpLQ/image_thumb%25255B1%25255D.png?imgmax=800" width="545" height="92" /&gt;&lt;/font&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Running Time: M has C elements and computing each element takes O(n) time so the total running time is O(nC).&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Working code:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:ec18ccef-ea2a-42e3-9fc8-c03846eff38a" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 605px; height: 465px;" src="http://lh3.ggpht.com/-YGmSMLW1a7M/ToDJAscI1CI/AAAAAAAAFAY/7d-U197SgQ4/transformedimage%25255B26%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/p&gt;    &lt;h4&gt;All Possible Solutions &lt;/h4&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:099db551-ba5b-4e87-aa7c-93e0bdff25a5" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 618px; height: 640px;" src="http://lh3.ggpht.com/-uyUMYG0atU4/ToDJBuBYmvI/AAAAAAAAFAc/gp16xKDi768/transformedimage%25255B55%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:1d7ae50b-677a-49d7-8c3a-0c0311795ef5" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 675px; height: 167px;" src="http://lh6.ggpht.com/-IB2T2MAc0Zg/ToDJCQbMIWI/AAAAAAAAFAg/Cn3jzmBSiAE/transformedimage%25255B91%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;h4&gt;Optimal Solution &lt;/h4&gt;  &lt;p&gt;The optimal solution to this program can be found by using dynamic programming and its runtime can be considerably reduced by a technique called memoization.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:f115a11a-c9a6-4fe4-aa5d-9d65c81c853e" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 623px; height: 712px;" src="http://lh6.ggpht.com/-ZvNuGfQEKsE/ToDJDZTEXwI/AAAAAAAAFAk/pXSn-qH1WsM/transformedimage%25255B146%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-5133891715699534694?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/5133891715699534694/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=5133891715699534694' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/5133891715699534694'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/5133891715699534694'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/08/making-change-coin-denomination-problem.html' title='Making change : Coin denomination problem'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/-uDHbHrEopGk/Tj2qRW-QTcI/AAAAAAAAE8c/4qGL5D1qpLQ/s72-c/image_thumb%25255B1%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-6134697939667947365</id><published>2011-08-06T13:48:00.001-07:00</published><updated>2011-08-06T13:48:10.658-07:00</updated><title type='text'>Dynamic programming</title><content type='html'>&lt;p&gt;&lt;font size="2"&gt;A DP is an algorithmic technique which is usually based on a recurrent formula and one (or some) starting states. A sub-solution of the problem is constructed from previously found ones. DP solutions have a polynomial complexity which assures a much faster running time than other techniques like backtracking, brute-force etc. &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;We will look at some dynamic programming problems from easier to harder. Solutions to these problems, as with most DP problems, take the form of a recurrence relation, a short correctness proof of the recurrence relation and a running time analysis.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;Q 1: Maximum value contiguous subsequence: Given a sequence of n real numbers, a1; a2; :::; an, give an algorithm for finding a contiguous subsequence for which the value of the sum of the elements is maximized.&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;Solution:       &lt;br /&gt;&lt;/strong&gt;Recursion: We recurse on the maximum value subsequence ending at j:&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-nySTEy0B2uI/Tj2oh2-_iJI/AAAAAAAAE8Q/I830CTlynCA/s1600-h/image%25255B3%25255D.png"&gt;&lt;font size="2"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/-rsj3MYYNhik/Tj2oibL9NMI/AAAAAAAAE8U/ZjngXjNZMS8/image_thumb%25255B1%25255D.png?imgmax=800" width="453" height="73" /&gt;&lt;/font&gt;&lt;/a&gt;    &lt;br /&gt;&lt;font size="2"&gt;With each element of M, you also keep the starting element of the sum (the same as for M(j -1) or j if you restart). At the end, you scan M for the maximum value and return it and the starting and ending indexes. Alternatively, you could keep track of the maximum value as you create M.     &lt;br /&gt;&lt;strong&gt;Running Time:&lt;/strong&gt; M is size n and evaluating each element of M takes O(1) time for O(n) time to create M. Scanning M also takes O(n) time for a total time of O(n).&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-6134697939667947365?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/6134697939667947365/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=6134697939667947365' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/6134697939667947365'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/6134697939667947365'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/08/dynamic-programming.html' title='Dynamic programming'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/-rsj3MYYNhik/Tj2oibL9NMI/AAAAAAAAE8U/ZjngXjNZMS8/s72-c/image_thumb%25255B1%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-4038373849575793813</id><published>2011-07-25T12:19:00.001-07:00</published><updated>2011-09-26T08:22:38.676-07:00</updated><title type='text'>Merging two sorted arrays</title><content type='html'>&lt;h5&gt;Given two sorted arrays a[]={1,3,77,78,90} and b[]={2,5,79,81}. Merge these two arrays, no extra spaces are allowed. Output has to be a[]={1,2,3,5,77} and b[]={78,79,81,90}. &lt;/h5&gt;  &lt;p&gt;&lt;font size="2"&gt;Algorithm: use two pointer &amp;amp; points l to 1st &amp;amp; r to 2nd array      &lt;br /&gt;if element in 1st is smaller than 2nd array element at index i then increment 1st pointer       &lt;br /&gt;else       &lt;br /&gt;swap element at index i in both array &amp;amp;b increment 1st pointer &amp;amp; sort 2nd       &lt;br /&gt;array its not necessary only if we need sorted output in each array       &lt;br /&gt;Solution:       &lt;br /&gt;size of a=m size of b =n       &lt;br /&gt;a[]={1,3,77,78,90} and b[]={2,5,79,81}       &lt;br /&gt;l r &lt;/font&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:4e78576e-4759-40bf-a1c2-15b929768558" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 510px; height: 236px;" src="http://lh4.ggpht.com/-AaVrjfRFOqU/ToCYvb6ar9I/AAAAAAAAE-U/7-tcVHbUQro/transformedimage%25255B43%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;p&gt;&lt;font size="2"&gt;     &lt;div id="codeSnippetWrapper"&gt;&lt;/div&gt;   &lt;/font&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-4038373849575793813?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/4038373849575793813/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=4038373849575793813' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4038373849575793813'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4038373849575793813'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/07/merging-two-sorted-arrays.html' title='Merging two sorted arrays'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/-AaVrjfRFOqU/ToCYvb6ar9I/AAAAAAAAE-U/7-tcVHbUQro/s72-c/transformedimage%25255B43%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-48940912051480084</id><published>2011-07-25T12:14:00.001-07:00</published><updated>2011-07-25T12:16:09.788-07:00</updated><title type='text'>Converting a word into palindrome</title><content type='html'>&lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;&lt;font style="font-weight: normal"&gt;Q: Given a word, convert it into a palindrome with minimum addition of letters to it. letters can be added anywhere in the word. for eg if hello is given, result should be hellolleh&lt;/font&gt;. &lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;Algorithm:&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;&lt;font size="2"&gt;You need to find the longest palindrome at the end of the string. An algorithm to see if a string is a palindrome can be created by simply running one pointer from the start of the string and one from the end, checking that the characters they refer to are identical, until they meet in the middle. Try that with the full string. If that doesn't work, save the first character on a stack then see if the remaining characters form a palindrome. If that doesn't work, save the second character as well and check again from the third character onwards.     &lt;br /&gt;&lt;/font&gt;&lt;/p&gt; &lt;font size="2"&gt;&lt;/font&gt;  &lt;p align="justify"&gt;&lt;font size="2"&gt;Eventually you'll end up with a series of saved characters and the remaining string which is a palindrome. Best case is if the original string was a palindrome in which case the stack will be empty. Worst case is one character left (a one-character string is automatically a palindrome) and all the others on the stack. The number of characters you need to add to the end of the original string is the number of characters on the stack. To actually make the palindrome, pop the characters off the stack one-by-one and put them at the start and the end of the palindromic string.&lt;/font&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-48940912051480084?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/48940912051480084/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=48940912051480084' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/48940912051480084'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/48940912051480084'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/07/converting-word-into-palindrome.html' title='Converting a word into palindrome'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-5186200806449881204</id><published>2011-06-11T09:38:00.001-07:00</published><updated>2011-07-11T12:03:06.806-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Technical'/><title type='text'>Windows 8 demo</title><content type='html'>&lt;p&gt;If you haven’t seen it already, may be you should. The video addresses many things in details and how windows 8 is going to change everything in developer market.&lt;/p&gt;  &lt;p&gt;&lt;iframe height="288" src="http://www.microsoft.com:80/presspass/silverlightApps/videoplayer3/standalone.aspx?contentID=win8_preview1&amp;amp;src=/presspass/presskits/windows7/channel.xml" frameborder="0" width="512" scrolling="no"&gt;&lt;/iframe&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-5186200806449881204?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/5186200806449881204/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=5186200806449881204' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/5186200806449881204'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/5186200806449881204'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/06/windows-8-demo.html' title='Windows 8 demo'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-4425165306545314048</id><published>2011-05-06T02:18:00.001-07:00</published><updated>2011-07-11T12:02:46.101-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview '/><title type='text'>what happens when you type in a URL in browser</title><content type='html'>&lt;ol&gt;   &lt;li&gt;browser checks cache; if requested object is in cache and is fresh, skip to #9 &lt;/li&gt;    &lt;li&gt;browser asks OS for server IP address &lt;/li&gt;    &lt;li&gt;OS makes a DNS lookup and replies to browser &lt;/li&gt;    &lt;li&gt;browser opens a TCP connection to server (this step is much more complex with HTTPS) &lt;/li&gt;    &lt;li&gt;browser sends the HTTP request through connection &lt;/li&gt;    &lt;li&gt;browser receives HTTP response and may close the TCP connection, or reuse it for another request &lt;/li&gt;    &lt;li&gt;browser checks if the response is a redirect, authorization request, etc.; this is handled differently from normal responses &lt;/li&gt;    &lt;li&gt;if cacheable, response is stored in cache &lt;/li&gt;    &lt;li&gt;browser decodes response (e.g. if it's gzipped) &lt;/li&gt;    &lt;li&gt;browser determines what to do with response (e.g. is it a HTML page, is it an image, is it a sound clip?) &lt;/li&gt;    &lt;li&gt;browser renders response, or offers a download dialog for unrecognized types&lt;/li&gt; &lt;/ol&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-4425165306545314048?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/4425165306545314048/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=4425165306545314048' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4425165306545314048'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4425165306545314048'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/05/what-happens-when-you-type-in-url-in.html' title='what happens when you type in a URL in browser'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-6184587563865133916</id><published>2011-04-14T10:07:00.001-07:00</published><updated>2011-07-11T12:02:04.998-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Pattern'/><title type='text'>Design Pattern:Decorator</title><content type='html'>&lt;p&gt;The Decorator attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.Also called as Wrapper.&lt;/p&gt;  &lt;p&gt;Problem&lt;/p&gt;  &lt;p&gt;Sometimes we want to add responsibilities to individual objects (aTextView ) not to an entire class. ( i.e. how to give control to client to decorate the component with a border / a scroll bar )&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Illustration:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_Pg0RCzJJYC4/TacpuJ1SUnI/AAAAAAAAEu8/KzNuxV3LmGo/s1600-h/image%5B3%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TacpvJgWIfI/AAAAAAAAEvA/BjAygvCsk14/image_thumb%5B1%5D.png?imgmax=800" width="580" height="381" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/Tacpv6cY2LI/AAAAAAAAEvE/EzPB-yGBl3Y/s1600-h/image%5B15%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_Pg0RCzJJYC4/TacpwWci_LI/AAAAAAAAEvI/G9cKj6f8y_0/image_thumb%5B7%5D.png?imgmax=800" width="575" height="413" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;One way to add responsibilities is with inheritance.Inheriting a border with another class puts a border along very subclass instance ( inflexible solution ).&lt;/li&gt;    &lt;li&gt;A more flexible approach is to enclose the component in another objects that adds the border. The enclosing object is called Decorator.&lt;/li&gt;    &lt;li&gt;The Decorator confirm to interface of the component it decorate so that its presence is transparent to its components clients.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Applicability&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Use Decorator&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;To add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects.&lt;/li&gt;    &lt;li&gt;For responsibilities that can be withdrawn.&lt;/li&gt;    &lt;li&gt;When extension by subclassing in impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for subclassing.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Structure&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_Pg0RCzJJYC4/TacpxYALUvI/AAAAAAAAEvM/Sy3y8aCUZYI/s1600-h/image%5B3%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_Pg0RCzJJYC4/TacpyK3V9QI/AAAAAAAAEvQ/MowMoHELhKk/image_thumb%5B1%5D.gif?imgmax=800" width="562" height="248" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Collaborations&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Decorator forwards requests to its Component object. It may optionally perform additional operations before and after forwarding the request.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Consequences&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;More flexibility than static inheritance:&lt;/li&gt; &lt;/ul&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;to add or delete responsibilities to objects at runtime simply by attaching and detaching them. &lt;/li&gt;      &lt;li&gt;Providing different decorator classes for a specific component class lets you mix and match responsibilities.&lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;ul&gt;   &lt;li&gt;Avoids feature-laden classes high up in the hierarchy:&lt;/li&gt; &lt;/ul&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;Decorator offers a pay-as-you-go approach to adding responsibilities.Instead of trying to support all features in a complex, customizable class, you can define simple class and add functionality incrementally with Decorator objects.&lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;ul&gt;   &lt;li&gt;A decorator and its component aren’t identical: &lt;/li&gt; &lt;/ul&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;A decorator acts as a transparent enclosure.&lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;ul&gt;   &lt;li&gt;Lots of little objects : &lt;/li&gt; &lt;/ul&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;Decorator often results in system composed of lots of little objects that all look like, this may leed to maintenance nightmare and hard to learn.&lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;Model&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/Tacpy2GQfJI/AAAAAAAAEvU/Wg1zPHUFM1g/s1600-h/image%5B7%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TacpzkA8GFI/AAAAAAAAEvY/uBhMBhOrlSU/image_thumb%5B3%5D.gif?imgmax=800" width="560" height="365" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Related Patterns&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Adapter: Adapter changes an object's interface, Decorator enhances an object's responsibilities. Decorator is thus more transparent to the client. As a consequence, Decorator supports recursive composition, which isn't possible with pure Adapters&lt;/p&gt;  &lt;p&gt;Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface&lt;/p&gt;  &lt;p&gt;Composite: A decorator can be viewed as a degenerate composite with only one component. However, a decorator adds additional responsibilities—it isn’t intended for object aggregation.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-6184587563865133916?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/6184587563865133916/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=6184587563865133916' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/6184587563865133916'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/6184587563865133916'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/04/design-patterndecorator.html' title='Design Pattern:Decorator'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/_Pg0RCzJJYC4/TacpvJgWIfI/AAAAAAAAEvA/BjAygvCsk14/s72-c/image_thumb%5B1%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-999246349502289799</id><published>2011-04-14T09:47:00.001-07:00</published><updated>2011-07-11T12:02:05.127-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Pattern'/><title type='text'>Design Pattern: Abstract Factory</title><content type='html'>&lt;p&gt;The purpose of the Abstract Factory is to provide an interface for creating families of related objects, without specifying concrete classes.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;• How should one go about designing applications which have to be adopted to different Look and feel standards?&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;•One could solve this problem by defining abstract widgets factory class that declares an interface for creating each basic kind of widgets ( control ) . &lt;/p&gt;  &lt;p&gt;•There is also an abstract class for each kind of widget, and concrete sub classes implement widgets for specific look and standards. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Illustration:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_Pg0RCzJJYC4/TaclHxBV-FI/AAAAAAAAEuk/14ejqAOrl0c/s1600-h/image%5B4%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TaclIchFMFI/AAAAAAAAEuo/OG6tHsw9RQA/image_thumb%5B2%5D.gif?imgmax=800" width="572" height="388" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Applicability:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Use the Abstract Factory pattern when&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;A system should be independent of how its products are created, composed, and represented.&lt;/li&gt;    &lt;li&gt;A system should be configured with one of multiple families of products.&lt;/li&gt;    &lt;li&gt;A family of related product objects is designed to be used together, and you need to enforce this constraint.&lt;/li&gt;    &lt;li&gt;You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Structure:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/TaclJGEnPnI/AAAAAAAAEus/dCH3mG5R5TU/s1600-h/image%5B9%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_Pg0RCzJJYC4/TaclJqrryBI/AAAAAAAAEuw/yx21JovgiNQ/image_thumb%5B5%5D.gif?imgmax=800" width="569" height="363" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Collaborations&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Normally a single instance of a ConcreteFactory class is created at run-time. This concrete factory creates product objects having a particular implementation. To create different product objects, clients should use a different concrete factory.&lt;/p&gt;  &lt;p&gt;AbstractFactory defers creation of product objects to its ConcreteFactory subclass.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Consequences&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;It isolates concrete classes.&lt;/li&gt;    &lt;li&gt;It makes exchanging product families easy.&lt;/li&gt;    &lt;li&gt;It promotes consistency among products.&lt;/li&gt;    &lt;li&gt;Supporting new kinds of products is difficult.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Model:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_Pg0RCzJJYC4/TaclKf-l5YI/AAAAAAAAEu0/uUDsKnd8NQs/s1600-h/image%5B13%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_Pg0RCzJJYC4/TaclLA7sDZI/AAAAAAAAEu4/G_TdG1ZWqHQ/image_thumb%5B7%5D.gif?imgmax=800" width="566" height="362" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;AbstractFactory classes are often implemented with factory methods, but they can also be implemented using Prototype.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-999246349502289799?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/999246349502289799/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=999246349502289799' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/999246349502289799'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/999246349502289799'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/04/design-pattern-abstract-factory.html' title='Design Pattern: Abstract Factory'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/_Pg0RCzJJYC4/TaclIchFMFI/AAAAAAAAEuo/OG6tHsw9RQA/s72-c/image_thumb%5B2%5D.gif?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-7705799249319077675</id><published>2011-04-07T10:33:00.001-07:00</published><updated>2011-07-11T12:02:05.054-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Pattern'/><title type='text'>Design Pattern: Observer</title><content type='html'>&lt;p&gt;The Observer defines a one to many relationship, so that when one object changes state, the others are notified and updated automatically. &lt;/p&gt;  &lt;p&gt;The Observer design pattern is about passing notifications around to update a set of objects when some important event has occurred. You can add new observer objects at runtime and remove them as needed. When an event occurs, all registered observers are notified. Below Figure shows how it works; an observer can register itself with the subject.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZ31UmPvS7I/AAAAAAAAEtQ/sQMNqIDAIlU/s1600-h/image%5B4%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZ31VXlJJUI/AAAAAAAAEtU/Je0V3HjlUxA/image_thumb%5B2%5D.png?imgmax=800" width="558" height="214" /&gt;&lt;/a&gt;    &lt;br /&gt;And another observer, Observer 2, can register itself as well, as shown in below figure&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZ31V-3qdHI/AAAAAAAAEtY/jPez2XukqGI/s1600-h/image%5B9%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZ31Wn0cWdI/AAAAAAAAEtc/7Q-DLEd9wnQ/image_thumb%5B5%5D.png?imgmax=800" width="559" height="229" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Now the subject is keeping track of two observers. When an event occurs, the subject notifies both observers.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZ31Xlx2rDI/AAAAAAAAEtg/uv34k5FLGJM/s1600-h/image%5B14%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZ31YX9tb_I/AAAAAAAAEtk/bnuTz1tjeuU/image_thumb%5B8%5D.png?imgmax=800" width="559" height="217" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Illustration:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZ31ZDfGGUI/AAAAAAAAEto/nMtqwPR7NLE/s1600-h/Picture1%5B4%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Picture1" border="0" alt="Picture1" src="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZ31aLnKgjI/AAAAAAAAEts/ZcGse-17YB4/Picture1_thumb%5B2%5D.png?imgmax=800" width="563" height="345" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The observer pattern describes how to establish these relationships. The key objects in this pattern are subject and observer. &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;A subject may have any number of dependent observer. &lt;/li&gt;    &lt;li&gt;All observers are notified whenever the subject undergoes a change in state.&lt;/li&gt;    &lt;li&gt;In response, each observer will query the subject to synchronize its state with the subject’s state.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Applicability:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Use the Observer pattern in any of the following situations:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independently.&lt;/li&gt;    &lt;li&gt;When a change to one object requires changing others, and you don’t know how many objects need to be changed.&lt;/li&gt;    &lt;li&gt;When an object should be able to notify other objects without making assumptions about who these objects are. In other words, you don’t want these objects tightly coupled.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Structure&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZ31bHzKtuI/AAAAAAAAEtw/JhgELMhvp0E/s1600-h/image%5B18%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZ31cLeod0I/AAAAAAAAEt0/e8o8kfLNSJw/image_thumb%5B10%5D.png?imgmax=800" width="551" height="350" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Collaborations&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;ConcreteSubject notifies its observers whenever a change occurs that could make its observers’ state inconsistent with its own.&lt;/li&gt;    &lt;li&gt;After being informed of a change in the concrete subject, a ConcreteObserver object may query the subject for information. ConcreteObserver uses this information to reconcile its state with that of the subject.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Consequences&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Abstract coupling between Subject and Observer&lt;/li&gt;    &lt;li&gt;Support for broadcast communication.&lt;/li&gt;    &lt;li&gt;Unexpected updates&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Model&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZ31dF3gp0I/AAAAAAAAEt4/M9njAfAVw-w/s1600-h/image%5B4%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZ31dvNuk0I/AAAAAAAAEt8/JqUpS-LZW0s/image_thumb%5B2%5D.gif?imgmax=800" width="548" height="322" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-7705799249319077675?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/7705799249319077675/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=7705799249319077675' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/7705799249319077675'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/7705799249319077675'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/04/design-pattern-observer.html' title='Design Pattern: Observer'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_Pg0RCzJJYC4/TZ31VXlJJUI/AAAAAAAAEtU/Je0V3HjlUxA/s72-c/image_thumb%5B2%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-4351533760803665741</id><published>2011-04-07T10:16:00.001-07:00</published><updated>2011-07-11T12:02:04.831-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Pattern'/><title type='text'>Design Pattern: Proxy</title><content type='html'>&lt;p&gt;The Proxy provides a surrogate or place holder to provide access to an object. You need to support resource-hungry objects, and you do not want to instantiate such objects unless and until they are actually requested by the client.&lt;/p&gt;  &lt;p&gt;Say that you’ve got some local code that’s used to dealing with a local object as shown in the figure below &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZ3xVPlx9TI/AAAAAAAAEsQ/96dHYDv67LQ/s1600-h/image%5B6%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" align="left" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZ3xVsw-BfI/AAAAAAAAEsU/XQP1xsRrvK0/image_thumb%5B4%5D.png?imgmax=800" width="537" height="175" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;But now say that you want to deal with some remote object, somewhere else in the world. How can you make the local code think it’s dealing with a local object still when in fact it’s working with that remote object?&lt;/p&gt;  &lt;p&gt;With a proxy. A proxy is a stand-in for another object that makes the local code think it’s dealing with a local object. Behind the scenes, the proxy connects to the remote object, all the while making the local code believe it’s working with a local object, as you can see in figure below&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZ3xWYcYnyI/AAAAAAAAEsY/VuJPvJTjwmE/s1600-h/image%5B12%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_Pg0RCzJJYC4/TZ3xW0aksDI/AAAAAAAAEsc/bK-wh-Dqy_0/image_thumb%5B8%5D.png?imgmax=800" width="535" height="178" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Illustration :problem&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Consider a document editor ( Html creator ) that can embed graphical objects in a document. Some graphical objects, like large raster images, can be expensive to create. But opening a document should be fast, so we should avoid creating all the expensive objects at once when the document is opened. This isn’t necessary anyway, because not all of these objects will be visible in the document at the same time. &lt;/p&gt;  &lt;p&gt;These constraints would suggest creating each expensive object on demand, which in this case occurs when an image becomes visible. &lt;/p&gt;  &lt;p&gt;But what do we put in the document in place of the image? And how can we hide the fact that the image is created on demand so that we don’t complicate the editor’s implementation? &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Illustration: solution&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The solution is to use another object, an image proxy, that acts as a stand-in for the real image. &lt;/li&gt;    &lt;li&gt;The proxy acts just like the image and takes care of instantiating it when it’s required.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZ3xXrxfQfI/AAAAAAAAEsg/VqRBd2x2zWA/s1600-h/image%5B4%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZ3xYX43pII/AAAAAAAAEsk/gx_blFxpcYM/image_thumb%5B2%5D.gif?imgmax=800" width="547" height="184" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The image proxy creates the real image only when the document editor asks it to display itself by invoking its Draw operation.&lt;/li&gt;    &lt;li&gt;The proxy forwards subsequent requests directly to the image. &lt;/li&gt;    &lt;li&gt;It must therefore keep a reference to the image after creating it.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Illustration: example Document Editor&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZ3xZItohlI/AAAAAAAAEso/FAJdc5KyJM0/s1600-h/image%5B9%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZ3xZtgYUGI/AAAAAAAAEss/vsLyQWkQ5rE/image_thumb%5B5%5D.gif?imgmax=800" width="525" height="311" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Design a surrogate, or proxy, object that: instantiates the real object the first time the client makes a request of the proxy, remembers the identity of this real object, and forwards the instigating request to this real object. &lt;/p&gt;  &lt;p&gt;Then all subsequent requests are simply forwarded directly to the encapsulated real object.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Applicability&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Proxy is applicable whenever there is a need for a more versatile or sophisticated reference to an object than a simple pointer. Here are several common situations in which the Proxy pattern is applicable:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;A remote proxy provides a local representative for an object in a different address space. NEXTSTEP uses the class NXProxy for this purpose.&lt;/li&gt;    &lt;li&gt;A virtual proxy creates expensive objects on demand. The Image-Proxy described in the Motivation in an example of such a proxy.&lt;/li&gt;    &lt;li&gt;A Protection proxy controls access to the original object. Protection proxies are useful when objects should have different access rights.&lt;/li&gt;    &lt;li&gt;A smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Typical uses include&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;counting the number of references to the real object so that it can be freed automatically when them are no more references (also called smart pointers).&lt;/li&gt;    &lt;li&gt;loading a persistent object into memory when it’s first referenced.&lt;/li&gt;    &lt;li&gt;checking that the real object is locked before it’s accessed to ensure that no other object can change it&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Structure&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZ3xaGVqw3I/AAAAAAAAEsw/H1CUTtCe-gY/s1600-h/image%5B12%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" align="left" src="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZ3xaohTc6I/AAAAAAAAEs0/bYxDkB_XQkk/image_thumb%5B6%5D.gif?imgmax=800" width="244" height="160" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Object diagram of a proxy structure at run-time:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZ3xbHR5pDI/AAAAAAAAEs4/yA7ZT3BPhvg/s1600-h/image%5B17%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZ3xbsDa4yI/AAAAAAAAEs8/Mx9oTu-E8cI/image_thumb%5B9%5D.gif?imgmax=800" width="311" height="95" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Collaborations&lt;/p&gt;  &lt;p&gt;Proxy forward requests to RealSubject when appropriate, depending on the kind of proxy&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZ3xcFg9_VI/AAAAAAAAEtA/b-IMFnyyEP4/s1600-h/image%5B22%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_Pg0RCzJJYC4/TZ3xcxt8dNI/AAAAAAAAEtE/VKHKD-WquhA/image_thumb%5B12%5D.gif?imgmax=800" width="530" height="178" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Consequences&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The proxy pattern introduces a level of indirection when accessing an object. The additional indirection has many uses, depending on the kind of proxy:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;A remote proxy can hide the fact that an object resides in a different address space.&lt;/li&gt;    &lt;li&gt;A virtual proxy can perform optimizations such as creating an object on demand.&lt;/li&gt;    &lt;li&gt;Both protection proxies and smart references allow additional housekeeping tasks when an object is accessed.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;Model&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZ3xdRFcH0I/AAAAAAAAEtI/Bjj5tFKP7ZE/s1600-h/image%5B26%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_Pg0RCzJJYC4/TZ3xd0WEpoI/AAAAAAAAEtM/AXRk9WcbMhY/image_thumb%5B14%5D.gif?imgmax=800" width="376" height="201" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-4351533760803665741?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/4351533760803665741/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=4351533760803665741' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4351533760803665741'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4351533760803665741'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/04/design-pattern-proxy.html' title='Design Pattern: Proxy'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/_Pg0RCzJJYC4/TZ3xVsw-BfI/AAAAAAAAEsU/XQP1xsRrvK0/s72-c/image_thumb%5B4%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-5868714120159167246</id><published>2011-04-07T09:53:00.001-07:00</published><updated>2011-07-11T12:02:04.990-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Pattern'/><title type='text'>Design Pattern: Mediator</title><content type='html'>&lt;p align="justify"&gt;The Mediator defines an object that controls how a set of objects interact. Loose coupling between colleague objects is achieved by having colleagues communicate with the Mediator, rather than with each other. Many a times in projects communication between components are complex.Due to this the logic between components becomes very complex. Mediator Pattern helps the objects to communicate in a disassociated manner, which leads to minimizing complexity. &lt;/p&gt;  &lt;p align="justify"&gt;Say that you’ve got a four-page Web site that lets users browse a store and make purchases. As things stand, the user can move from page to page. But there’s a problem — the code in each page has to know when to jump to a new page as well as how to activate the new page. You’ve got a lot of possible connections and a lot of duplicate code in the various pages.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZ3r2PUGh4I/AAAAAAAAErQ/5jMdmMpMfaw/s1600-h/image%5B3%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZ3r2z5ze0I/AAAAAAAAErU/88yecS0IOyQ/image_thumb%5B1%5D.png?imgmax=800" width="567" height="271" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p align="justify"&gt;You can use a mediator here to encapsulate all the navigation code out of the separate pages and place it into a mediator object instead. From then on, each page just has to report any change of state to the mediator, and the mediator knows what page to send the user to.&amp;#160; You can build the mediator to deal with the internals of each page so the various pages don’t have to know the intimate details of the other pages (such as which methods to call). And when it’s time to modify the navigation code that takes users from page to page, that code is all collected in one place, so   &lt;br /&gt;it’s easier to modify.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZ3r3Tm4s_I/AAAAAAAAErY/-AkTx053mr8/s1600-h/image%5B8%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZ3r4GRAXCI/AAAAAAAAErc/1-BkpX6sdoM/image_thumb%5B4%5D.png?imgmax=800" width="566" height="328" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Illustration: Problem&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Consider the implementation of dialog boxes in a GUI.&amp;#160; A dialog box uses a window to present a collection of widgets such as buttons, menus, and entry fields, as shown here.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZ3r50Fei0I/AAAAAAAAErg/sgqd3nga3LQ/s1600-h/image%5B2%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" align="left" src="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZ3r6uRguLI/AAAAAAAAErk/XKH7TriEjH0/image_thumb.gif?imgmax=800" width="232" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Often there are dependencies between the widgets in the dialog. For example a button gets disabled or enabled when a certain entry in a list of choices of list box is selected . &lt;/p&gt;  &lt;p&gt;Different dialog boxes will have different dependencies between widgets. So even though dialogs display the same kinds of widgets, they can’t simply reuse stock widget classes; they have to be customized to reflect dialog-specific dependencies. Customizing them individually by subclassing will be tedious, since many classes are involved.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Illustration:Solution&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;We can avoid previous said problem by encapsulating collective behavior in a separate mediator object. A mediator is responsible for controlling and coordinating the iterations of a group of objects. The mediator serves as an intermediary that keeps objects in the group form referring to each other explicitly. The objects only know the mediator, thereby reducing the number of interconnection &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZ3r7BD1j7I/AAAAAAAAEro/ueQ6iE7qPvo/s1600-h/image%5B6%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" align="left" src="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZ3r71YDELI/AAAAAAAAErs/6zOQ3AGGbLk/image_thumb%5B2%5D.gif?imgmax=800" width="277" height="186" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;FontDialogDirector can be mediator between the widgets in a dialog box. &lt;/p&gt;  &lt;p&gt;A FontDialogDirector object knows the widgets in a dialog and coordinates their interaction.&lt;/p&gt;  &lt;p&gt;It acts as a hub of communication for widgets&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_Pg0RCzJJYC4/TZ3r8mb1RlI/AAAAAAAAErw/w333v0p-9EQ/s1600-h/image%5B14%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZ3r9oKqQBI/AAAAAAAAEr0/Z7133ik7Y3U/image_thumb%5B8%5D.png?imgmax=800" width="557" height="284" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In the above model notice who the director communicates with list box and entry field. Widgets communicate with each other only indirectly, through the director. All they know is the director and they in turn don’t know each other. &lt;/p&gt;  &lt;p&gt;Furthermore, because the behavior is localized in one class, it can be changed or replaced by extending or replacing that class&lt;/p&gt;  &lt;p&gt;Here’s the succession of events by which a list box’s selection passes to an entry field:&lt;/p&gt;  &lt;p&gt;The following interaction diagram illustrates how the objects cooperate to handle a change in a list box’s selection.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZ3r90HW23I/AAAAAAAAEr4/xSlpoJqpTJE/s1600-h/image%5B11%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" align="left" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZ3r-1E9u3I/AAAAAAAAEr8/BlO-gFJ_Cbo/image_thumb%5B5%5D.gif?imgmax=800" width="364" height="204" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;1. The list box tells its director that it’s changed&lt;/p&gt;  &lt;p&gt;2. The director gets the selection form the list box&lt;/p&gt;  &lt;p&gt;3. The director passes the selection to the entry field&lt;/p&gt;  &lt;p&gt;4. Now that the entry field contains some text, the director enables button(s) for initializing an action ( e.g. ‘demibold’,’oblique’&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Applicability&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Use the Mediator pattern when:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;a set of objects communicate in well-defined but complex ways . The resulting interdependencies are unstructured and difficult to understand.&lt;/li&gt;    &lt;li&gt;reusing an object is difficult because it refers to and communicates with many other objects.&lt;/li&gt;    &lt;li&gt;a behavior that’s distributed between several classes should be customizable without a lot of sub classing.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_Pg0RCzJJYC4/TZ3r_lQhbBI/AAAAAAAAEsA/rJnK2ggY_-o/s1600-h/image%5B18%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" align="left" src="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZ3sAA0VmeI/AAAAAAAAEsE/s1uEPFIzFis/image_thumb%5B10%5D.gif?imgmax=800" width="299" height="183" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_Pg0RCzJJYC4/TZ3sAo0VyBI/AAAAAAAAEsI/LRMedZ4F11Q/s1600-h/image%5B22%5D.gif"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZ3sBYGtshI/AAAAAAAAEsM/cvm-U9JY02M/image_thumb%5B12%5D.gif?imgmax=800" width="247" height="162" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Collaborations&lt;/p&gt;  &lt;p&gt;Colleagues send and receive requests form a Mediator object. The mediator implements the cooperative behavior by routing between the appropriate colleague(s).&lt;/p&gt;  &lt;p&gt;Consequences&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;It limits subclassing: mediator localizes behavior that otherwise would be distribured among various objects. Changing this behavior requires subclassing Mediator only; Colleagure classes can be reused as it is.&lt;/li&gt;    &lt;li&gt;It decouples colleagues:&lt;/li&gt;    &lt;li&gt;It simplifies object protocols: A one to many relationships are easier to understand, maintain, and extend. Thus mediator replaces many to many interactions with one-to-many interactions between the mediator and its colleagues.&lt;/li&gt;    &lt;li&gt;It abstracts how objects cooperate:&lt;/li&gt;    &lt;li&gt;It centralizes control:&lt;/li&gt; &lt;/ol&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-5868714120159167246?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/5868714120159167246/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=5868714120159167246' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/5868714120159167246'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/5868714120159167246'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/04/design-pattern-mediator.html' title='Design Pattern: Mediator'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_Pg0RCzJJYC4/TZ3r2z5ze0I/AAAAAAAAErU/88yecS0IOyQ/s72-c/image_thumb%5B1%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-1089387346121880650</id><published>2011-04-06T11:30:00.001-07:00</published><updated>2011-07-11T12:02:04.540-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Pattern'/><title type='text'>Design Pattern : Command</title><content type='html'>&lt;p&gt;The &lt;i&gt;Command &lt;/i&gt;pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests, queue or log requests, and support undoable operations.It falls into Behavioral category.&lt;/p&gt;  &lt;p&gt;Illustration: - Menus implemented using command&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Each choice in a Menu is an instance of a MenuItem class. An Application class creates these menus and their menu items along with the rest of the user interface. The Application class also keeps track of Document objects that a user has opened.&lt;/li&gt;    &lt;li&gt;The application configures each MenuItem with an instance of a concrete Command subclass. When the user selects a Menuitem, the Menuitem calls Execute on its command, and Execute carries out the operation.&lt;/li&gt;    &lt;li&gt;MenuItems don’t know which subclass of Command they use. Command subclasses store the receiver of the request and invoke one or more operations on the receiver. &lt;/li&gt;    &lt;li&gt;For example, PasteCommand supports pasting text from the clipboard into a Document. PasteCommand’s receiver is the Document object it is supplied upon instantiation. The Execute operation invokes Paste on the receiving Document.&lt;/li&gt;    &lt;li&gt;OpenCommand’s Execute operation is different: it prompts the user for a document name, creates a corresponding Document object, adds the document to the receiving application, and opens the document.&lt;/li&gt; &lt;/ul&gt; &lt;a href="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZyxEBj304I/AAAAAAAAEqc/CIMBEypRDBk/s1600-h/image%5B3%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZyxEuLjHII/AAAAAAAAEqg/BVJwJHA3Q7Y/image_thumb%5B1%5D.gif?imgmax=800" width="580" height="174" /&gt;&lt;/a&gt;                 &lt;a href="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZyxFCInkkI/AAAAAAAAEqk/15-mEl5Cc4I/s1600-h/image%5B7%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZyxFxW_E2I/AAAAAAAAEqo/sUDZ7_l5MKs/image_thumb%5B3%5D.gif?imgmax=800" width="432" height="161" /&gt;&lt;/a&gt;&lt;a href="http://lh5.ggpht.com/_Pg0RCzJJYC4/TZyxGqYCVMI/AAAAAAAAEqs/yAxLv7vQmk8/s1600-h/image%5B11%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_Pg0RCzJJYC4/TZyxHLkQfaI/AAAAAAAAEqw/eznKc-3D9nE/image_thumb%5B5%5D.gif?imgmax=800" width="316" height="170" /&gt;&lt;/a&gt;  &lt;p align="justify"&gt;In each of these examples, notice how the Command pattern decouples the object that invokes the operation from the one having the knowledge to perform it. This gives us a lot of flexibility in designing our user interface. An application can provide both a menu and a push button interface to a feature just by making the menu and the push button share an instance of the same concrete Command subclass. We can replace commands dynamically, which would be useful for implementing context-sensitive menus. We can also support command scripting by composing commands into larger ones. All of this is possible because the object that issues a request only needs to know how to issue it; it doesn’t need to know how the request will be carried out.&lt;/p&gt;      &lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Command decouples the object that invokes the operation from the one that knows how to perform it. To achieve this separation, the designer creates an abstract base class that maps a receiver (an object) with an action (a pointer to a member function). The base class contains an execute() method that simply calls the action on the receiver. &lt;/li&gt;    &lt;li&gt;All clients of Command objects treat each object as a &amp;quot;black box&amp;quot; by simply invoking the object's virtual execute() method whenever the client requires the object's &amp;quot;service&amp;quot;. &lt;/li&gt;    &lt;li&gt;Sequences of Command objects can be assembled into composite (or macro) commands.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size="3"&gt;Applicability&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Use the Command pattern when you want to&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;parameterize objects by an action to perform, as MenuItem.&lt;/li&gt;    &lt;li&gt;You can express such parameterization in a procedural language with a callback function, that is, a function that’s registered somewhere to be called at a later point. Commands are an object Oriented replacement for callbacks.&lt;/li&gt;    &lt;li&gt;Specify, queue, and execute requests at different times. A Command object can have a lifetime independent of the original request. If the receiver of a request can be represented in an address space-independent way, then you can transfer a command object for the request to a different process and fulfill the request there.&lt;/li&gt;    &lt;li&gt;Supports Undo&lt;/li&gt;    &lt;li&gt;Support logging changes to that they can be reapplied in case of a system crash. - Recovering form a crash involves reloading logged commands form disk and re-executing them with the execute operation.&lt;/li&gt;    &lt;li&gt;Structure a system around high-level operations built on primitives operations. Such structure is common in information system that support transactions.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Structure&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_Pg0RCzJJYC4/TZyxHj6GFyI/AAAAAAAAEq0/KvXkp6LJAC0/s1600-h/image%5B16%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZyxIKV79uI/AAAAAAAAEq4/12zgbJibaCI/image_thumb%5B8%5D.gif?imgmax=800" width="575" height="336" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Collaborations&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The client creates a ConcreteCommand object and specifies its receiver.&lt;/li&gt;    &lt;li&gt;An Invoker object stores the ConcreteCommand object.&lt;/li&gt;    &lt;li&gt;The invoker issues a request by calling Execute on the command. When commands are undo-able, ConcreteCommand stores state for undoing the com-mand prior to invoking Execute.&lt;/li&gt;    &lt;li&gt;The ConcreteCommand object invokes operations on its receiver to carry out the request.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The following diagram shows the interactions between these objects. It illustrates how Command decouples the invoker form the receiver ( and the request it carries out ).&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZyxJBFuiBI/AAAAAAAAEq8/AQ_1iwW9i1A/s1600-h/image%5B21%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZyxJius2NI/AAAAAAAAErA/Q1Ak0WacQck/image_thumb%5B11%5D.gif?imgmax=800" width="529" height="302" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Consequences&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The Command pattern has the following consequences:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Command decouples the object that invokes the operation from the one that knows how to perform it.&lt;/li&gt;    &lt;li&gt;Commands are first-class objects. They can be manipulated and extended like any other object.&lt;/li&gt;    &lt;li&gt;You can assemble commands into a composite command. An example is the Macro Command class described earlier. In general, composite commands are an instance of the Composite pattern.&lt;/li&gt;    &lt;li&gt;It’s easy to add new Commands, because you don’t have to change existing classes&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;Model:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZyxKkilYqI/AAAAAAAAErE/PrqOGokbI6s/s1600-h/image%5B26%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZyxLUVww7I/AAAAAAAAErI/wpVjT-h6374/image_thumb%5B14%5D.gif?imgmax=800" width="572" height="369" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-1089387346121880650?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/1089387346121880650/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=1089387346121880650' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/1089387346121880650'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/1089387346121880650'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/04/design-pattern-command.html' title='Design Pattern : Command'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/_Pg0RCzJJYC4/TZyxEuLjHII/AAAAAAAAEqg/BVJwJHA3Q7Y/s72-c/image_thumb%5B1%5D.gif?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-380045012690933857</id><published>2011-04-05T09:55:00.001-07:00</published><updated>2011-07-11T12:02:04.822-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Pattern'/><title type='text'>Design Pattern: Adapter</title><content type='html'>&lt;p&gt;It falls into structural pattern category. The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_Pg0RCzJJYC4/TZtJXAYRs8I/AAAAAAAAEp8/JNIu4PWhZCA/s1600-h/image%5B2%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZtJXuRUj1I/AAAAAAAAEqA/hX4MS-KmD_o/image_thumb.png?imgmax=800" width="244" height="106" /&gt;&lt;/a&gt;&lt;a href="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZtJYYdDlXI/AAAAAAAAEqE/46m1Crk8Ttc/s1600-h/image%5B7%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZtJYzkQ_6I/AAAAAAAAEqI/82a9qJ5TRic/image_thumb%5B3%5D.png?imgmax=800" width="244" height="108" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZtJZRBzhxI/AAAAAAAAEqM/h0Z4PktV2ig/s1600-h/image%5B12%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZtJZ5B7e9I/AAAAAAAAEqQ/2M-SwTgP9Aw/image_thumb%5B6%5D.png?imgmax=800" width="272" height="120" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;•Use the Adapter pattern when&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;You want to use an existing class, and its interface does not match the one you need.&lt;/li&gt;    &lt;li&gt;You want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don’t necessarily have compatible interfaces.&lt;/li&gt;    &lt;li&gt;You need to use several existing subclasses, but it’s impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Structure&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_Pg0RCzJJYC4/TZtJac-UafI/AAAAAAAAEqU/7v0VFbM__Uw/s1600-h/image%5B6%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_Pg0RCzJJYC4/TZtJbAeAteI/AAAAAAAAEqY/xPZCbPLW2gs/image_thumb%5B2%5D.gif?imgmax=800" width="543" height="354" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Collaborations&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Clients call operations on an Adapter instance. In turn, the adapter calls Adaptee operations that carry out the request.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Consequences:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Class and object adapters have different trade-offs.&lt;/p&gt;  &lt;p&gt;A: &lt;u&gt;class adapter:&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;1. Adapts Adapter to target by committing to a concrete Adapter class. As a consequence, a class adapter won’t work when we want to adapt a class and all its subclasses.&lt;/p&gt;  &lt;p&gt;2. Lets-adapter override some of Adaptee’s behavior, since Adapter is a subclass of Adaptee.&lt;/p&gt;  &lt;p&gt;3. Introduces only one object, and no additional pointer indirection is needed to get to the Adaptee&lt;/p&gt;  &lt;p&gt;B: &lt;u&gt;Object adapter:&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;1.Lets a single adapter work with many Adaptees-that is, the Adaptee itself and all of its subclasses (if any). The Adapter can also add functionality to all Adaptees at once.&lt;/p&gt;  &lt;p&gt;2. Makes it harder to overide Adaptee behavior. It will require subclassing Adaptee and making Adapter refer to the subclass rather than the Adapter itself.&lt;/p&gt;  &lt;p&gt;Issues to be considered while using adapter pattern:&lt;/p&gt;  &lt;p&gt;1. How much adapting does Adapter do?&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Pluggable adapters.&lt;/li&gt;    &lt;li&gt;Using two-way adapters to provide transparency&lt;/li&gt; &lt;/ul&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-380045012690933857?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/380045012690933857/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=380045012690933857' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/380045012690933857'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/380045012690933857'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/04/design-pattern-adapter.html' title='Design Pattern: Adapter'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/_Pg0RCzJJYC4/TZtJXuRUj1I/AAAAAAAAEqA/hX4MS-KmD_o/s72-c/image_thumb.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-7368986971171527500</id><published>2011-04-05T08:56:00.001-07:00</published><updated>2011-07-11T12:02:05.044-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Pattern'/><title type='text'>Design Pattern : Facade</title><content type='html'>&lt;p&gt;It falls into structural pattern categories. The &lt;i&gt;Facade Pattern &lt;/i&gt;defines a higher level interface to a subsystem, that makes it easier to use. It provides unified interface to a set of interfaces in a subsystem. &lt;/p&gt;  &lt;p&gt;Use the Facade pattern when&lt;/p&gt;  &lt;p&gt;•You want to provide a simple interface to a complex subsystem.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;To tackle the complexity of subsystem as they evolve&lt;/p&gt;    &lt;p&gt;Most patterns, when applied, result in more and smaller classes. This makes the subsystem more reusable and easier to customize, but it also becomes harder to use for clients that don’t need to customize it.&lt;/p&gt;    &lt;p&gt;A façade can provide a simple default view of the subsystem that is good enough for most of the clients. Only clients needing more customizability will need to look beyond the façade&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;•Facade to decouple the subsystem for clients and other subsystems, thereby promoting subsystem independence and portability which tackle the disadvantage of dependencies between clients and the implementation classes on an abstraction.&lt;/p&gt;  &lt;p&gt;•To layer your subsystem. Use façade to define an entry point to each subsystem level. If subsystems are dependent, then you can simplify the dependencies between them by making them communicate with each other solely through their facades.&lt;/p&gt;  &lt;p&gt;The structure of the Façade Pattern looks as below&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_Pg0RCzJJYC4/TZs7ugJrlkI/AAAAAAAAEpk/MTUd-9ASozM/s1600-h/Picture3%5B3%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Picture3" border="0" alt="Picture3" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZs7vSIRzgI/AAAAAAAAEpo/Ej3rzv_wELc/Picture3_thumb%5B1%5D.png?imgmax=800" width="524" height="305" /&gt;&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;•Clients communicate with the subsystem by sending request to Façade, which forwards them to the appropriate subsystem object(s). Although the subsystem objects perform the actual work, the façade may have to do work of its own to translate its interface to subsystem interfaces.&lt;/p&gt;  &lt;p&gt;•Clients that user the façade don’t have to access its subsystem objects directly.&lt;/p&gt;  &lt;p&gt;The Facade Patterns offers the following benefits:&lt;/p&gt;  &lt;p&gt;•1. It shields client from subsystem components, thereby reducing the number of objects that clients deal with and making the subsystem easier to use.&lt;/p&gt;  &lt;p&gt;•It promotes weak coupling between the subsystem and its clients. &lt;/p&gt;  &lt;p&gt;•It doesn’t prevent applications form using subsystem classes if they need to. Thus you can choose between ease of use and generality&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Illustration: Problem&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Consider for example a programming environment that gives application access to its compiler subsystem. This subsystem contains classes such as Scanner, Parser,ProgramNode, ByteCodeStream, and ProgramNodeBuilder that implements the Compiler. Some Specialized applications might need to access these classes directly. &lt;/p&gt;  &lt;p&gt;But most clients of a compiler generally don’t care about details like parsing and byte code generation; they merely want to complier subsysem only complicate their task.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Illustration : solution&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZs7wOSOE-I/AAAAAAAAEps/sjaRNzo4t3o/s1600-h/Picture4%5B4%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Picture4" border="0" alt="Picture4" src="http://lh3.ggpht.com/_Pg0RCzJJYC4/TZs7wk6t6PI/AAAAAAAAEpw/I01_IQNzmiU/Picture4_thumb%5B2%5D.png?imgmax=800" width="558" height="143" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Compiler class in the above can act as a facade, which will provide higher-level interface that can shield clients form these classes, the compiler subsystem also includes a Compiler class. This class defines a unified interface to the compiler’s functionality. &lt;/p&gt;  &lt;p&gt;It offers client a single, simple interface to the compiler subsystem. It glues together the classes that implement compiler functionality without hiding them completely.&lt;/p&gt;  &lt;p&gt;[Compiler façade makes easier for most programmers without hiding the lower-level functionality form the few that need it]&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Class Diagram Solution:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_Pg0RCzJJYC4/TZs7xcbZTMI/AAAAAAAAEp0/qy-XCDNmSdU/s1600-h/image%5B4%5D.gif"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_Pg0RCzJJYC4/TZs7x1CSdvI/AAAAAAAAEp4/2K3oM2t-7y4/image_thumb%5B2%5D.gif?imgmax=800" width="560" height="329" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-7368986971171527500?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/7368986971171527500/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=7368986971171527500' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/7368986971171527500'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/7368986971171527500'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/04/design-pattern-facade.html' title='Design Pattern : Facade'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/_Pg0RCzJJYC4/TZs7vSIRzgI/AAAAAAAAEpo/Ej3rzv_wELc/s72-c/Picture3_thumb%5B1%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-8420793894408322507</id><published>2011-03-30T11:36:00.001-07:00</published><updated>2011-03-30T11:36:25.279-07:00</updated><title type='text'>Database Input Issues</title><content type='html'>&lt;p&gt;This was a sample presentation which I created at my workplace to present Database security issues. Most of the content is taken from the book Writing Secure code. Hope it is helpful to someone.&lt;/p&gt;  &lt;div style="text-align: left; width: 425px"&gt;&lt;object style='margin:0px' width='425' height='355'&gt;&lt;param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=databasesecurity-12814347393668-phpapp02&amp;amp;stripped_title=database-security-4933717" /&gt;&lt;param name="allowFullScreen" value="true" /&gt;&lt;param name="allowScriptAccess" value="always" /&gt;&lt;embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=databasesecurity-12814347393668-phpapp02&amp;amp;stripped_title=database-security-4933717" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-8420793894408322507?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/8420793894408322507/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=8420793894408322507' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/8420793894408322507'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/8420793894408322507'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/database-input-issues.html' title='Database Input Issues'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-3387970809030370486</id><published>2011-03-30T10:48:00.001-07:00</published><updated>2011-07-11T12:07:19.153-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview '/><title type='text'>Number to word converter</title><content type='html'>&lt;p&gt;Q: Take a number and give the equivalent number in British English words e.g.&lt;/p&gt;  &lt;p&gt;1 = one&lt;/p&gt;  &lt;p&gt;21 = twenty one&lt;/p&gt;  &lt;p&gt;105 = one hundred and five&lt;/p&gt;  &lt;p&gt;56945781 = fifty six million nine hundred and forty five thousand seven hundred and eighty one&lt;/p&gt;  &lt;p&gt;etc.. up to 999,999,999 without using a tokenizer, or any external libraries&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Working Code: &lt;/strong&gt;&lt;/p&gt;   &lt;code style="padding-bottom: 2px; padding-left: 2px; padding-right: 2px; display: block; background: rgb(38,41,46); color: white; font-size: 12px; padding-top: 2px; -moz-background-clip: border; -moz-background-inline-policy: continuous; -moz-background-origin: padding"&gt; &lt;p&gt;import java.text.DecimalFormat;&lt;/p&gt;  &lt;p&gt;public class NumberToWord {&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; private static final String[] tensWords = { &amp;quot;&amp;quot;, &amp;quot; ten&amp;quot;, &amp;quot; twenty&amp;quot;,   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot; thirty&amp;quot;, &amp;quot; forty&amp;quot;, &amp;quot; fifty&amp;quot;, &amp;quot; sixty&amp;quot;, &amp;quot; seventy&amp;quot;, &amp;quot; eighty&amp;quot;,    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot; ninety&amp;quot; };&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; private static final String[] words = { &amp;quot;&amp;quot;, &amp;quot; one&amp;quot;, &amp;quot; two&amp;quot;, &amp;quot; three&amp;quot;,   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot; four&amp;quot;, &amp;quot; five&amp;quot;, &amp;quot; six&amp;quot;, &amp;quot; seven&amp;quot;, &amp;quot; eight&amp;quot;, &amp;quot; nine&amp;quot;, &amp;quot; ten&amp;quot;,    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot; eleven&amp;quot;, &amp;quot; twelve&amp;quot;, &amp;quot; thirteen&amp;quot;, &amp;quot; fourteen&amp;quot;, &amp;quot; fifteen&amp;quot;,    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot; sixteen&amp;quot;, &amp;quot; seventeen&amp;quot;, &amp;quot; eighteen&amp;quot;, &amp;quot; nineteen&amp;quot; };&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; // Convert Number to word which is less than one thousand&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; private static String convertLessThanOneThousand(int number) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String thousandWord;&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (number % 100 &amp;lt; 20) {   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; thousandWord = words[number % 100];    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; number /= 100;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } else {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; thousandWord = words[number % 10];    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; number /= 10;&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; thousandWord = tensWords[number % 10] + thousandWord;   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; number /= 10;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (number == 0)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return thousandWord;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return words[number] + &amp;quot; hundred&amp;quot; + thousandWord;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Converts any number to word between 0 to 999 999 999 999 billions millions    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public static String convertNumberToWord(long number) throws Exception{&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(number &amp;lt; 0)   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new Exception(&amp;quot;Negative numbers not supported&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (number == 0) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return &amp;quot;zero&amp;quot;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String snumber = Long.toString(number);   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(snumber.length() &amp;gt; 13)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new Exception(&amp;quot;Numbers greater than 999 billion are not supported&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // pad with &amp;quot;0&amp;quot;   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String mask = &amp;quot;000000000000&amp;quot;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // DecimalFormat converts 50 to 000000000050 based on the given mask    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; DecimalFormat df = new DecimalFormat(mask);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; snumber = df.format(number);&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; StringBuilder result = new StringBuilder();&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // XXXnnnnnnnnn   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int billions = Integer.parseInt(snumber.substring(0, 3));    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // nnnXXXnnnnnn    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int millions = Integer.parseInt(snumber.substring(3, 6));    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // nnnnnnXXXnnn    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int hundredThousands = Integer.parseInt(snumber.substring(6, 9));    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // nnnnnnnnnXXX    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int thousands = Integer.parseInt(snumber.substring(9, 12));&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String wordBillions;   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; switch (billions) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; case 0:    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; wordBillions = &amp;quot;&amp;quot;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; break;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; case 1:    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; wordBillions = convertLessThanOneThousand(billions) + &amp;quot; billion&amp;quot;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; break;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; default:    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; wordBillions = convertLessThanOneThousand(billions) + &amp;quot; billion&amp;quot;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; result.append(wordBillions);&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String wordMillions;   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; switch (millions) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; case 0:    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; wordMillions = &amp;quot;&amp;quot;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; break;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; case 1:    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; wordMillions = convertLessThanOneThousand(millions) + &amp;quot; million&amp;quot;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; break;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; default:    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; wordMillions = convertLessThanOneThousand(millions) + &amp;quot; million&amp;quot;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; result.append(wordMillions);&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String wordHundredThousands;   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; switch (hundredThousands) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; case 0:    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; wordHundredThousands = &amp;quot;&amp;quot;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; break;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; case 1:    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; wordHundredThousands = &amp;quot;one thousand &amp;quot;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; break;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; default:    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; wordHundredThousands = convertLessThanOneThousand(hundredThousands)+ &amp;quot; thousand&amp;quot;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; result.append(wordHundredThousands);&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; result.append(convertLessThanOneThousand(thousands));&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return result.toString();   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public static void main(String[] args) {   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String word = &amp;quot;&amp;quot;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; try {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; word = NumberToWord.convertNumberToWord(999999999999L);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } catch (Exception e) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(&amp;quot;error: &amp;quot;+e.getMessage());    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(word);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;}   &lt;/p&gt;&lt;/code&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-3387970809030370486?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/3387970809030370486/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=3387970809030370486' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3387970809030370486'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3387970809030370486'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/number-to-word-converter.html' title='Number to word converter'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-4094991474400202772</id><published>2011-03-27T01:31:00.001-07:00</published><updated>2011-09-26T08:30:46.206-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Serializing a binary tree</title><content type='html'>&lt;p&gt;&lt;strong&gt;Q: Design an algorithm and write code to serialize and deserialize a binary tree.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Advantages of serialization/deserialization include storing a binary into and restoring it for a later point of time, transmitting the tree over a network from one computer and load it in another computer.&lt;/p&gt;  &lt;p&gt;One approach for this problem could be the nodes with NULL values using some kind of sentinel so that later nodes can be inserted In correct place while deserialization. Lets use the sentinel as # and we will use preorder traversal here serialization. &lt;/p&gt;  &lt;p&gt;Assume we have a binary tree below: &lt;/p&gt;  &lt;pre&gt;&lt;font face="Arial"&gt;The preorder traversal of the above tree can be written as &lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;30 10 50 # # # 20 45 # # 35 # #&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:2d55ca63-ceff-4a37-bddc-96e95af55b4e" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 575px; height: 518px;" src="http://lh5.ggpht.com/-GdXkSqHreNA/ToCapPL4uqI/AAAAAAAAE-Y/Rqp65atbLnU/transformedimage%25255B65%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-4094991474400202772?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/4094991474400202772/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=4094991474400202772' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4094991474400202772'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4094991474400202772'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/serializing-binary-tree.html' title='Serializing a binary tree'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/-GdXkSqHreNA/ToCapPL4uqI/AAAAAAAAE-Y/Rqp65atbLnU/s72-c/transformedimage%25255B65%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-8335231841488283029</id><published>2011-03-23T08:51:00.001-07:00</published><updated>2011-09-26T08:34:16.025-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Finding Unique number</title><content type='html'>&lt;p&gt;&lt;strong&gt;Q: You are given an array that contains integers. The integers content is such that every integer occurs 3 times in that array leaving one integer that appears only once.      &lt;br /&gt;Fastest way to find that single integer&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;eg:    &lt;br /&gt;Input: {1,1,1,3,3,3,20,4,4,4}; &lt;/p&gt;  &lt;p&gt;Answer: 20&lt;/p&gt;  &lt;p&gt;The logic is similar to finding the element which appears once in an array - containing other elements each appearing twice. There we do XOR all the elements and the result will be the unique number.&lt;/p&gt;  &lt;p&gt;To apply the similar logic to this problem we maintain 2 variables &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;ones &lt;/strong&gt;- This variable holds XOR of all the elements which have appeared only once.     &lt;br /&gt;&lt;strong&gt;twos&lt;/strong&gt; - This variable holds XOR of all the elements which have appeared twice.&lt;/p&gt;  &lt;p&gt;The algorithm goes as follows…&lt;/p&gt;  &lt;p&gt;1. A new number appears - It gets XOR' ed to the variable &amp;quot;&lt;strong&gt;ones&lt;/strong&gt;&amp;quot;.     &lt;br /&gt;2. A number gets repeated(appears twice) - It is removed from &amp;quot;&lt;strong&gt;ones&lt;/strong&gt;&amp;quot; and XOR'd to the     &lt;br /&gt;variable &amp;quot;&lt;strong&gt;twos&lt;/strong&gt;&amp;quot;.     &lt;br /&gt;3. A number appears for the third time - It gets removed from both &amp;quot;&lt;strong&gt;ones&lt;/strong&gt;&amp;quot; and &amp;quot;&lt;strong&gt;twos&lt;/strong&gt;&amp;quot;.&lt;/p&gt;  &lt;p&gt;The final answer will be in &amp;quot;&lt;strong&gt;ones&lt;/strong&gt;&amp;quot; as it holds the unique element.&lt;/p&gt;  &lt;p&gt;Working Java code:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:dd15477d-8667-4ea6-8f5e-afc412273207" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 571px; height: 590px;" src="http://lh3.ggpht.com/-4fWzECEWPVw/ToCbdqYoG7I/AAAAAAAAE-c/dKtRSX3AErg/transformedimage%25255B20%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-8335231841488283029?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/8335231841488283029/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=8335231841488283029' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/8335231841488283029'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/8335231841488283029'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/finding-unique-number.html' title='Finding Unique number'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/-4fWzECEWPVw/ToCbdqYoG7I/AAAAAAAAE-c/dKtRSX3AErg/s72-c/transformedimage%25255B20%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-8331597925622834158</id><published>2011-03-22T08:47:00.001-07:00</published><updated>2011-09-26T08:36:09.424-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Multiplication of numbers</title><content type='html'>&lt;p&gt;&lt;strong&gt;Q: There is an array A[N] of N numbers. You have to compose an array Output[N] such that Output[i] will be equal to multiplication of all the elements of A[N] except A[i]. For example Output[0] will be multiplication of A[1] to A[N-1] and Output[1] will be multiplication of A[0] and from A[2] to A[N-1]. Solve it without division operator and in O(n). &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;A: {4, 3, 2, 1, 2}    &lt;br /&gt;OUTPUT: {12, 16, 24, 48, 24}&lt;/p&gt;  &lt;p&gt;Since we should not use, division operator, Brute force approach would be O(n^2)&lt;/p&gt;  &lt;p&gt;Other approach is by storing the result temporarily in an array.&lt;/p&gt;  &lt;p&gt;Let’s define array B where element B[i] = multiplication of numbers from A[0] to A[i]. For example, if A = {4, 3, 2, 1, 2}, then B = {4, 12, 24, 24, 48}. Then, we scan the array A from right to left, and have a temporary variable called product which stores the multiplication from right to left so far. Calculating OUTPUT[i] is straight forward, as OUTPUT[i] = B[i-1] * product.&lt;/p&gt;  &lt;p&gt;Above approach requires O( n ) time and also O( n ) space.&lt;/p&gt;  &lt;p&gt;We can actually avoid the temporary array. We can have two variables called left and right, each keeping track of the product of numbers multiplied from left-&amp;gt;right and right-&amp;gt;left. &lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:b9c238be-ae0e-4194-ad7f-4bf9387b184c" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 552px; height: 264px;" src="http://lh6.ggpht.com/-RryZRyxUFXs/ToCb6GO5cBI/AAAAAAAAE-g/-A0_M9nkCGM/transformedimage%25255B17%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-8331597925622834158?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/8331597925622834158/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=8331597925622834158' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/8331597925622834158'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/8331597925622834158'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/multiplication-of-numbers.html' title='Multiplication of numbers'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/-RryZRyxUFXs/ToCb6GO5cBI/AAAAAAAAE-g/-A0_M9nkCGM/s72-c/transformedimage%25255B17%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-5392371785317951577</id><published>2011-03-22T08:18:00.001-07:00</published><updated>2011-09-26T08:41:08.331-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Robot Unique Paths</title><content type='html'>&lt;p&gt;&lt;strong&gt;Q: A robot is located at the top-left corner of a &lt;i&gt;m&lt;/i&gt; x &lt;i&gt;n&lt;/i&gt; grid. The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid. How many possible unique paths are there?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;We can solve this problem using backtracking approach.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:c43a33df-a12b-4e58-af8f-394a20d947e6" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 555px; height: 229px;" src="http://lh4.ggpht.com/-ZhCTNb4miRk/ToCdEIenM1I/AAAAAAAAE-k/gtoNlstPxXA/transformedimage%25255B10%25255D.png?imgmax=800" /&gt;&lt;/div&gt; &lt;code style="padding-bottom: 2px; padding-left: 2px; padding-right: 2px; display: block; background: rgb(38,41,46); color: white; font-size: 12px; padding-top: 2px; -moz-background-clip: border; -moz-background-inline-policy: continuous; -moz-background-origin: padding"&gt;&lt;/code&gt;  &lt;p&gt;Other approach is using dynamic programming with memorization.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:9bcdfed8-7e5c-4d0e-b16a-81d0f4f22642" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 567px; height: 465px;" src="http://lh6.ggpht.com/-kD4EuCRrUqI/ToCdE2luAWI/AAAAAAAAE-o/Gcoes-y6vO0/transformedimage%25255B57%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-5392371785317951577?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/5392371785317951577/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=5392371785317951577' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/5392371785317951577'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/5392371785317951577'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/robot-unique-paths.html' title='Robot Unique Paths'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/-ZhCTNb4miRk/ToCdEIenM1I/AAAAAAAAE-k/gtoNlstPxXA/s72-c/transformedimage%25255B10%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-8911383456883081570</id><published>2011-03-21T09:28:00.001-07:00</published><updated>2011-09-26T08:43:14.147-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Longest Palindrome in a string</title><content type='html'>&lt;p&gt;Brute force Algorithm.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Have 2 for loops      &lt;br /&gt;for i = 1 to i less than array.length -1       &lt;br /&gt;for j=i+1 to j less than array.length &lt;/li&gt;    &lt;li&gt;This way you can get substring of every possible combination from the array &lt;/li&gt;    &lt;li&gt;Have a palindrome function which checks if a string is palindrome &lt;/li&gt;    &lt;li&gt;so for every substring (i,j) call this function, if it is a palindrome store it in a string variable &lt;/li&gt;    &lt;li&gt;If you find next palindrome substring and if it is greater than the current one, replace it with current one. &lt;/li&gt;    &lt;li&gt;Finally your string variable will have the answer &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Brute force approach for this problem takes O(n3) time.&lt;/p&gt;  &lt;p&gt;Another approach is &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Reverse the string and store it in different string &lt;/li&gt;    &lt;li&gt;Now find the largest matching substring between both the strings &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;This too will take O(n2) time&lt;/p&gt;  &lt;p&gt;We can solve this problem using suffix trees, but constructing the suffix tree itself seems to be more complex in terms of both time and space complexity.&lt;/p&gt;  &lt;p&gt;We can solve this problem using a better approach which will revolve around with a center for every single character.&lt;/p&gt;  &lt;p&gt;For each position in the string we store an upper and lower pointer that tracks the upper and lower bounds of the largest palindrome centered at that position.&lt;/p&gt;  &lt;p&gt;For instance, position 2 in the string &amp;quot;racecar&amp;quot; would start as:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_Pg0RCzJJYC4/TYd8gymO22I/AAAAAAAAEm0/4DPxWmP-wf0/s1600-h/image%5B2%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_Pg0RCzJJYC4/TYd8hfgn8TI/AAAAAAAAEm4/r7IKZX6_uvY/image_thumb.png?imgmax=800" width="244" height="140" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;At this point, the longest known palindrome at position 2 is the single character 'c'. We then increment the upper pointer and decrement the lower pointer and compare their respective values.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/TYd8h1CMnBI/AAAAAAAAEm8/VFr6pLmriYs/s1600-h/image%5B5%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TYd8iQCdHcI/AAAAAAAAEnA/LCytgdylLug/image_thumb%5B1%5D.png?imgmax=800" width="244" height="140" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Since the upper and lower values are not the same, we stop and now know that the longest palindrome centered at position 2 is 'c'. We then start the process again at the next string position, 3.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/TYd8i8dsykI/AAAAAAAAEnE/kH7ajTe9SI0/s1600-h/image%5B8%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TYd8jpUwPSI/AAAAAAAAEnI/1A3EYvaLM4U/image_thumb%5B2%5D.png?imgmax=800" width="244" height="140" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;We increment and decrement the upper and lower pointers and see that they are equal. Our current longest palindrome centered at position 3 is now 'cec'.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_Pg0RCzJJYC4/TYd8kDgImpI/AAAAAAAAEnM/6oR6GeXdJrA/s1600-h/image%5B11%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_Pg0RCzJJYC4/TYd8kjMrmII/AAAAAAAAEnQ/OhwZLxBuSPQ/image_thumb%5B3%5D.png?imgmax=800" width="244" height="140" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Since they were equal, we increment and decrement the pointers again and see that they are still equal. Our new longest palindrome centered at position 3 is now 'aceca'.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_Pg0RCzJJYC4/TYd8lOcB8aI/AAAAAAAAEnU/to-Knx0ZLyI/s1600-h/image%5B14%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_Pg0RCzJJYC4/TYd8mbvoy8I/AAAAAAAAEnY/_VkRAt0jeao/image_thumb%5B4%5D.png?imgmax=800" width="244" height="140" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;We continue incrementing and decrementing the upper and lower pointers until they don't match or they hit one of the ends of the string.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_Pg0RCzJJYC4/TYd8pMFZNrI/AAAAAAAAEnc/YwluVzuI8Lg/s1600-h/image%5B17%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_Pg0RCzJJYC4/TYd8pttZbNI/AAAAAAAAEng/-m-irXzes3M/image_thumb%5B5%5D.png?imgmax=800" width="244" height="135" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Since our pointers are equal, we increment and decrement them again. We hit an end of the string and the pointers are equal so we know that our longest palindrome centered at position 3 is 'racecar'.&lt;/p&gt;  &lt;p&gt;We're done with position 3 and continue this same process again at position 4. We'll do this until we've covered every position in the string.&lt;/p&gt;  &lt;p&gt;NOTE: We initially assumed that the palindrome had an odd length. This algorithm works for even palindromes as well. The only change we make is that when we initialize the pointers, instead of having the lower and upper pointer point to the same position we have the upper pointer point at the next position. Everything else with the algorithm remains the same. i.e. The initial pointers for an even palindrome 'centered' at position 3 would look like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_Pg0RCzJJYC4/TYd8qPgsCyI/AAAAAAAAEnk/KSq06DAOoTA/s1600-h/image%5B20%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_Pg0RCzJJYC4/TYd8r2KyYiI/AAAAAAAAEno/MLvXu0bqeNI/image_thumb%5B6%5D.png?imgmax=800" width="244" height="140" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Runtime:&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Worst-Case: O(N^2)&lt;/p&gt;    &lt;p&gt;Best-Case: O(N)&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The worst case is achieved when the string is a single repeated character because every substring is a palindrome.&lt;/p&gt;  &lt;p&gt;The best case is achieved when the string contains no palindromes.&lt;/p&gt;  &lt;p&gt;Typically if a string only contains a single palindrome (the fewer the better), the closer to O(N) it will run. This is because every time it checks a position in the string, it checks the character before and after that position, and if they don't match then it stops looking for the palindrome. Positions in the string can be discarded after only one lookup if that position doesn't have a palindrome, so if there are no palindromes you only do N comparisons.&lt;/p&gt;  &lt;p&gt;JavaCode:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:b046ca81-8bcb-4c25-9214-9b64856a6671" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 575px; height: 646px;" src="http://lh5.ggpht.com/-Xh4z3pN79fE/ToCdkPK08HI/AAAAAAAAE-s/F3eoED33SXo/transformedimage%25255B15%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-8911383456883081570?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/8911383456883081570/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=8911383456883081570' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/8911383456883081570'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/8911383456883081570'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/longest-palindrome-in-string.html' title='Longest Palindrome in a string'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_Pg0RCzJJYC4/TYd8hfgn8TI/AAAAAAAAEm4/r7IKZX6_uvY/s72-c/image_thumb.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-3985986948135040619</id><published>2011-03-21T09:00:00.001-07:00</published><updated>2011-09-26T08:45:04.147-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>3 sum and 2 sum problem</title><content type='html'>&lt;p&gt;Q: Given an integer x and an unsorted array of integers, describe an algorithm to determine whether two of the numbers add up to x.&lt;/p&gt;  &lt;p&gt;If we have hash table, we can just check for every number ‘a’, if x-‘b’ is present in hash table. This takes O( n ) with constant space for hashtable. What if the interviewer hates the hashtable.&lt;/p&gt;  &lt;p&gt;Sort the array. Then, keep track of two pointers in the array, one at the beginning and one at the end. Whenever the sum of the current two integers is less than x, move the first pointer forwards, and whenever the sum is greater than x, move the second pointer backwards. This solution takes O(n log n) time because we sort the numbers.&lt;/p&gt;  &lt;p&gt;Another approach for this problem could be…&lt;/p&gt;  &lt;p&gt;Create a binary search tree containing x minus each element in the array. Then, check whether any element of the array appears in the BST. It takes O(n log n) time to create a binary search tree from an array, since it takes O(log n) time to insert something into a BST, and it takes O(n log n) time to see if any element in an array is in a BST, since the lookup time for each element in the array takes O(log n). Therefore step one takes O(n log n) time and step two takes O(n log n) time, so our total running time is O(n log n).&lt;/p&gt;  &lt;p&gt;Variations for this problem would be, find three integers a,b,c in the array such that a+b=c&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:6464fcb0-76f1-41ac-90a8-72f4e897c6e0" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 567px; height: 558px;" src="http://lh4.ggpht.com/-N93kzZ9WPj8/ToCd_2zcjeI/AAAAAAAAE-w/uost8c3O9N0/transformedimage%25255B10%25255D.png?imgmax=800" /&gt;&lt;/div&gt; &lt;code style="padding-bottom: 2px; padding-left: 2px; padding-right: 2px; display: block; background: rgb(38,41,46); color: white; font-size: 12px; padding-top: 2px; -moz-background-clip: border; -moz-background-inline-policy: continuous; -moz-background-origin: padding"&gt;&lt;/code&gt;  &lt;p&gt;This will&amp;#160; take O(n&lt;sup&gt;2&lt;/sup&gt;) time complexity.&lt;/p&gt;  &lt;p&gt;Other variation is Find three integers a,b,c in the array such that a+b+c =0&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-3985986948135040619?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/3985986948135040619/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=3985986948135040619' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3985986948135040619'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3985986948135040619'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/3-sum-and-2-sum-problem.html' title='3 sum and 2 sum problem'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/-N93kzZ9WPj8/ToCd_2zcjeI/AAAAAAAAE-w/uost8c3O9N0/s72-c/transformedimage%25255B10%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-144791996608220941</id><published>2011-03-20T10:57:00.001-07:00</published><updated>2011-07-11T12:07:19.017-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview '/><title type='text'>Big File containing billions of numbers</title><content type='html'>&lt;p&gt;Q: There is big file containing billions of numbers. How do we find maximum 10 numbers from those files?&lt;/p&gt;  &lt;p&gt;Priority queue is the best data structure to answer such question. Of course loading all the data and building the heap is not such a good idea. Main challenge is how do we manage chunks of data in memory and arrive at the solution.&lt;/p&gt;  &lt;p&gt;Divide the data into some sets of 1000 elements, make a heap of them and take 10 element from each heap one by one. Finally sort all the sets of 10 elements and take top 10 among those. But where do we store 10 element from each heap which may itself require large amount of memory as we have billions of numbers.&lt;/p&gt;  &lt;p&gt;Reuse top 10 elements from earlier heap in subsequent elements can solve this problem. Take first block of 1000 elements and subsequent blocks of 990 elements. The last set of 990 elements or less than that and taking top 10 elements from the final heap will fetch you the answer.&lt;/p&gt;  &lt;p&gt;Time Complexity would be O( (n/1000)*(complexity of heapsort of 1000 elements) = O( n )&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-144791996608220941?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/144791996608220941/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=144791996608220941' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/144791996608220941'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/144791996608220941'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/big-file-containing-billions-of-numbers.html' title='Big File containing billions of numbers'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-3181596221575244127</id><published>2011-03-18T23:21:00.001-07:00</published><updated>2011-09-26T08:48:22.334-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Median of two sorted arrays</title><content type='html'>&lt;p&gt;This question is similar to the previous question which can solved in O(logn) using binary search approach. Lets see the algorithm….&lt;/p&gt;  &lt;p&gt;Algorithm    &lt;br /&gt;1.Let the two arrays be a1,a2 with n elements each with n elements     &lt;br /&gt;2.Let med1 and med 2 be the medians of two arrays respcetively     &lt;br /&gt;3.if med1==med2 means the required median is med1(or med2)     &lt;br /&gt;4.if med1 &amp;lt; med2 that means the median can either lie in upper half of a1 or lower half&amp;#160; of a2&amp;#160;&amp;#160;&amp;#160;&amp;#160; 5.if med1 &amp;gt;med2 that means the median can be in lower half of of a1 or upper half of a2     &lt;br /&gt;we continue this process until two elements are present in the array     &lt;br /&gt;if two elements are present in the array then we calculate the median as follows     &lt;br /&gt;median_required = (max(a1[0],a2[0])+min(a1[1]+a2[1])) /2&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:0198e636-9505-4955-b879-da86beb71006" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 560px; height: 476px;" src="http://lh6.ggpht.com/-mHOzGyJePf4/ToCexVF0ZFI/AAAAAAAAE-0/1N-EhxaGe-Y/transformedimage%25255B44%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-3181596221575244127?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/3181596221575244127/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=3181596221575244127' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3181596221575244127'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3181596221575244127'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/median-of-two-sorted-arrays.html' title='Median of two sorted arrays'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/-mHOzGyJePf4/ToCexVF0ZFI/AAAAAAAAE-0/1N-EhxaGe-Y/s72-c/transformedimage%25255B44%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-2342820447979363306</id><published>2011-03-18T10:29:00.001-07:00</published><updated>2011-09-26T08:50:49.323-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Kth Smallest element</title><content type='html'>&lt;p&gt;Q: Given two sorted arrays A, B of size &lt;i&gt;m&lt;/i&gt; and &lt;i&gt;n&lt;/i&gt; respectively. Find the k-th smallest element in the union of A and B. You can assume that there are no duplicate elements.&lt;/p&gt;  &lt;p&gt;One approach we could think of is merge both arrays and the k-th smallest element could be accessed directly. Merging would require extra space of &lt;i&gt;O&lt;/i&gt;(&lt;i&gt;m&lt;/i&gt;+&lt;i&gt;n&lt;/i&gt;). The linear run time is pretty good, but could we improve it even further?&lt;/p&gt;  &lt;p&gt;Another approach is using two pointers, you can traverse both arrays without actually merging them, thus without the extra space. Both pointers are initialized to point to head of A and B respectively, and the pointer that has the larger of the two is incremented one step. The k-th smallest is obtained by traversing a total of &lt;i&gt;k&lt;/i&gt; steps.&lt;/p&gt;  &lt;p&gt;We can solve this problem in logarithmic time in &lt;strong&gt;&lt;i&gt;O&lt;/i&gt;(lg &lt;i&gt;m&lt;/i&gt; + lg &lt;i&gt;n&lt;/i&gt;)&lt;/strong&gt; using binary search approach&lt;strong&gt;.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;We try to approach this tricky problem by comparing middle elements of A and B, which we identify as A&lt;sub&gt;i&lt;/sub&gt; and B&lt;sub&gt;j&lt;/sub&gt;. If A&lt;sub&gt;i&lt;/sub&gt; is between B&lt;sub&gt;j&lt;/sub&gt; and B&lt;sub&gt;j-1&lt;/sub&gt;, we have just found the &lt;i&gt;i&lt;/i&gt;+&lt;i&gt;j&lt;/i&gt;+1 smallest element. Therefore, if we choose &lt;i&gt;i&lt;/i&gt; and &lt;i&gt;j&lt;/i&gt; such that&lt;i&gt; i&lt;/i&gt;+&lt;i&gt;j&lt;/i&gt; = &lt;i&gt;k&lt;/i&gt;-1, we are able to find the k-th smallest element. This is an important invariant that we must maintain for the correctness of this algorithm.&lt;/p&gt;  &lt;p&gt;Maintaining the invariant    &lt;br /&gt;&lt;i&gt;&amp;#160;&amp;#160;&amp;#160; i &lt;/i&gt;+ &lt;i&gt;j&lt;/i&gt; = &lt;i&gt;k&lt;/i&gt; – 1, &lt;/p&gt;  &lt;p&gt;If B&lt;sub&gt;j-1&lt;/sub&gt; &amp;lt; A&lt;sub&gt;i&lt;/sub&gt; &amp;lt; B&lt;sub&gt;j&lt;/sub&gt;, then A&lt;sub&gt;i&lt;/sub&gt; must be the k-th smallest,     &lt;br /&gt;or else if A&lt;sub&gt;i-1&lt;/sub&gt; &amp;lt; B&lt;sub&gt;j&lt;/sub&gt; &amp;lt; A&lt;sub&gt;i&lt;/sub&gt;, then B&lt;sub&gt;j&lt;/sub&gt; must be the k-th smallest.&lt;/p&gt;  &lt;p&gt;We can make an observation that when A&lt;sub&gt;i&lt;/sub&gt; &amp;lt; B&lt;sub&gt;j&lt;/sub&gt;, then it must be true that A&lt;sub&gt;i&lt;/sub&gt; &amp;lt; B&lt;sub&gt;j-1&lt;/sub&gt;. On the other hand, if B&lt;sub&gt;j&lt;/sub&gt; &amp;lt; A&lt;sub&gt;i&lt;/sub&gt;, then B&lt;sub&gt;j&lt;/sub&gt; &amp;lt; A&lt;sub&gt;i-1&lt;/sub&gt;. &lt;/p&gt;  &lt;p&gt;Using the above relationship, it becomes clear that when A&lt;sub&gt;i&lt;/sub&gt; &amp;lt; B&lt;sub&gt;j&lt;/sub&gt;, A&lt;sub&gt;i&lt;/sub&gt; and its lower portion could never be the k-th smallest element. So do B&lt;sub&gt;j&lt;/sub&gt; and its upper portion. Therefore, we could conveniently discard A&lt;sub&gt;i&lt;/sub&gt; with its lower portion and B&lt;sub&gt;j&lt;/sub&gt; with its upper portion.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:2f3ad72a-540a-4bb9-85f5-2b2d65d76e5d" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 620px; height: 566px;" src="http://lh3.ggpht.com/-RXXJqpwap0A/ToCfV8Lr-RI/AAAAAAAAE-4/uaABBwTzUfY/transformedimage%25255B58%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-2342820447979363306?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/2342820447979363306/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=2342820447979363306' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/2342820447979363306'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/2342820447979363306'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/kth-smallest-element.html' title='Kth Smallest element'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/-RXXJqpwap0A/ToCfV8Lr-RI/AAAAAAAAE-4/uaABBwTzUfY/s72-c/transformedimage%25255B58%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-5237382769986808784</id><published>2011-03-18T09:10:00.001-07:00</published><updated>2011-09-26T08:56:32.269-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Saving a Binary Search Tree to a File</title><content type='html'>&lt;p&gt;Q: Describe an algorithm to save a Binary Search Tree (BST) to a file in terms of run-time and disk space complexity. You must be able to restore to the exact original BST using the saved format&lt;/p&gt;  &lt;p&gt;&lt;small&gt;&lt;font size="2"&gt;Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be “resurrected” later in the same or another computer environment&lt;/font&gt;.&lt;/small&gt;&lt;/p&gt;  &lt;p&gt;Pre-order traversal is the &lt;i&gt;perfect &lt;/i&gt;algorithm for making a copy of a BST. We can store the preorder traversal of the tree into File and then read it again to construct the tree. Assuming we have a file where the pre order traversal of the tree is written to it, We will now see how to read the file to construct that into BST.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:653b4222-cee5-4df5-80ae-6a4a2fb2b4e5" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 623px; height: 656px;" src="http://lh5.ggpht.com/-NTUBOkd0M7A/ToCgrxzdwEI/AAAAAAAAE-8/4VYjRFLv8Nk/transformedimage%25255B62%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-5237382769986808784?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/5237382769986808784/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=5237382769986808784' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/5237382769986808784'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/5237382769986808784'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/saving-binary-search-tree-to-file.html' title='Saving a Binary Search Tree to a File'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/-NTUBOkd0M7A/ToCgrxzdwEI/AAAAAAAAE-8/4VYjRFLv8Nk/s72-c/transformedimage%25255B62%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-763836518049336772</id><published>2011-03-18T08:29:00.001-07:00</published><updated>2011-09-26T08:58:56.095-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Sorted List to Balanced Binary Search Tree</title><content type='html'>&lt;p&gt;Q: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.&lt;/p&gt;  &lt;p&gt;We can solve this problem using bottom up approach with recursion. We create nodes bottom-up, and assign them to its parents. The bottom-up approach enables us to access the list in its order while creating nodes.&amp;#160; Lets define the node structure for Binary Search Tree Node as follows&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:ae2c3f3d-0a44-4e3d-a08e-527d251e520a" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 574px; height: 244px;" src="http://lh3.ggpht.com/-G3RPaLbD2BY/ToChOpnglHI/AAAAAAAAE_A/Lr9JY9N7JFw/transformedimage%25255B42%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;p&gt;Below is the java code for converting a singly linked list to a balanced BST. The algorithm requires the list’s length to be passed in as the function’s parameters. The list’s length could be found in &lt;i&gt;O&lt;/i&gt;(&lt;i&gt;N&lt;/i&gt;) time by traversing the entire list’s once. The recursive calls traverse the list and create tree’s nodes by the list’s order, which also takes &lt;i&gt;O&lt;/i&gt;(&lt;i&gt;N&lt;/i&gt;) time. Therefore, the overall run time complexity is still &lt;i&gt;O&lt;/i&gt;(&lt;i&gt;N&lt;/i&gt;).&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:6b69cd8f-1ed1-4c2f-bd4e-987daca6b25f" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 617px; height: 325px;" src="http://lh5.ggpht.com/-BHzQJBvgxEA/ToChPi9WWVI/AAAAAAAAE_E/jVoyPd5tDWA/transformedimage%25255B22%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-763836518049336772?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/763836518049336772/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=763836518049336772' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/763836518049336772'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/763836518049336772'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/sorted-list-to-balanced-binary-search.html' title='Sorted List to Balanced Binary Search Tree'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/-G3RPaLbD2BY/ToChOpnglHI/AAAAAAAAE_A/Lr9JY9N7JFw/s72-c/transformedimage%25255B42%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-9206081710341710480</id><published>2011-03-16T10:12:00.001-07:00</published><updated>2011-09-26T09:14:09.364-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Word Suggest</title><content type='html'>&lt;p&gt;This is general algorithm we use in search engines. Question is Given a dictionary suggest the words for a given misspelled word.&lt;/p&gt;  &lt;p&gt;The word could be misspelled in either of the following 4 ways.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Transpositions: These are set of words where 2 adjacent characters in a word are trans positioned. Ex: hlelo –&amp;gt; hello . There are atmost n-1 trans positioned words for a given word. &lt;/li&gt;    &lt;li&gt;Insertion: Word could be misspelled by inserting an extra character. Ex: helxlo –&amp;gt; hello There could be at the max n+1 words with this. &lt;/li&gt;    &lt;li&gt;Substitution: Word could be misspelled by typing wrong character in place of another character. Ex: helxo –&amp;gt; hello . Max No. of Substituted words could 25 * ( n ) &lt;/li&gt;    &lt;li&gt;Omissions: These are set of words where a character is omitted Ex: hllo –&amp;gt; hello . Max No of omitted words could be 26*(n+1) &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Lets code them now.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:bf869c2a-506d-4118-964f-700084061584" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 583px; height: 720px;" src="http://lh4.ggpht.com/-CM31mtAdbEI/ToCkxScSG8I/AAAAAAAAE_I/d9loqEqUGyo/transformedimage%25255B13%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div id="codeSnippetWrapper"&gt;&lt;/div&gt;          &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:fe6238f8-9585-401e-ab0b-99096173d442" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 594px; height: 600px;" src="http://lh3.ggpht.com/-0p_j-k5GN3c/ToCkyGMiiLI/AAAAAAAAE_M/v5iMkU8iz1o/transformedimage%25255B34%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:74a2a026-9f1c-481e-b1a2-6fb9d953bdb7" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 609px; height: 792px;" src="http://lh5.ggpht.com/-YwCEzW1ZDU4/ToCkyw2xWjI/AAAAAAAAE_Q/8kz6xrjI89U/transformedimage%25255B49%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:dc8c3d23-a7a9-46ba-9a4c-591fe86aa9dc" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 609px; height: 680px;" src="http://lh6.ggpht.com/-Bxj1oLJiCME/ToCkzx7qoMI/AAAAAAAAE_U/a_u8SeJkesA/transformedimage%25255B59%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-9206081710341710480?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/9206081710341710480/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=9206081710341710480' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/9206081710341710480'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/9206081710341710480'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/word-suggest.html' title='Word Suggest'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/-CM31mtAdbEI/ToCkxScSG8I/AAAAAAAAE_I/d9loqEqUGyo/s72-c/transformedimage%25255B13%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-5442334741197891118</id><published>2011-03-16T08:46:00.001-07:00</published><updated>2011-09-26T10:01:07.277-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Suffix Trees</title><content type='html'>&lt;p&gt;Suffix Tree for a String T is Trie like data that represents the suffixes of T. For example, if your word were bibs, you would create the following tree. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_Pg0RCzJJYC4/TYDbbT5HwYI/AAAAAAAAEmU/dqT-hRi9ZfQ/s1600-h/Capture%5B3%5D.jpg"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="Capture" border="0" alt="Capture" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TYDbb5cWIZI/AAAAAAAAEmY/JHPzA44eOWM/Capture_thumb%5B1%5D.jpg?imgmax=800" width="463" height="255" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Suffix trees answers the queries related to one string. There are many applications of suffix trees. Some of them are..&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;String Matching and Pattern Matching algorithm &lt;/li&gt;    &lt;li&gt;Longest Repeated SubString &lt;/li&gt;    &lt;li&gt;Longest palindrome &lt;/li&gt;    &lt;li&gt;Longest common substring &lt;/li&gt;    &lt;li&gt;Longest Common prefix &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Sample java Implementation of Suffix Tree:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:d06f0e74-c8e8-459f-a16d-e3b406a94094" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 546px; height: 527px;" src="http://lh4.ggpht.com/-Fd9ZzGF8Zwk/ToCvzogB3II/AAAAAAAAE_Y/SHUIrkPXGWE/transformedimage%25255B13%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:b70e0564-b79b-4962-b039-c56e9227fd94" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 536px; height: 612px;" src="http://lh3.ggpht.com/-NWaqG_K5_jM/ToCv0U1tHWI/AAAAAAAAE_c/j-bTTdzN0lM/transformedimage%25255B64%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;h6&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/h6&gt;  &lt;h6&gt;&lt;font size="2"&gt;String Search&lt;/font&gt;&lt;/h6&gt;  &lt;p&gt;Searching for a substring, &lt;font face="Arial"&gt;&lt;code&gt;pat[1..m]&lt;/code&gt;, in &lt;code&gt;txt[1..n]&lt;/code&gt;, can be solved in O(m) time (after the suffix tree for &lt;code&gt;txt&lt;/code&gt; has been built in O(n) time). &lt;/font&gt;&lt;/p&gt;  &lt;h6&gt;&lt;font size="2"&gt;Longest Repeated Substring&lt;/font&gt;&lt;/h6&gt;  &lt;p&gt;Add a special ``end of string'' character, e.g. `$', to &lt;font face="Arial"&gt;&lt;code&gt;txt[1..n]&lt;/code&gt; and build a suffix tree; the &lt;em&gt;longest repeated substring&lt;/em&gt; of &lt;code&gt;txt[1..n]&lt;/code&gt; is indicated by the &lt;em&gt;deepest&lt;/em&gt; fork node in the suffix tree, where depth is measured by the number of &lt;em&gt;characters&lt;/em&gt; traversed from the root, i.e., `issi' in the case of `mississippi'. The longest repeated substring can be found in O(n) time using a suffix tree. &lt;/font&gt;&lt;/p&gt;  &lt;h6&gt;&lt;font size="2"&gt;Longest Common Substring&lt;/font&gt;&lt;/h6&gt;  &lt;p&gt;The &lt;em&gt;longest common substring&lt;/em&gt; of two strings, T1 &lt;font face="Arial"&gt;and T2, can be found by building a &lt;em&gt;generalized&lt;/em&gt; suffix tree for T1&amp;#160; and T2 : Each node is &lt;em&gt;marked&lt;/em&gt; to indicate if it represents a suffix of &lt;code&gt;T1 &lt;/code&gt;or &lt;font face="Courier New"&gt;T2&lt;/font&gt; or both. The deepest node marked for both T1 and T2 represents the longest common substring. &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Equivalently, one can build a (basic) suffix tree for the string T1$T2#&lt;font face="Arial"&gt;, where `$' is a special terminator for T1 and `#' is a special terminator for T2. The longest common substring is indicated by the deepest fork node that has both `...$...' and `...#...' (no $) beneath it.      &lt;br /&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;Palindromes&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;A palindrome is a string, P, such that P=reverse(P). e.g. `abba'=reverse(`abba'). e.g. `ississi' is the &lt;em&gt;longest&lt;/em&gt; palindrome in `mississippi'. &lt;/p&gt;  &lt;p&gt;The longest palindrome of T&lt;font face="Arial"&gt; can be found in O(n) time, e.g. by building the suffix tree for T&lt;code&gt;$reverse(T)#&lt;/code&gt; or by building the generalized suffix tree for T and &lt;code&gt;reverse(T)&lt;/code&gt;.&lt;/font&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-5442334741197891118?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/5442334741197891118/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=5442334741197891118' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/5442334741197891118'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/5442334741197891118'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/suffix-trees.html' title='Suffix Trees'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/_Pg0RCzJJYC4/TYDbb5cWIZI/AAAAAAAAEmY/JHPzA44eOWM/s72-c/Capture_thumb%5B1%5D.jpg?imgmax=800' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-1416218302992478258</id><published>2011-03-16T02:33:00.001-07:00</published><updated>2011-09-26T10:08:21.158-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Linked List Question</title><content type='html'>&lt;p&gt;Q: Given a linked list with two pointers in node structure where one points to the next node and the other pointer points to some random node in the linked list. Write a program to create a copy of this linked list&lt;/p&gt;  &lt;p&gt;The above problem can be solved in O( n ) time and in constant space complexity. Assuming the Node Structure as following..&lt;/p&gt;  &lt;p&gt;Algorithm:&lt;/p&gt;  &lt;p&gt;1)[Merge listst]Create list B this way: for each NodeA in A create a NodeB in B , copy NEXT ptr to NodeB: NodeB{NEXT} = NodeA{NEXT} and change NEXT ptr in NodeA to this newly created node in B: NodeA{NEXT}=NodeB;    &lt;br /&gt;2) go through the list A (NB: you have to jump through the nodes of B!) and set NEXTRND ptr in next B nodes to the next ptrs after next_random nodes: NodeA{NEXT}{NEXTRND}=NodeA{NEXTRNF}{NEXT};     &lt;br /&gt;After that all NEXTRND nodes in list B will be pointing to correct nodes in B;     &lt;br /&gt;3) [Un-merge]. For each node in A and in NEXT node B set correct NEXT ptrs:     &lt;br /&gt;node=headA;     &lt;br /&gt;while(node){tmp = node{NEXT}; node{NEXT}=tmp?tmp{NEXT}:NULL; node=tmp;}     &lt;br /&gt;After this process all {NEXT} nodes in both lists A and B will be correct&lt;/p&gt;  &lt;p&gt;Java Code:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:99a650f1-8af6-4b9a-af6e-8eead34461e4" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 659px; height: 790px;" src="http://lh4.ggpht.com/-RvasfKfU74o/ToCxhAhX5qI/AAAAAAAAE_g/oPUGYnkaCOc/transformedimage%25255B118%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-1416218302992478258?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/1416218302992478258/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=1416218302992478258' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/1416218302992478258'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/1416218302992478258'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/linked-list-question.html' title='Linked List Question'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/-RvasfKfU74o/ToCxhAhX5qI/AAAAAAAAE_g/oPUGYnkaCOc/s72-c/transformedimage%25255B118%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-7907158005141879242</id><published>2011-03-16T02:05:00.001-07:00</published><updated>2011-09-26T10:10:17.361-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Best days to sell and buy stock, if array of stock prices given</title><content type='html'>&lt;p&gt;&lt;strong&gt;Q: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to buy one share of the stock and sell one share of the stock, design an algorithm to find the best times to buy and sell&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Algorithm: Go through the array in order, keeping track of the lowest stock price and the best deal you've seen so far. Whenever the current stock price minus the current lowest stock price is better than the current best deal, update the best deal to this new value.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Working Java Code:&lt;/strong&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:4649aba5-8eca-4bda-b65f-cda9d4b8eef0" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 659px; height: 790px;" src="http://lh5.ggpht.com/-ef0oL3OxQmA/ToCx-D50VFI/AAAAAAAAE_k/4GIMClU2KQ0/transformedimage%25255B7%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-7907158005141879242?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/7907158005141879242/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=7907158005141879242' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/7907158005141879242'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/7907158005141879242'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/best-days-to-sell-and-buy-stock-if.html' title='Best days to sell and buy stock, if array of stock prices given'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/-ef0oL3OxQmA/ToCx-D50VFI/AAAAAAAAE_k/4GIMClU2KQ0/s72-c/transformedimage%25255B7%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-4084103926630274002</id><published>2011-03-15T10:58:00.001-07:00</published><updated>2011-09-26T10:12:12.691-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Arranging elements in specific order</title><content type='html'>&lt;h5&gt;n a given array of elements like [a1, a2, a3, a4, ..... an, b1, b2, b3, b4, ....... bn, c1, c2, c3, c4, ........ cn] without taking a extra memory how to merge like [a1, b1, c1, a2, b2, c2, a3, b3, c3, ............ an, bn, cn] &lt;/h5&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:b3918051-b669-4566-a376-5a3ffab507ef" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 543px; height: 535px;" src="http://lh3.ggpht.com/-4rh5LpiDlZ0/ToCyazV_gwI/AAAAAAAAE_o/RVbvPCwjul8/transformedimage%25255B28%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-4084103926630274002?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/4084103926630274002/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=4084103926630274002' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4084103926630274002'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4084103926630274002'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/arranging-elements-in-specific-order.html' title='Arranging elements in specific order'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/-4rh5LpiDlZ0/ToCyazV_gwI/AAAAAAAAE_o/RVbvPCwjul8/s72-c/transformedimage%25255B28%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-5348180183450763519</id><published>2011-03-15T10:50:00.001-07:00</published><updated>2011-09-26T10:26:48.646-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Triplet (x,y,z) whose distance is min</title><content type='html'>&lt;h5&gt;Given 3 sorted arrays (in ascending order). Let the arrays be A(of n1 length), B(of n2 length), C(of n3 length). Let x be an element of A, y of B, z of C. The distance between x, y and z is defined as the max(|x-y|,|y-z|,|z-x|). Find the tuple with the minimum distance? &lt;/h5&gt;  &lt;p&gt;Working java Code:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:dfb0580b-dedb-488f-b259-3e57a3ad652d" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 535px; height: 475px;" src="http://lh5.ggpht.com/-iKMhMx-seZo/ToC10_p1oYI/AAAAAAAAE_s/5Gk91wucZhA/transformedimage%25255B160%25255D.png?imgmax=800" /&gt;&lt;/div&gt;            &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:1c34f6fd-26e9-40f7-ad68-5112c866fa78" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 599px; height: 597px;" src="http://lh5.ggpht.com/-WpmqMCguPbE/ToC11-PpXPI/AAAAAAAAE_w/MjVQvWCCM4A/transformedimage%25255B204%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-5348180183450763519?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/5348180183450763519/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=5348180183450763519' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/5348180183450763519'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/5348180183450763519'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/triplet-xyz-whose-distance-is-min.html' title='Triplet (x,y,z) whose distance is min'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/-iKMhMx-seZo/ToC10_p1oYI/AAAAAAAAE_s/5Gk91wucZhA/s72-c/transformedimage%25255B160%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-3269173228543506657</id><published>2011-03-15T10:40:00.001-07:00</published><updated>2011-07-11T12:07:19.095-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview '/><title type='text'>External Merge Sort</title><content type='html'>&lt;p&gt;If data to be sorted doesn’t fit into main memory external sorting is applicable. External merge sort can be separated into two phases:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Create initial runs (run is a sequence of records that are in correct relative order or in other words sorted chunk of original file). &lt;/li&gt;    &lt;li&gt;Merge created runs into single sorted file. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Let’s assume that M records at the same time are allowed to be loaded into main memory. One of the ways to create initial runs is to successively read M records from original file, sort them in memory and write back to disk. &lt;/p&gt;  &lt;p&gt;The core structure behind this algorithm is priority queue. Taking one by one current minimum element out of the queue forms ordered sequence.&lt;/p&gt;  &lt;p&gt;The algorithm can be described as follows:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Create two priority queues (that will contain items for current and next runs respectively) with capacity of M records. &lt;/li&gt;    &lt;li&gt;Prefill current run priority queue from unsorted file with M records. &lt;/li&gt;    &lt;li&gt;Create current run if there are elements in the current run priority queue:      &lt;ol&gt;       &lt;li&gt;Take minimum element out of current run priority queue and append it to current run (basically write it to disk). &lt;/li&gt;        &lt;li&gt;Take next element from unsorted file (this is the replacement part of the algorithm) and compare it with just appended element. &lt;/li&gt;        &lt;li&gt;If it is less then it cannot be part of the current run (or otherwise order will be destroyed) and thus it is queued to the next&amp;#160; run priority queue. &lt;/li&gt;        &lt;li&gt;Otherwise it is part of the current run and it is queued to the current run priority queue. &lt;/li&gt;        &lt;li&gt;Continue steps 1 through 4 until current run priority queue is empty. &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;Switch current and next runs priority queues and repeat step 3. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;At any given moment at most M records are loaded into main memory as single written element into current run is replaced with single element from unsorted file if any (depending on comparison it either goes into current or next run).&lt;/p&gt;  &lt;p&gt;Next step is to merge created initial runs. For the merge step we will use simplified algorithm based on k-way merge. &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Append created runs into a queue. &lt;/li&gt;    &lt;li&gt;Until there are more than one run in the queue:      &lt;ol&gt;       &lt;li&gt;Dequeue and merge K runs into a single run and put it into the queue. &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;Remaining run represents sorted original file. &lt;/li&gt; &lt;/ol&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-3269173228543506657?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/3269173228543506657/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=3269173228543506657' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3269173228543506657'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3269173228543506657'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/external-merge-sort.html' title='External Merge Sort'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-7966825694322610680</id><published>2011-03-15T10:27:00.001-07:00</published><updated>2011-07-11T12:07:19.427-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview '/><title type='text'>K-Way Merge</title><content type='html'>&lt;p&gt;The classic merge takes as input some sorted lists and at each step outputs element with next smallest key thus producing sorted list that contains all the elements of the input lists.&lt;/p&gt;  &lt;p&gt;We can solve this problem using two ways.&lt;/p&gt;  &lt;p&gt;Let the sorted arrays be A1, A2,...Ak   &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;Way 1:   &lt;br /&gt;Merge (A1, A2), (A3, A4),...    &lt;br /&gt;Then there are k/2 sorted arrays now.    &lt;br /&gt;Repeat the above procedure to get the full array sorted. It is not always practical to have whole sequence in memory because of its considerable size nor to constraint sequence to be finite as only K first elements may be needed. Thus our algorithm must produce monotonically increasing (according to some comparison logic) potentially infinite sequence. This way wont work as the memory we can use is limited&lt;/p&gt;  &lt;p&gt;Way 2:   &lt;br /&gt;Form a priority min heap from the first element of all the k arrays. Extract the min element. It is the min element among all the arrays. Now take the next element from the array from which the min element has come, and put it in the min heap. Repeat the procedure.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The queue will hold non empty sequences. &lt;/li&gt;    &lt;li&gt;The priority of a sequence is its next element. &lt;/li&gt;    &lt;li&gt;At each step we dequeue out of the queue sequence that has smallest next element. This element is next in the merged sequence. &lt;/li&gt;    &lt;li&gt;If dequeued sequence is not empty it must be enqueued back because it may contain next smallest element in the merged&amp;#160; sequence. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Thus we will have queue of size that doesn’t exceed m (number of sequences) and thus making total running time O(n log m).&amp;#160; It works well with infinite sequences and cases where we need only K first elements and it fetches only bare minimum out of source sequences. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-7966825694322610680?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/7966825694322610680/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=7966825694322610680' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/7966825694322610680'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/7966825694322610680'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/k-way-merge.html' title='K-Way Merge'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-4539875144233601335</id><published>2011-03-15T09:59:00.001-07:00</published><updated>2011-09-26T10:33:17.381-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Selecting k smallest or largest elements</title><content type='html'>&lt;p&gt;There are cases when you need to select a number of best (according to some definition) elements out of finite sequence (list). For example, select 10 most popular baby names in a particular year or select 10 biggest files on your hard drive.&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;While selecting single minimum or maximum element can easily be done iteratively in O(n) selecting k smallest or largest elements (k smallest for short) is not that simple. One approach could be sort the elements and take the first k elements. But that requires O( nlogn) time. &lt;/p&gt;  &lt;p&gt;Priority Queue can be used for better performance if only subset of sorted sequence is required. It requires O(n) to build priority queue based on binary min heap and O(k log n) to retrieve first k elements. Can we improve this further?&lt;/p&gt;  &lt;p&gt;Quicksort algorithm picks pivot element, reorders elements such that the ones less than pivot go before it while greater elements go after it (equal can go either way). After that pivot is in its final position. Then both partitions are sorted recursively making whole sequence sorted. In order to prevent worst case scenario pivot selection can be randomized. &lt;/p&gt;  &lt;p&gt;Basically we are interested in the k smallest elements themselves and not the ordering relation between them. Assuming partitioning just completed let’s denote set of elements that are before pivot (including pivot itself) by L and set of elements that are after pivot by H. According to partition definition L contains |L| (where |X| denotes number of elements in a set X) smallest elements. If |L| is equal to k we are done. If it is less than k than look for k smallest elements in L. Otherwise as we already have |L| smallest elements look for k - |L| smallest elements in H. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Working Java Code:&lt;/strong&gt;&lt;/p&gt; &lt;code style="padding-bottom: 2px; padding-left: 2px; padding-right: 2px; display: block; background: rgb(38,41,46); color: white; font-size: 12px; padding-top: 2px; -moz-background-clip: border; -moz-background-inline-policy: continuous; -moz-background-origin: padding"&gt;   &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:351fd79c-9d7a-413e-b980-41f75e26071b" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 624px; height: 628px;" src="http://lh3.ggpht.com/-5arIzhf5FbY/ToC3WMKWI9I/AAAAAAAAE_0/XqyBla7r1JU/transformedimage%25255B15%25255D.png?imgmax=800" /&gt;&lt;/div&gt; &lt;/code&gt;    &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:eec2965c-d8e4-4ecb-82db-a7a507f9bf83" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 628px; height: 593px;" src="http://lh5.ggpht.com/-aNqUhdNAxQQ/ToC3W4LxXuI/AAAAAAAAE_4/EdcE7Hwngw0/transformedimage%25255B32%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-4539875144233601335?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/4539875144233601335/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=4539875144233601335' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4539875144233601335'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4539875144233601335'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/selecting-k-smallest-or-largest.html' title='Selecting k smallest or largest elements'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/-5arIzhf5FbY/ToC3WMKWI9I/AAAAAAAAE_0/XqyBla7r1JU/s72-c/transformedimage%25255B15%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-3315408583132544353</id><published>2011-03-15T09:40:00.001-07:00</published><updated>2011-09-26T10:37:08.555-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Queue based on single stack</title><content type='html'>&lt;p&gt;Queue based on 2 stacks is generally what we usually know of and probably would be on our minds while answering such questions. We could solve the same using one stack and taking advantage of recursion. We will see how we can solve this with no additional storage.&lt;/p&gt;  &lt;p&gt;Assume that items come out of stack in the order they must appear in the queue (FIFO). Choosing the opposite order is also possible however is not practical. To make it happen we simply need to make sure that items in the stack (LIFO) are placed in the opposite order. Items queued first must appear at the top of the stack. This basically means that in order to queue item all items must be popped, the item&amp;#160; pushed and then existent items pushed inversely to pop order. But we have no additional explicit storage requirement. Then store items implicitly through recursion. &lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:fedd3e99-c411-4d23-b6a5-07dc2dec9f38" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 628px; height: 680px;" src="http://lh4.ggpht.com/-01kvZL1qfog/ToC4QO9f9PI/AAAAAAAAE_8/Yy_aBVH6UtU/transformedimage%25255B13%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:2f4b6fd2-1e2c-44c1-9f8e-594161080fc4" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 616px; height: 457px;" src="http://lh6.ggpht.com/-u92m8QaBOB0/ToC4Q0faWHI/AAAAAAAAFAA/VEOfcDXYoI8/transformedimage%25255B29%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;p&gt;Enqueue is a O(n) operation (where n is the number items in the stack). Dequeue and Peek is a O(1) operation.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-3315408583132544353?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/3315408583132544353/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=3315408583132544353' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3315408583132544353'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3315408583132544353'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/queue-based-on-single-stack.html' title='Queue based on single stack'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/-01kvZL1qfog/ToC4QO9f9PI/AAAAAAAAE_8/Yy_aBVH6UtU/s72-c/transformedimage%25255B13%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-2993164020841088215</id><published>2011-03-15T09:00:00.001-07:00</published><updated>2011-09-26T10:48:55.190-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Cumulative sum of a tree at each node.</title><content type='html'>&lt;h5&gt;Q: Given a binary tree, print the sum( inclusive of the root ) of sub-tree rooted at each node in in-order &lt;/h5&gt;  &lt;p&gt;This question can be answered in two ways. We can do it using two passes with constant space of with one pass and O( n ) space.&lt;/p&gt;  &lt;p&gt;Lets say the tree node is as follows &lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:d631c413-e26c-45ca-8d4b-d3579982a5b9" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 566px; height: 668px;" src="http://lh3.ggpht.com/-6QwdNua3cQE/ToC7A-mPv7I/AAAAAAAAFAI/Nj-q8XHzhWA/transformedimage%25255B139%25255D.png?imgmax=800" /&gt;&lt;/div&gt;    &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:266b9c80-9362-4c70-b95d-9f200ee51a75" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 532px; height: 224px;" src="http://lh3.ggpht.com/-UZuczwg41cU/ToC7BnI4zCI/AAAAAAAAFAM/yk0k4oHq5D8/transformedimage%25255B159%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-2993164020841088215?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/2993164020841088215/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=2993164020841088215' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/2993164020841088215'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/2993164020841088215'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/cumulative-sum-of-tree-at-each-node.html' title='Cumulative sum of a tree at each node.'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/-6QwdNua3cQE/ToC7A-mPv7I/AAAAAAAAFAI/Nj-q8XHzhWA/s72-c/transformedimage%25255B139%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-4369490785140346792</id><published>2011-03-15T08:24:00.001-07:00</published><updated>2011-09-26T10:51:18.340-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview'/><title type='text'>Converting a Tree to a double Linked List</title><content type='html'>&lt;p&gt;Q: Write a program which will accept Binary Search Tree and convert it to a sorted Double Linked List.&lt;/p&gt;  &lt;p&gt;For example if the Binary search tree is as shown below…&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_Pg0RCzJJYC4/TX-Eo1FSgnI/AAAAAAAAEmA/PH_25PxDtJM/s1600-h/tree%5B3%5D.jpg"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="tree" border="0" alt="tree" src="http://lh4.ggpht.com/_Pg0RCzJJYC4/TX-EpbjnSoI/AAAAAAAAEmE/8GOUyJ97LU0/tree_thumb%5B1%5D.jpg?imgmax=800" width="452" height="323" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Then the converted doubly linked&amp;#160; list should be&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_Pg0RCzJJYC4/TX-EqBxCXII/AAAAAAAAEmI/XTTJsYALU_M/s1600-h/list%5B8%5D.jpg"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="list" border="0" alt="list" src="http://lh6.ggpht.com/_Pg0RCzJJYC4/TX-EqjocpZI/AAAAAAAAEmM/lE2tQ5WYxvw/list_thumb%5B4%5D.jpg?imgmax=800" width="443" height="280" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Lets define the Node structure of the tree as follows&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:7bd86afe-e55f-4a4d-b8ce-5fe7560660b6" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 518px; height: 262px;" src="http://lh4.ggpht.com/-IDqJ4BnXtjk/ToC7kuvNGTI/AAAAAAAAFAQ/zz600xudn2Q/transformedimage%25255B47%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;p&gt;We can use recursive approach to solve this by just arranging the pointers..&lt;/p&gt;  &lt;p&gt;Algorithm:&lt;/p&gt;  &lt;p&gt;If tree is not empty&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Convert Left Subtree to List and let hLeft point to it&lt;/p&gt;    &lt;p&gt;Convert Right Subtree to List and left hRight point to it.&lt;/p&gt;    &lt;p&gt;Append hLeft to root and then hRight&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Working java Code:&lt;/strong&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:4116afab-6035-4a5b-9a6d-2540d76d3981" class="wlWriterEditableSmartContent"&gt;&lt;img style="border:0; width: 510px; height: 620px;" src="http://lh6.ggpht.com/-GJBZ7T2j9uw/ToC7lXkA9QI/AAAAAAAAFAU/kJEh597LZoQ/transformedimage%25255B29%25255D.png?imgmax=800" /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-4369490785140346792?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/4369490785140346792/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=4369490785140346792' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4369490785140346792'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4369490785140346792'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/converting-tree-to-double-linked-list.html' title='Converting a Tree to a double Linked List'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_Pg0RCzJJYC4/TX-EpbjnSoI/AAAAAAAAEmE/8GOUyJ97LU0/s72-c/tree_thumb%5B1%5D.jpg?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-1350306991235634820</id><published>2011-03-14T10:24:00.001-07:00</published><updated>2011-07-11T12:07:18.987-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview '/><title type='text'>Minimum window that contains all characters</title><content type='html'>&lt;h5&gt;Q: Find the smallest window in a string containing all characters of another string &lt;/h5&gt;  &lt;p&gt;Given set of characters P and string T find minimum window in T that contains all characters in P. Applicable solution is restricted to O(length(T)) time complexity. For example, given a string T “of characters and as” and set of characters T in a form of a string “aa s” the minimum window will be “and as”. &lt;/p&gt;  &lt;p&gt;The problem can be broken into two parts:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;How to select window? &lt;/li&gt;    &lt;li&gt;How to check that selected window contains all characters from P? &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Selecting every possible window (all unique pairs (i, j) where 0 &amp;lt;= i &amp;lt;= j &amp;lt; length(T)) will lead to solution worse than O(length(T)^2) because you still need to check if all characters from P are within selected window. Instead we will check every possible window ending position. Thus there are at least length(T) windows to consider. &lt;/p&gt;  &lt;p&gt;Any feasible window has length equal to or greater than length(P). Performing recheck for any considered window will result in a solution no better than O(length(T)*length(P)). Instead we need to use check results from previous iteration.&lt;/p&gt;  &lt;p&gt;Now we need to make sure that checking if a particular character is in P is done in an optimal way. Taking into account that a particular character may appear more than once and window thus must contain appropriate number of characters. We will use hash table to map unique characters from P to their count for fast lookup. &lt;/p&gt;  &lt;p&gt;And now let’s tie all things together. &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Until reached the end of T move by one current window ending position. &lt;/li&gt;    &lt;li&gt;Append next character to the end of previous window which to this moment doesn’t contain all necessary characters. Char to count map is used to track the number of characters left to find. Basically if character is in P count is decremented. The number may become negative meaning that there are more than required characters. &lt;/li&gt;    &lt;li&gt;If unmatched character count goes to zero the window contains all required characters. However there may be redundant characters. Thus we try to compact current window. It is ok to do this as we are looking for minimum window and any window that is extended from this one won’t be better. &lt;/li&gt;    &lt;li&gt;Once window is compacted compare it with the minimum one and updated it if needed. &lt;/li&gt;    &lt;li&gt;If current window contains all the characters remove from it the first one simply by moving by one starting position to make sure that at each iteration previous window doesn’t contain all the characters (there is no point in appending new characters to a window that already contains all of the required ones). &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Working Java code:&lt;/strong&gt;&lt;/p&gt; &lt;code style="padding-bottom: 2px; padding-left: 2px; padding-right: 2px; display: block; background: rgb(38,41,46); color: white; font-size: 12px; padding-top: 2px; -moz-background-clip: border; -moz-background-inline-policy: continuous; -moz-background-origin: padding"&gt;   &lt;p&gt;public static String findMinWindow(String t, String p)   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Create char to count mapping for fast lookup    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // as some characters may appear more than once    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; HashMap&amp;lt;Character, Integer&amp;gt; charToCount = new HashMap&amp;lt;Character, Integer&amp;gt;();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; for (char c : p.toCharArray())    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (!charToCount.containsKey(c))    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; charToCount.put(c, 0);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; charToCount.put(c, charToCount.get(c)+1);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int unmatchesCount = p.length();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int minWindowLength = t.length() + 1,minWindowStart = -1;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int currWindowStart = 0, currWindowEnd = 0;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; for (; currWindowEnd &amp;lt; t.length(); currWindowEnd++)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; char c = t.charAt(currWindowEnd);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Skip chars that are not in P    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (!charToCount.containsKey(c))    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; continue;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Reduce unmatched characters count    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; charToCount.put(c, charToCount.get(c)-1);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (charToCount.get(c) &amp;gt;= 0)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // But do this only while count is positive    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // as count may go negative which means    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // that there are more than required characters    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; unmatchesCount--;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // No complete match, so continue searching    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (unmatchesCount &amp;gt; 0)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; continue;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Decrease window as much as possible by removing    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // either chars that are not in T or those that    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // are in T but there are too many of them    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c = t.charAt(currWindowStart);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; boolean contains = charToCount.containsKey(c);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; while (!contains || charToCount.get(c) &amp;lt; 0)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (contains)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Return character to P    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; charToCount.put(c,charToCount.get(c)+1);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c = t.charAt(++currWindowStart);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; contains = charToCount.containsKey(c);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (minWindowLength &amp;gt; currWindowEnd - currWindowStart + 1)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; minWindowLength = currWindowEnd - currWindowStart + 1;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; minWindowStart = currWindowStart;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Remove last char from window - it is definitely in a    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // window because we stopped at it during decrease phase    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; charToCount.put(c, charToCount.get(c)+1);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; unmatchesCount++;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; currWindowStart++;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return minWindowStart &amp;gt; -1 ?    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; t.substring(minWindowStart, minWindowLength) : &amp;quot;&amp;quot;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;/code&gt;&lt;p&gt;Every character is examined at most twice (during appending to the end and during compaction) so the whole solution has O(length(T)) time complexity assuming hash table lookup is O(1) operation. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-1350306991235634820?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/1350306991235634820/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=1350306991235634820' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/1350306991235634820'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/1350306991235634820'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/minimum-window-that-contains-all.html' title='Minimum window that contains all characters'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-3304530549985564818</id><published>2011-03-14T09:50:00.001-07:00</published><updated>2011-07-11T12:07:19.108-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview '/><title type='text'>Traversing binary tree in zig zag order</title><content type='html'>&lt;p&gt;The question is we need to traverse binary tree level by level in such a way that traversal direction within level changes from level to level thus forming a spiral. &lt;/p&gt;  &lt;p&gt;We’ll start with a well know problem of traversing binary tree level by level It can be done using queue. But this problem seems to be tricky. Its not the obvious approach. Instead of going with queue approach we can solve this problem using two stacks. Let us say the two stacks are curr Level and nextLevel. Next level is a set of child nodes from current level. We also need a variable to keep track of the current level’s order. If next level must be traversed from left to right we must first push right child node and then left and in opposite order if next level will be traversed from right to left we will push the left child first and then right child.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Working Java Code:&lt;/strong&gt;&lt;/p&gt;  &lt;code style="padding-bottom: 2px; padding-left: 2px; padding-right: 2px; display: block; background: rgb(38,41,46); color: white; font-size: 12px; padding-top: 2px; -moz-background-clip: border; -moz-background-inline-policy: continuous; -moz-background-origin: padding"&gt;  &lt;p&gt;public void spiralOrderTraversal(BinaryTreeNode root)   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Stack&amp;lt;BinaryTreeNode&amp;gt; currentLevel,nextLevel;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; BinaryTreeNode temp;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int leftToRight = 1;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(root == null)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; currentLevel = new Stack&amp;lt;BinaryTreeNode&amp;gt;();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; nextLevel = new Stack&amp;lt;BinaryTreeNode&amp;gt;();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; currentLevel.push(root);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; while(!(currentLevel.isEmpty())    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; temp = currentLevel.pop();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(temp)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(temp.data);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(leftToRight)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(temp.left)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; nextLevel.push(temp.left);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(temp.right)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; nextLevel.push(temp.right);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(temp.right)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; nextLevel.push(temp.right);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(temp.left)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; nextLevel.push(temp.left);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(currentLevel.isEmpty())    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; leftToRight = 1-leftToRight;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; swap(currentLevel,nextLevel)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;&lt;/code&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-3304530549985564818?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/3304530549985564818/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=3304530549985564818' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3304530549985564818'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/3304530549985564818'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/traversing-binary-tree-in-zig-zag-order.html' title='Traversing binary tree in zig zag order'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-2568707800585311320</id><published>2011-03-14T09:22:00.001-07:00</published><updated>2011-07-11T12:07:19.047-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview '/><title type='text'>Longest consecutive elements sequence</title><content type='html'>&lt;p&gt;For example, in {5, 7, 3, 4, 9, 10, 1, 6, 15, 1, 3, 12, 2, 11} longest sequence is {1, 2, 3, 4, 5,6}. &lt;/p&gt;  &lt;p&gt;The first thing that comes into our mind is to sort given array in O(n log n ) and look for longest consecutive elements sequence. Can we do it in Linear time?&lt;/p&gt;  &lt;p&gt;The next solution would be using bit vector indexed by numbers from original array. It may not be justified due to incomparable solution space and original array size (although time complexity is O(n)). &lt;/p&gt;  &lt;p&gt;Lets see how to solve this problem in O( n ) time complexity with constant space assuming the hashtable has O(1) time complexity and constant space complexity.&lt;/p&gt;  &lt;p&gt;By Knowing range boundaries we can definitely say what numbers are in there (for example, [1..3] contains 1, 2, 3). O(1) time complexity for range manipulation operations with O(1) space complexity for each range are the things we are looking for. We can do this using two hash tables:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;‘low’ with range start numbers as keys and range end numbers as values &lt;/li&gt;    &lt;li&gt;‘high’ with range end numbers as keys and range start numbers as values &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;For example, for a range [x..y] the tables will hold low[x] = y and high[y] = x. The algorithm looks the following:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Scan original array and for each element:      &lt;ul&gt;       &lt;li&gt;If it already belongs to any range skip it &lt;/li&gt;        &lt;li&gt;Otherwise create unit size range [i..i] &lt;/li&gt;        &lt;li&gt;If there is a range next to the right [i+1.. y] merge with it to produce [i..y] range &lt;/li&gt;        &lt;li&gt;If there is a range next to the left [x..i-1] merge with it as well (either [i..i] or merged [i..y] will be merged with [x..i-1]) &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Scan through created ranges to find out the longest &lt;/li&gt; &lt;/ul&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-2568707800585311320?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/2568707800585311320/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=2568707800585311320' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/2568707800585311320'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/2568707800585311320'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/longest-consecutive-elements-sequence.html' title='Longest consecutive elements sequence'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-4340718231608918120</id><published>2011-03-14T09:02:00.001-07:00</published><updated>2011-07-11T12:07:19.226-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview '/><title type='text'>Word in a Maze</title><content type='html'>&lt;p&gt;Q: Given a puzzle of letters/ characters e.g.&lt;/p&gt;  &lt;p&gt;a e r o p s   &lt;br /&gt;b h a r l s    &lt;br /&gt;w r i s l o     &lt;br /&gt;a s n k t q&lt;/p&gt;  &lt;p&gt;Write a function to which this puzzle and a word will be passed to test whether that word exists in the puzzle or not.    &lt;br /&gt;e.g. rain and slow will return true. rain is present in the third column and slow in the third row wrapped around.&lt;/p&gt;  &lt;p&gt;Algorithm:&lt;/p&gt;  &lt;p&gt;Idea is for each element in matrix find if it matches the first character, if it matches then we have to look in 4 directions. If there is match in that direction proceed till we are end of the string. If match return true if not return false. We have to look in all the directions because the string can be in any of 4 directions. Also Math.abs takes care of wrap around (this was my big bug)&lt;/p&gt;  &lt;p&gt;Working Java Code:&lt;/p&gt;   &lt;code style="padding-bottom: 2px; padding-left: 2px; padding-right: 2px; display: block; background: rgb(38,41,46); color: white; font-size: 12px; padding-top: 2px; -moz-background-clip: border; -moz-background-inline-policy: continuous; -moz-background-origin: padding"&gt; &lt;p&gt;public class StringFinder {&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; enum Direction {   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; up, down, left, right    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; };&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public static void main(String args[]) {   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; char[][] ar = new char[][] { { 'a', 'e', 'r', 'o','p','s' },    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { 'b', 'h', 'a', 'r','l','s' },     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { 'w', 'r', 'i', 's','l','o' },    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { 'a', 's', 'n', 'k','t','q' },     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { 'a', 'e', 'r', 'o','p','s' },    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { 'b', 'h', 'a', 'r','l','s' },};    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String str = &amp;quot;rain&amp;quot;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; StringFinder sf = new StringFinder();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.print(sf.stringFinder(ar, str));    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; private boolean stringFinder(char[][] ar, String str) {   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int size = ar.length;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (str.length() &amp;gt; size + 1)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return false;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int k = 0;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; char[] charArray = str.toCharArray();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; for (int i = 0; i &amp;lt; size; i++)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; for (int j = 0; j &amp;lt; size; j++)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (ar[i][j] == charArray[k])    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return isMatch(ar, charArray, i, Math.abs((j + 1) % size),    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; k + 1, Direction.right)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; || isMatch(ar, charArray, Math.abs((i + 1) % size),    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; j, k + 1, Direction.down)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; || isMatch(ar, charArray, Math.abs((i - 1) % size),    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; j, k + 1, Direction.up)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; || isMatch(ar, charArray, i,    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Math.abs((j - 1) % size), k + 1,    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Direction.left);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return false;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; private boolean isMatch(char[][] ar, char[] charArray, int i, int j, int k,   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Direction dir) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int size = ar.length;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (k == charArray.length - 1) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (ar[i][j] == charArray[k])    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return true;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return false;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (ar[i][j] == charArray[k]) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (dir == Direction.down)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i = Math.abs((i + 1) % size);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else if (dir == Direction.right)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; j = Math.abs((j + 1) % size);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else if (dir == Direction.left)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; j = Math.abs((j - 1) % size);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // dir==Direction.up    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i = Math.abs((i - 1) % size);&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return isMatch(ar, charArray, i, j, k + 1, dir);   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return false;   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;}&lt;/p&gt;&lt;/code&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-4340718231608918120?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/4340718231608918120/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=4340718231608918120' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4340718231608918120'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4340718231608918120'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/word-in-maze.html' title='Word in a Maze'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-5005469999677001222</id><published>2011-03-14T08:45:00.001-07:00</published><updated>2011-07-11T12:07:19.272-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview '/><title type='text'>Finding max and min</title><content type='html'>&lt;p&gt;&lt;strong&gt;Q: Given an array of n elements, find the min and max in 3n/2 comparisons even in worst case.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;We usually think of the following approach as soon as we see the question&lt;/p&gt;  &lt;p&gt;One scan through the array with two extra variables to store current min and max.   &lt;br /&gt;In this approach the worst case number of comparisons = 2n    &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;Can we change the approach slightly to make sure that even the worst case number of comparisons is 3*n/2&lt;/p&gt;  &lt;p&gt;Following is the approach..&lt;/p&gt;  &lt;p&gt;1) take 2 number at a time .... &lt;/p&gt;  &lt;p&gt;2) compare these 2 numbers .... (1st comparison)&lt;/p&gt;  &lt;p&gt;3) compare the minimum of these two with current minimum&amp;#160; (2nd comparison)&lt;/p&gt;  &lt;p&gt;4) compare the max of these two with current max (3rd comparison)&lt;/p&gt;  &lt;p&gt;as there are (n/2) pairs ... and 3 comparisons per a pair .... total comparisons are 3n/2 .... &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-5005469999677001222?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/5005469999677001222/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=5005469999677001222' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/5005469999677001222'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/5005469999677001222'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/finding-max-and-min.html' title='Finding max and min'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-4232417215517515567</id><published>2011-03-14T08:37:00.001-07:00</published><updated>2011-07-11T12:07:19.196-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview '/><title type='text'>No of occurrences of a number</title><content type='html'>&lt;p&gt;Q: Given a sorted array find number of occurrences of a given number&lt;/p&gt;  &lt;p&gt;If the interviewer has mentioned that the given array is in sorted array then he is expecting the solution to be in log( n ) time.&lt;/p&gt;  &lt;p&gt;We can solve this using binary search. To find the no of occurrences of the given number we can find the first occurrence of the number and the last occurrence of the number and return the difference between them.&lt;/p&gt;  &lt;p&gt;To find the first occurrence of a number we need to check for the following condition. Return the position if any of the following is true.&lt;/p&gt;  &lt;p&gt;(mid == low &amp;amp;&amp;amp; A[mid] == data) || (A[mid] == data &amp;amp;&amp;amp; A[mid-1] &amp;lt; data)&lt;/p&gt;  &lt;p&gt;To find the last occurrence of a number we need to check for the following condition. Return the position if any of the following is true.&lt;/p&gt;  &lt;p&gt;(mid == high &amp;amp;&amp;amp; A[mid] == data) || (A[mid] == data &amp;amp;&amp;amp; A[mid+1] &amp;gt; data)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Working Java Code:&lt;/strong&gt;&lt;/p&gt;   &lt;code style="padding-bottom: 2px; padding-left: 2px; padding-right: 2px; display: block; background: rgb(38,41,46); color: white; font-size: 12px; padding-top: 2px; -moz-background-clip: border; -moz-background-inline-policy: continuous; -moz-background-origin: padding"&gt; &lt;p&gt;public class NoOfOccurences {&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public static int findFirstOccurence(int A[],int low,int high,int data)   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int mid;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; while(low &amp;lt;= high)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; mid = low + (high-low)/2;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(( mid == low &amp;amp;&amp;amp; A[mid] == data) || (A[mid] == data &amp;amp;&amp;amp; A[mid-1] &amp;lt; data))    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return mid;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }else if(A[mid] &amp;gt;= data)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; high = mid-1;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }else    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; low = mid+1;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return -1;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public static int findLastOccurence(int A[],int low,int high,int data)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int mid;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; while(low &amp;lt;= high)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; mid = low + (high-low)/2;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(( mid == high &amp;amp;&amp;amp; A[mid] == data) || (A[mid] == data &amp;amp;&amp;amp; A[mid+1] &amp;gt; data))    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return mid;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }else if(A[mid] &amp;lt;= data)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; low = mid+1;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }else    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; high = mid-1;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return -1;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public static void main(String[] args) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int a[] = {1,2,3,3,3,3,4,5,6};    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int firstCount = NoOfOccurences.findFirstOccurence(a,0,8,3);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int lastCount = NoOfOccurences.findLastOccurence(a,0,8,3);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int noOfOccur = (lastCount - firstCount) +1;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.print(&amp;quot;No of occurences&amp;quot;+noOfOccur);&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;}&lt;/p&gt;&lt;/code&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-4232417215517515567?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/4232417215517515567/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=4232417215517515567' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4232417215517515567'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/4232417215517515567'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/no-of-occurrences-of-number.html' title='No of occurrences of a number'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-1000868672008006748</id><published>2011-03-10T09:17:00.001-08:00</published><updated>2011-07-11T12:07:18.975-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview '/><title type='text'>SubArray With sum zero.</title><content type='html'>&lt;h5&gt;Q: An array contain positive and negative elements, find maximum subarray whose sum is equal to zero. &lt;/h5&gt;  &lt;p&gt;Algorithm:&lt;/p&gt;  &lt;p&gt;Lets take input array as a[]={-3,2,4,-6,-8,10,11}&lt;/p&gt;  &lt;p&gt;Make an array b [] such that b[i]=b[i-1]+a[i]; means b[i] = sum(a[0],..., a[i]);&lt;/p&gt;  &lt;p&gt;So b[]={-3,-1,3,-3,-11,-1,10}&lt;/p&gt;  &lt;p&gt;Now all those pairs at indexes (i,j)in array b[] having equal numbers are the indexes of subarrays from (i+1,j) having sum as zero.&lt;/p&gt;  &lt;p&gt;we have to find some (i, j) so: a[i] + a[i+1] + ... + a[j] = 0. But a[i] + a[i+1] + ... + a[j] = b[j] - b[i-1].   &lt;br /&gt;when creating the map using b values, the key in map is the value from b and the value in map is the index of from b. if a new key exists, this means we found a solution.&lt;/p&gt;  &lt;p&gt;Finding all pairs in O(n) can be done by using map.&lt;/p&gt;  &lt;p&gt;Working JAVA Code    &lt;code style="padding-bottom: 2px; padding-left: 2px; padding-right: 2px; display: block; background: rgb(38,41,46); color: white; font-size: 12px; padding-top: 2px; -moz-background-clip: border; -moz-background-inline-policy: continuous; -moz-background-origin: padding"&gt; &lt;br /&gt;---------------------------    &lt;br /&gt;import java.util.Comparator;    &lt;br /&gt;public class Index implements Comparable{    &lt;br /&gt;Integer start;    &lt;br /&gt;Integer end;    &lt;br /&gt;public Integer getStart() {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; return start;    &lt;br /&gt;}    &lt;br /&gt;public void setStart(Integer start) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; this.start = start;    &lt;br /&gt;}    &lt;br /&gt;public Integer getEnd() {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; return end;    &lt;br /&gt;}    &lt;br /&gt;public void setEnd(Integer end) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; this.end = end;    &lt;br /&gt;}    &lt;br /&gt;public int compareTo(Object index1) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; index1 = (Index) index1;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Index index2 = (Index) this;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Integer&amp;#160; index1Distance = ((Index) index1).getEnd().intValue() -&amp;#160; ((Index) index1).getStart().intValue();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Integer index2Distance = ((Index) index2).getEnd().intValue() -&amp;#160; ((Index) index2).getStart().intValue();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (index1Distance &amp;gt; index2Distance)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return 1;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; else if (index1Distance == index2Distance)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return 0;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; else     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return -1;    &lt;br /&gt;}    &lt;br /&gt;} &lt;/p&gt;  &lt;p&gt;import java.util.HashMap;   &lt;br /&gt;import java.util.Map;    &lt;br /&gt;public class SubarraywithSumZero {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public Index findLongestSubArraywithSumZero(long[] inputArray) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // 1. Generate a cummulative array    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; for (int i=1; i &amp;lt; inputArray.length; i++) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; inputArray[i] += inputArray[ (i-1)];    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //2. Read the array, store it in map    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // For a subraary with 0 there should + and - numbers    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // we need to figure the longest distance indexes which have same numbers    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // since our array is cummulative postives and negatives which sum to 0 will bring the    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // intermittent array back to same number where it started    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // eg intermittent array = -6, -8, -10, -6, -4, 1, 2, 3, 4, 1    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // now from -6 we again reach -6    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // and later from 1 back to 1 which implies these two arrays are summing to 0    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // we just need the longest one    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Map&amp;lt;Integer, Integer&amp;gt; indexMap = new HashMap&amp;lt;Integer, Integer&amp;gt;();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Index maxIndex = null;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; for (int i=0; i &amp;lt; inputArray.length; i++) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Integer index = new Integer(i);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Integer val = new Integer((int) inputArray[i]);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (indexMap.get(val) == null) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; indexMap.put(val, index);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (maxIndex == null) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; maxIndex = new Index();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //collosion found the subarray    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; maxIndex.setStart(indexMap.get(val));    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; maxIndex.setEnd(new Integer(i));    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Index index1 = new Index();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; index1.setStart(indexMap.get(val));    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; index1.setEnd(new Integer(i));    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (maxIndex.compareTo(index1) &amp;lt;= 0) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; maxIndex = index1;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return maxIndex;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public static void main(String[] args) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; long inputArray[] = {3,2,4,-6,-8,10,11};    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SubarraywithSumZero subarraywithSumZero = new SubarraywithSumZero();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Index maxIndex = subarraywithSumZero.findLongestSubArraywithSumZero(inputArray);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(&amp;quot;Longgest Subarray with sum starts at [&amp;quot; + maxIndex.getStart() + &amp;quot; ] and ends at [&amp;quot; + maxIndex.getEnd() + &amp;quot;]&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;}&lt;/p&gt;&lt;/code&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-1000868672008006748?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/1000868672008006748/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=1000868672008006748' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/1000868672008006748'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/1000868672008006748'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/subarray-with-sum-zero.html' title='SubArray With sum zero.'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-8750179837255508944</id><published>2011-03-10T09:05:00.001-08:00</published><updated>2011-07-11T12:07:19.576-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview '/><title type='text'>Finding duplicates in an unsorted array</title><content type='html'>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Q: You are given an array of elements. Some/all of them are duplicates. Find them in 0(n) time and 0(1) space. Property of inputs - Number are in the range of 1..n where n is the limit of the array.&lt;/p&gt;  &lt;p&gt;Algorithm:&lt;/p&gt;  &lt;p&gt;1. Read input from startPos   &lt;br /&gt;2. If input is within the maxRange or it's not in correct place then it's a candidate to be&amp;#160;&amp;#160; replaced else incrementStartPos    &lt;br /&gt;3. finalPosition for input is input[i]    &lt;br /&gt;4. if finalPosition &amp;lt; maxRange then swap input with finalPosition and increment finalPosition with MaxRange    &lt;br /&gt;5. else no need to swap as previous instances of this element are recorded at finalPos so just increment finalPosition with MaxRange to record another instance    &lt;br /&gt;6. if input at startPos is in correct place or reached zero then increment startPos++ till you go through end of Array    &lt;br /&gt;7. Iterate through the array and divide each element with maxRange. The divisor indicates how many times a element was repeated and 0 indicates a missing element&lt;/p&gt;  &lt;p&gt;Eg: if an array had   &lt;br /&gt;(2, 3, 4, 3, 2}    &lt;br /&gt;1. n=5 after step1 we'll get    &lt;br /&gt;{3, 7, 4, 3, 2}    &lt;br /&gt;2. now a[0] = 3 so again in loop we'll get    &lt;br /&gt;{4, 7, 8, 3, 2}    &lt;br /&gt;3. now a[0] = 4 so again in loop we'll get     &lt;br /&gt;{3, 7, 8, 9, 2}    &lt;br /&gt;4. a[0] = 3 so again in loop we'll get { , 7, 13, 9, 2}    &lt;br /&gt;5. since a[0] is empty move to a[1] and in loop we find it's at the right place so move to next, 8, 9 are also in place so we move to last element and have the final array as    &lt;br /&gt;{ , 12, 13, 9, }&lt;/p&gt;  &lt;p&gt;Now to find how many are duplicates divide every element by 5 and take the mod =&amp;gt; m = (a[i] mod n) -1 this gives you how many times an element is repeated.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;code style="padding-bottom: 2px; padding-left: 2px; padding-right: 2px; display: block; background: rgb(38,41,46); color: white; font-size: 12px; padding-top: 2px; -moz-background-clip: border; -moz-background-inline-policy: continuous; -moz-background-origin: padding"&gt; &lt;p&gt;public static void main(String[] args) {&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int[] inputArr1 = {0, 2, 3, 4, 3, 2};   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; findDuplicates(inputArr1, 5);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; printDuplicatesAndMissingElements(inputArr1);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; private static void findDuplicates(int[] inputArr, int maxRange) {   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // for simplicity reasons we will assume nothing is there in 0 index of array     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // and start from 1    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int i = 1;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int maxArrLength = inputArr.length;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; while(true) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if ( i &amp;gt; maxArrLength - 1)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; break;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int finalPos = inputArr[i];    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(&amp;quot; Read :: &amp;quot; + inputArr[i]);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (finalPos &amp;gt; maxRange) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //this element is already in final position skip and move further looking for elements within maxRange    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i++;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; continue;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int finalPosElement = inputArr[inputArr[i]];    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int startPos = i;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int startPosElement = inputArr[i];    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // if startPos element isn't in place and it's within the maxRange..    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // then candidate for swap or reaching final pos...    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (inputArr[startPos] != i ||     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; inputArr[startPos] &amp;lt;= maxRange) {&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (finalPosElement &amp;gt; maxRange) {   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // if finalposElement &amp;gt; maxRange then no swapping required    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // just add maxRange to denote multiple entry    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; inputArr[finalPos] += maxRange;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //make startPosElement as 0    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; inputArr[startPos] = 0;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(&amp;quot;No swapping required incemented &amp;quot; + inputArr[finalPos] );    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //swap    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int temp = startPosElement;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; inputArr[startPos] = finalPosElement;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; inputArr[finalPos] = temp + maxRange;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(&amp;quot;Swapped : &amp;quot; + startPosElement + &amp;quot; with &amp;quot; +finalPosElement);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; printArray(inputArr);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // if the element in current evaluated position isn't still correct repeat the above again    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (inputArr[startPos] == i ||    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; inputArr[startPos] &amp;gt; maxRange) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(&amp;quot; Read :: &amp;quot;+ inputArr[startPos] + &amp;quot; it's already in place&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; inputArr[startPos] += maxRange;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i++;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else if (inputArr[startPos] == 0){    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i++;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; continue;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; continue;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; private static void printArray(int[] inputArr) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; for (int i=1; i &amp;lt; inputArr.length; i++) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.print(inputArr[i] +&amp;quot;,&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(&amp;quot;&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; private static void printDuplicatesAndMissingElements(int[] inputArr) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int maxRange = inputArr.length;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; for (int i=1; i &amp;lt; inputArr.length; i++) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (inputArr[i] == 0) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(&amp;quot; Element [&amp;quot; + i + &amp;quot;] is missing&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int input = inputArr[i];    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int mod = input / maxRange;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if ( mod &amp;gt; 1) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println (&amp;quot; Element [&amp;quot; + i + &amp;quot;] is repeated + [&amp;quot; + mod + &amp;quot;] times&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;}&lt;/p&gt;&lt;/code&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-8750179837255508944?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/8750179837255508944/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=8750179837255508944' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/8750179837255508944'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/8750179837255508944'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/03/finding-duplicates-in-unsorted-array.html' title='Finding duplicates in an unsorted array'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-1383694350354351944</id><published>2011-02-07T09:59:00.001-08:00</published><updated>2011-07-11T12:07:18.998-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview '/><title type='text'>Ternary Search Tree implementation</title><content type='html'>&lt;p&gt;The ternary search tree contains three types of links. First, there are pointers that correspond to links in the corresponding trie, shown as dashed down-arrows. Traversing a down-link corresponds to “matching” the character from which the arrow starts. The left- and right- links are traversed when the current character does not match the desired character at the current position. We take the left-link if the character we are looking for is alphabetically before the character in the current node, and the right-link in the opposite case.&lt;/p&gt;  &lt;p&gt;Ternary tree data structure solves the memory problem of tries which we discussed in the earlier post in a more clever way. To avoid the memory occupied by unnecessary pointers, each trie node is represented as a tree-within-a-tree rather than as an array. Each non-null pointer in the trie node gets its own node in a ternary search tree.&lt;/p&gt;  &lt;p&gt;Each node in a Ternary search tree could be implemented like this in java:&lt;/p&gt;  &lt;p&gt;class TNode   &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; char m_char;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; TNode left, center, right;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; boolean wordEnd;&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public TNode(char ch, boolean wordEnd)   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; m_char = ch;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; wordEnd = wordEnd;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;Here is a Ternary search tree that stores words AB, ABBA, ABCD, and BCD. Nodes that terminate words are marked yellow: &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_Pg0RCzJJYC4/TVAy_kKF-gI/AAAAAAAADdg/fAZqxdaUOSk/s1600-h/image_thumb%5B7%5D%5B2%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image_thumb[7]" border="0" alt="image_thumb[7]" src="http://lh5.ggpht.com/_Pg0RCzJJYC4/TVAzAR3PY_I/AAAAAAAADdk/GHRAY3N0-n8/image_thumb%5B7%5D_thumb.png?imgmax=800" width="255" height="452" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Here is a simple Ternary search tree implementation in java:&lt;/p&gt;   &lt;code style="padding-bottom: 2px; padding-left: 2px; padding-right: 2px; display: block; background: rgb(38,41,46); color: white; font-size: 12px; padding-top: 2px; -moz-background-clip: border; -moz-background-inline-policy: continuous; -moz-background-origin: padding"&gt; &lt;p&gt;   &lt;br /&gt;public class TernarySearchTree {&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; TNode root = null;   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public TernarySearchTree()    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.root = null;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; private void insert(String key, int pos, TNode node)   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; char s[] = key.toCharArray();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (node == null) { node = new TNode(s[pos], false); }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (s[pos] &amp;lt; node.m_char) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; insert(key, pos, node.left);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else if (s[pos] &amp;gt; node.m_char) {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; insert(key, pos, node.right);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (pos + 1 == key.length()) { node.wordEnd = true; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else { insert(key, pos + 1, node.center); }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public void insert(String s)   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (s == null || s == &amp;quot;&amp;quot;) throw new IllegalArgumentException();&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; insert(s, 0, this.root);   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public boolean containsKey(String key)   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (key == null || key == &amp;quot;&amp;quot;) throw new IllegalArgumentException();&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int pos = 0;   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TNode node = this.root;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; char s[] = key.toCharArray();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; while (node != null)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (s[pos] &amp;lt; node.m_char) { node = node.left; }   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else if (s[pos] &amp;gt; node.m_char) { node = node.right; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (++pos == key.length()) return node.wordEnd;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; node = node.center;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return false;   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public static void main(String[] args)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TernarySearchTree tree = new TernarySearchTree();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tree.insert(&amp;quot;AB&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tree.insert(&amp;quot;ABBA&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tree.insert(&amp;quot;ABCD&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tree.insert(&amp;quot;BCD&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; boolean found = tree.containsKey(&amp;quot;AB&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(found)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(&amp;quot;AB is found in the tree&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(&amp;quot;AB is not found&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; found = tree.containsKey(&amp;quot;ABCD&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(found)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(&amp;quot;ABCD is found in the tree&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(&amp;quot;ABCD is not found&amp;quot;);&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; }   &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;class TNode   &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; char m_char;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; TNode left, center, right;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; boolean wordEnd;&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public TNode(char ch, boolean wordEnd)   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; m_char = ch;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; wordEnd = wordEnd;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;}&lt;/p&gt;&lt;/code&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-1383694350354351944?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/1383694350354351944/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=1383694350354351944' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/1383694350354351944'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/1383694350354351944'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2011/02/ternary-search-tree-implementation.html' title='Ternary Search Tree implementation'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_Pg0RCzJJYC4/TVAzAR3PY_I/AAAAAAAADdk/GHRAY3N0-n8/s72-c/image_thumb%5B7%5D_thumb.png?imgmax=800' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-6903348180790573118</id><published>2011-02-07T09:00:00.000-08:00</published><updated>2011-07-11T12:07:19.355-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Interview '/><title type='text'>Trie implementation</title><content type='html'>&lt;p&gt;A trie is a tree-like data structure in which each node contains an array of pointers, one pointer for each character in the alphabet. Starting at the root node, we can trace a word by following pointers corresponding to the letters in the target word. &lt;/p&gt;  &lt;p&gt;Some of the uses of this Trie data structure is&lt;/p&gt;  &lt;p&gt;1) Auto completion of the words or sentences like in Google Suggest.&lt;/p&gt;  &lt;p&gt;2) Dictionary etc..&lt;/p&gt;  &lt;p&gt;Each node in a trie could be implemented like this in java:&lt;/p&gt;  &lt;code style="padding-bottom: 2px; padding-left: 2px; padding-right: 2px; display: block; background: rgb(38,41,46); color: white; font-size: 12px; padding-top: 2px; -moz-background-clip: border; -moz-background-inline-policy: continuous; -moz-background-origin: padding"&gt;  &lt;p&gt;class Node {   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Node[] children;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; boolean isKey;&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public Node() {   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; isKey = false;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; children = new Node[26];    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public Node(boolean key) {   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; isKey = key;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; children = new Node[26];    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;}&lt;/p&gt; &lt;/code&gt; &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Here is a trie that stores words AB, ABBA, ABCD, and BCD. Nodes that terminate words are marked yellow: &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/TVAk4eaNcyI/AAAAAAAADdQ/fl_PFIX7rRk/s1600-h/image%5B4%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_Pg0RCzJJYC4/TVAk5KM_ZrI/AAAAAAAADdU/DHTwD2dYX74/image_thumb%5B2%5D.png?imgmax=800" width="420" height="409" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Here is a simple Trie search tree implementation in java:&lt;/p&gt;  &lt;code style="padding-bottom: 2px; padding-left: 2px; padding-right: 2px; display: block; background: rgb(38,41,46); color: white; font-size: 12px; padding-top: 2px; -moz-background-clip: border; -moz-background-inline-policy: continuous; -moz-background-origin: padding"&gt;  &lt;p&gt;public class Trie {&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; Node root;&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public Trie() {   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.root = new Node();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; /**   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * Method to insert a string to Node and its children    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; *     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * @param key    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; *&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; the string to insert (the string is assumed to be uppercase)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * @return true if the node or one of its children is changed, false    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; *&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; otherwise    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; */    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public boolean insert(String key) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return root.insert(key.toUpperCase());    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; /**   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * Returns whether key is a valid prefix for certain key in this trie. For    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * example: if key &amp;quot;hello&amp;quot; is in this trie, tests with all prefixes &amp;quot;hel&amp;quot;,    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * &amp;quot;hell&amp;quot;, &amp;quot;hello&amp;quot; return true    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; *     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * @param prefix    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; *&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; the prefix to check    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * @return true if the prefix is valid, false otherwise    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; */    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public boolean containPrefix(String prefix) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return root.containPrefix(prefix.toUpperCase());    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; /**   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * Returns whether key is a valid key in this trie. For example: if key    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * &amp;quot;hello&amp;quot; is in this trie, tests with all prefixes &amp;quot;hel&amp;quot;, &amp;quot;hell&amp;quot; return    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * false    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; *     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * @param key    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; *&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; the key to check    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * @return true if the key is valid, false otherwise    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; */    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public boolean containKey(String key) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return root.containKey(key.toUpperCase());    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public static void main(String[] args)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Trie trie = new Trie();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; trie.insert(&amp;quot;AB&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; trie.insert(&amp;quot;ABBA&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; trie.insert(&amp;quot;ABCD&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; trie.insert(&amp;quot;BCD&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; boolean found = trie.containKey(&amp;quot;AB&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(found)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(&amp;quot;AB is found in the trie&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(&amp;quot;AB is not found&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; found = trie.containPrefix(&amp;quot;ABC&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(found)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(&amp;quot;ABC prefix is found in the trie&amp;quot;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.out.println(&amp;quot;ABC prefix is not found&amp;quot;);&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; }   &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;class Node {   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Node[] children;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; boolean isKey;&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public Node() {   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; isKey = false;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; children = new Node[26];    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public Node(boolean key) {   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; isKey = key;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; children = new Node[26];    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; /**   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * Method to insert a string to Node and its children    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; *     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * @param key    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; *&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; the string to insert (the string is assumed to be uppercase)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * @return true if the node or one of its children is changed, false    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; *&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; otherwise    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; */    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public boolean insert(String key) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // If the key is empty, this node is a key    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (key.length() == 0) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (isKey)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return false;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; isKey = true;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return true;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } else {// otherwise, insert in one of its child&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int childNodePosition = key.charAt(0) - 'A';   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (children[childNodePosition] == null) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; children[childNodePosition] = new Node();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; children[childNodePosition].insert(key.substring(1));    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return true;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } else {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return children[childNodePosition].insert(key.substring(1));    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; /**   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * Returns whether key is a valid prefix for certain key in this trie. For    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * example: if key &amp;quot;hello&amp;quot; is in this trie, tests with all prefixes &amp;quot;hel&amp;quot;,    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * &amp;quot;hell&amp;quot;, &amp;quot;hello&amp;quot; return true    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; *     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * @param prefix    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; *&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; the prefix to check    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * @return true if the prefix is valid, false otherwise    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; */    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public boolean containPrefix(String prefix) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // If the prefix is empty, return true    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (prefix.length() == 0) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return true;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } else {// otherwise, check in one of its child    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int childNodePosition = prefix.charAt(0) - 'A';    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return children[childNodePosition] != null    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;amp;&amp;amp; children[childNodePosition].containPrefix(prefix    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .substring(1));    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; /**   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * Returns whether key is a valid key in this trie. For example: if key    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * &amp;quot;hello&amp;quot; is in this trie, tests with all prefixes &amp;quot;hel&amp;quot;, &amp;quot;hell&amp;quot; return    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * false    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; *     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * @param key    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; *&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; the key to check    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * @return true if the key is valid, false otherwise    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; */    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public boolean containKey(String key) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // If the prefix is empty, return true    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (key.length() == 0) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return isKey;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } else {// otherwise, check in one of its child    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int childNodePosition = key.charAt(0) - 'A';    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return children[childNodePosition] != null    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;amp;&amp;amp; children[childNodePosition].containKey(key.substring(1));    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public boolean isKey() {   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return isKey;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public void setKey(boolean key) {   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; isKey = key;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;}    &lt;/p&gt;&lt;/code&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-6903348180790573118?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/6903348180790573118/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=6903348180790573118' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/6903348180790573118'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/6903348180790573118'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2009/12/flex-email-client-for-google-app-engine_13.html' title='Trie implementation'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_Pg0RCzJJYC4/TVAk5KM_ZrI/AAAAAAAADdU/DHTwD2dYX74/s72-c/image_thumb%5B2%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-2316874417291502767</id><published>2010-02-17T10:56:00.001-08:00</published><updated>2011-07-11T12:01:17.693-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='AIR'/><category scheme='http://www.blogger.com/atom/ns#' term='Actionscript'/><category scheme='http://www.blogger.com/atom/ns#' term='Flex'/><title type='text'>360 degree Viewer</title><content type='html'>&lt;p&gt;Sample Application showing the 360 degree view of an image using paper vision 3d.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://demo.premapix.net//demos/360viewer/Flex360.html"&gt;Demo here&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Pg0RCzJJYC4/S3w7zWVd4pI/AAAAAAAACuo/4-D5jWKAKpE/s1600-h/360view%5B3%5D.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="360view" border="0" alt="360view" src="http://lh3.ggpht.com/_Pg0RCzJJYC4/S3w70QHRYxI/AAAAAAAACus/FnYoWgDYjoY/360view_thumb%5B1%5D.jpg?imgmax=800" width="505" height="410" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Source code&lt;/p&gt; &lt;code style="padding-bottom: 2px; padding-left: 2px; padding-right: 2px; display: block; background: #26292e; color: #fff; font-size: 12px; padding-top: 2px"&gt; &lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;   &lt;br /&gt;&amp;lt;mx:Application xmlns:mx=&amp;quot;&lt;a href="http://www.adobe.com/2006/mxml&amp;quot;"&gt;http://www.adobe.com/2006/mxml&amp;quot;&lt;/a&gt; layout=&amp;quot;absolute&amp;quot;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; xmlns:view=&amp;quot;org.papervision3d.view.*&amp;quot; creationComplete=&amp;quot;init();&amp;quot;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; width=&amp;quot;622&amp;quot; height=&amp;quot;504&amp;quot;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; backgroundGradientAlphas=&amp;quot;[1.0, 1.0]&amp;quot; backgroundGradientColors=&amp;quot;[#FFFFFF, #DEDEDE]&amp;quot;&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;mx:Script&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;![CDATA[    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; import mx.events.ItemClickEvent;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; import org.papervision3d.objects.primitives.Cylinder;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; import org.papervision3d.materials.BitmapFileMaterial;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; private var cylinder:Cylinder;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Initialization function    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; private function init():void{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // BasicView added to the Stage    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; myUI.addChild( bv );    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; bv.cameraAsCamera3D.x = 0;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; bv.cameraAsCamera3D.z = 0;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; bv.cameraAsCamera3D.y = 0;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Create object    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var mat:BitmapFileMaterial = new BitmapFileMaterial(&amp;quot;DSC00020.jpg&amp;quot;,true);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; mat.doubleSided = true;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; mat.precise = true;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; mat.smooth = true;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cylinder = new Cylinder( mat , 80 , 100 , 100 , 100 , 80 , false , false );    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; bv.scene.addChild( cylinder );    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Rendering bv.startRendering();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.addEventListener(Event.ENTER_FRAME , onTimer );    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Rendering    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; private function onTimer( e:Event ):void{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; bv.renderer.renderScene( bv.scene , bv.camera , bv.viewport );    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cylinder.rotationY += (myUI.width - myUI.mouseX )/ 100;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; private function onClick(e:ItemClickEvent):void{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cylinder.rotationY = e.item.angle;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ]]&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/mx:Script&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;view:BasicView id=&amp;quot;bv&amp;quot;/&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;mx:UIComponent id=&amp;quot;myUI&amp;quot; width=&amp;quot;300&amp;quot; height=&amp;quot;300&amp;quot;/&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;mx:LinkBar id=&amp;quot;link&amp;quot; x=&amp;quot;263.5&amp;quot; y=&amp;quot;6&amp;quot; labelField=&amp;quot;label&amp;quot; itemClick=&amp;quot;onClick(event)&amp;quot;&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;mx:Object label=&amp;quot;a&amp;quot; angle=&amp;quot;0&amp;quot;/&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;mx:Object label=&amp;quot;b&amp;quot; angle=&amp;quot;90&amp;quot;/&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;mx:Object label=&amp;quot;c&amp;quot; angle=&amp;quot;180&amp;quot;/&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;mx:Object label=&amp;quot;d&amp;quot; angle=&amp;quot;270&amp;quot;/&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/mx:LinkBar&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;mx:Label x=&amp;quot;280.5&amp;quot; y=&amp;quot;476&amp;quot; text=&amp;quot;360 Video&amp;quot;/&amp;gt;    &lt;br /&gt;&amp;lt;/mx:Application&amp;gt;&lt;/p&gt;&lt;/code&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4640943833280351629-2316874417291502767?l=flexaired.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexaired.blogspot.com/feeds/2316874417291502767/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4640943833280351629&amp;postID=2316874417291502767' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/2316874417291502767'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4640943833280351629/posts/default/2316874417291502767'/><link rel='alternate' type='text/html' href='http://flexaired.blogspot.com/2010/02/360-degree-viewer.html' title='360 degree Viewer'/><author><name>Ram</name><uri>http://www.blogger.com/profile/14216463582774488342</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/_Pg0RCzJJYC4/S3w70QHRYxI/AAAAAAAACus/FnYoWgDYjoY/s72-c/360view_thumb%5B1%5D.jpg?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4640943833280351629.post-8962417871740570258</id><published>2009-12-14T10:52:00.001-08:00</published><updated>2011-07-11T12:01:18.205-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='AIR'/><category scheme='http://www.blogger.com/atom/ns#' term='Actionscript'/><category scheme='http://www.blogger.com/atom/ns#' term='Flex'/><title type='text'>Crud Operations with Java Web Services, Flex and Flash Builder</title><content type='html'>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://java.sun.com/javase/downloads/ea.jsp"&gt;Java SE 6&lt;/a&gt; has built-in support for web services thanks to &lt;a href="http://java.sun.com/webservices/jaxws/index.jsp"&gt;JAX-WS 2.0&lt;/a&gt;. Using the DCD features in Flash Builder 4, a web service can be easily accessed.&amp;#160; We will build an end-to-end simple CRUD application in Java and Flex 4, where the Java methods are exposed as web service operations. We will take an Employee Class as an example and try to do the crud operations on it, using the Flex UI and Flash Builder 4 for deploying the web services and using it.&lt;/p&gt;  &lt;p&gt;Create an Employee table as shown below. &lt;/p&gt; &lt;code style="padding-bottom: 2px; padding-left: 2px; padding-right: 2px; display: block; background: #26292e; color: #fff; font-size: 12px; padding-top: 2px"&gt;   &lt;p&gt;CREATE TABLE `employee` (      &lt;br /&gt;&amp;#160; `emp_id` int(11) NOT NULL AUTO_INCREMENT,       &lt;br /&gt;&amp;#160; `emp_name` varchar(50) NOT NULL,       &lt;br /&gt;&amp;#160; `dept` varchar(50) NOT NULL,       &lt;br /&gt;&amp;#160; `emp_sal` int(11) NOT NULL,       &lt;br /&gt;&amp;#160; `manager` varchar(50) NOT NULL,       &lt;br /&gt;&amp;#160; PRIMARY KEY (`emp_id`)       &lt;br /&gt;) &lt;/p&gt; &lt;/code&gt;  &lt;p&gt;The Employee POJO is shown below.&lt;/p&gt; &lt;code style="padding-bottom: 2px; padding-left: 2px; padding-right: 2px; display: block; background: #26292e; color: #fff; font-size: 12px; padding-top: 2px"&gt;   &lt;p&gt;package com.adobe.objects; &lt;/p&gt;    &lt;p&gt;public class SimpleEmployee {      &lt;br /&gt;&amp;#160;&amp;#160; private int empId;       &lt;br /&gt;&amp;#160;&amp;#160; private String empName;       &lt;br /&gt;&amp;#160;&amp;#160; private String dept;       &lt;br /&gt;&amp;#160;&amp;#160; private int empSal;       &lt;br /&gt;&amp;#160;&amp;#160; private String manager;       &lt;br /&gt;&amp;#160;&amp;#160; public SimpleEmployee(int id, String name, String dep, int sal,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String mgr) {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.empId = id;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.empName = name;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.dept = dep;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.empSal = sal;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.manager = mgr;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public SimpleEmployee() {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // TODO Auto-generated constructor stub       &lt;br /&gt;}       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int getEmpId() {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return empId;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public void setEmpId(int id) {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.empId = id;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public String getEmpName() {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return empName;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public void setEmpName(String name) {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.empName = name;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public String getDept() {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return dept;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public void setDept(String dep) {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.dept = dep;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int getEmpSal() {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return empSal;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public void setEmpSal(int sal) {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.empSal = sal;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public String getManager() {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return manager;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public void setManager(String mgr) {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.manager = mgr;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p&gt;}&lt;/p&gt; &lt;/code&gt;  &lt;p&gt;The Employee Service with all the operations exposed is as shown below.&amp;#160; &lt;/p&gt;  &lt;p&gt;To expose your Java class as a web service, simply: &lt;/p&gt;  &lt;p&gt;1) Annotate your Java class with @WebService, @WebMethod. &lt;/p&gt;  &lt;p&gt;2) Use the Endpoint class in main()&lt;/p&gt;  &lt;p&gt;EmployeeService.Java&lt;/p&gt; &lt;code style="padding-bottom: 2px; padding-left: 2px; padding-right: 2px; display: block; background: #26292e; color: #fff; font-size: 12px; padding-top: 2px"&gt;   &lt;p&gt;package com.adobe.services; &lt;/p&gt;    &lt;p&gt;import java.sql.Connection;      &lt;br /&gt;import java.sql.DriverManager;       &lt;br /&gt;import java.sql.PreparedStatement;       &lt;br /&gt;import java.sql.ResultSet;       &lt;br /&gt;import java.sql.SQLException;       &lt;br /&gt;import java.util.ArrayList;       &lt;br /&gt;import javax.jws.WebService;       &lt;br /&gt;import javax.jws.WebMethod;       &lt;br /&gt;import javax.jws.soap.SOAPBinding;       &lt;br /&gt;import javax.xml.ws.Endpoint; &lt;/p&gt;    &lt;p&gt;import com.adobe.objects.SimpleEmployee;      &lt;br /&gt;import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException; &lt;/p&gt;    &lt;p&gt;@WebService      &lt;br /&gt;@SOAPBinding(style = SOAPBinding.Style.RPC) &lt;/p&gt;    &lt;p&gt;public class SimpleEmployeeService      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public SimpleEmployeeService()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; @WebMethod       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int addEmployee(SimpleEmployee emp)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int createdEmpId= 0;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Connection connection = null; &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(emp == null)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new IllegalArgumentException(&amp;quot;Employee object is invalid&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int empId = emp.getEmpId();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String empName = emp.getEmpName();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String dept = emp.getDept();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int sal = emp.getEmpSal();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String manager = emp.getManager();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(empName == null ||       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dept == null ||       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; manager == null)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new IllegalArgumentException(&amp;quot;employee object is invalid&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; try       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection = getConnection();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PreparedStatement statement =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection.prepareStatement(&amp;quot;INSERT INTO employee (&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;emp_id, emp_name, dept,emp_sal,manager) &amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;VALUES(?,?,?,?,?)&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; statement.setInt(1, empId);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; statement.setString(2, empName);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; statement.setString(3, dept);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; statement.setInt(4, sal);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; statement.setString(5,manager);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int rowCount = statement.executeUpdate(); &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(rowCount == 1)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ResultSet rs = statement.getGeneratedKeys();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(rs.next())       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; createdEmpId = rs.getInt(1);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; catch(MySQLIntegrityConstraintViolationException multipleEntryException)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //duplicate entry found       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new RuntimeException(&amp;quot;Employee entry already exists&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; catch(Exception e)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new RuntimeException(e);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; finally       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; try       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(connection != null)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection.close();&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }catch(SQLException sqe)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new RuntimeException(sqe);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return createdEmpId;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; @WebMethod &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public void updateEmployee(SimpleEmployee emp)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Connection connection = null; &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(emp == null)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new IllegalArgumentException(&amp;quot;Employee object is invalid&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int empId = emp.getEmpId();      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String empName = emp.getEmpName();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String dept = emp.getDept();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int sal = emp.getEmpSal();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String manager = emp.getManager();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(empName == null ||       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dept == null ||       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; empId &amp;lt;= 0 ||       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; manager == null)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new IllegalArgumentException(&amp;quot;Employee object is invalid&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //update customer address       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; try       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection = getConnection();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PreparedStatement statement =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection.prepareStatement(&amp;quot;UPDATE employee SET &amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;emp_name = ?,&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;dept = ?, &amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;manager = ?,&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;emp_sal = ? &amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;WHERE emp_id = ?&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; statement.setString(1, empName);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; statement.setString(2, dept);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; statement.setString(3, manager);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; statement.setInt(4, sal);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; statement.setInt(5, empId);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; statement.executeUpdate(); &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; catch(MySQLIntegrityConstraintViolationException multipleEntryException)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //duplicate entry found       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new RuntimeException(&amp;quot;Employee entry already exists&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; catch(Exception e)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new RuntimeException(e);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; finally       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; try       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(connection != null)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection.close();&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }catch(SQLException sqe)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new RuntimeException(sqe);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; @WebMethod       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public boolean deleteEmployee(SimpleEmployee emp)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; boolean result = false;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Connection connection = null;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(emp == null)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new IllegalArgumentException(&amp;quot;Employee instance&amp;#160; is invalid&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int empId = emp.getEmpId(); &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(empId &amp;lt;= 0)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new IllegalArgumentException(&amp;quot;Employee id is invalid&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; try      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection = getConnection();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PreparedStatement employeeStatement; &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; employeeStatement = connection.prepareStatement(&amp;quot;DELETE FROM employee WHERE emp_id = ?&amp;quot;);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; employeeStatement.setInt(1, empId);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int rowCount = employeeStatement.executeUpdate();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(rowCount == 1)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; result = true;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; catch(Exception e)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new RuntimeException(e);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; finally       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; try       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(connection != null)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection.close();&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }catch(SQLException sqe)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new RuntimeException(sqe);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return result;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; @WebMethod       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public SimpleEmployee getEmployee(int empId)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SimpleEmployee result = null;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Connection connection = null; &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; try      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection = getConnection();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PreparedStatement statement =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection.prepareStatement(&amp;quot;SELECT * FROM employee WHERE emp_id = ?&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; statement.setInt(1, empId);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ResultSet rs = statement.executeQuery();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ArrayList&amp;lt;SimpleEmployee&amp;gt; emps = createEmployeesFromRS(rs);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(emps != null &amp;amp;&amp;amp; emps.size() &amp;gt; 0 )       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; result = emps.get(0);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; catch(Exception e)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new RuntimeException(e);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; finally       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; try       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(connection != null)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection.close();&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }catch(SQLException sqe)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new RuntimeException(sqe);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return result;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int getEmployeesCount()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int result = 0;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SimpleEmployee[] emps = getAllEmployees();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(emps != null)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; result = emps.length;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return result;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public ArrayList&amp;lt;SimpleEmployee&amp;gt; getEmployeePaged(int startIndex, int numberOfRows)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ArrayList&amp;lt;SimpleEmployee&amp;gt; employees = new ArrayList&amp;lt;SimpleEmployee&amp;gt;();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Connection connection = null; &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; try      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection = getConnection();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PreparedStatement statement =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection.prepareStatement(&amp;quot;SELECT * FROM employees LIMIT ?, ?&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; statement.setInt(1, startIndex);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; statement.setInt(2, numberOfRows);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ResultSet rs = statement.executeQuery();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; employees = createEmployeesFromRS(rs);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; catch(Exception e)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new RuntimeException(e);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; finally       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; try       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(connection != null)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection.close();&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }catch(SQLException sqe)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new RuntimeException(sqe);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return employees;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; @WebMethod      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public SimpleEmployee[] getAllEmployees()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ArrayList&amp;lt;SimpleEmployee&amp;gt; employees = new ArrayList&amp;lt;SimpleEmployee&amp;gt;();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Connection connection = null; &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; try      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection = getConnection();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PreparedStatement statement =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection.prepareStatement(&amp;quot;SELECT * FROM employee&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ResultSet rs = statement.executeQuery();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; employees = createEmployeesFromRS(rs);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; catch(Exception e)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new RuntimeException(e);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; finally       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; try       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(connection != null)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection.close();&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }catch(SQLException sqe)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new RuntimeException(sqe);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int len = employees.size();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SimpleEmployee[] emps = new SimpleEmployee[len];       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; employees.toArray(emps);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return emps;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; private ArrayList&amp;lt;SimpleEmployee&amp;gt; createEmployeesFromRS(ResultSet rs)throws Exception      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ArrayList&amp;lt;SimpleEmployee&amp;gt; emps = new ArrayList&amp;lt;SimpleEmployee&amp;gt;();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SimpleEmployee emp;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; while(rs.next())       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; emp = new SimpleEmployee();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; emp.setEmpId(rs.getInt(&amp;quot;emp_id&amp;quot;));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; emp.setEmpName(rs.getString(&amp;quot;emp_name&amp;quot;));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; emp.setDept(rs.getString(&amp;quot;dept&amp;quot;));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; emp.setEmpSal(rs.getInt(&amp;quot;emp_sal&amp;quot;));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; emp.setManager(rs.getString(&amp;quot;manager&amp;quot;));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; emps.add(emp);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return emps;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; private Connection getConnection()throws Exception       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Connection connection = null;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; try       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Class.forName(&amp;quot;org.gjt.mm.mysql.Driver&amp;quot;).newInstance();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; catch (Exception e)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw e;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; try      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection = DriverManager.getConnection(       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;jdbc:mysql://localhost/&amp;quot; + &amp;quot;empdb&amp;quot;, &amp;quot;root&amp;quot;,&amp;quot;&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; catch (SQLException sqlException)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw sqlException;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return connection;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public static void main(String[] args)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // create and publish an endpoint       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SimpleEmployeeService empService = new SimpleEmployeeService();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Endpoint endpoint = Endpoint.publish(&amp;quot;&lt;a href="http://localhost:8080/employee&amp;quot;"&gt;http://localhost:8080/employee&amp;quot;&lt;/a&gt;, empService);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;    &lt;p&gt;}&lt;/p&gt; &lt;/code&gt;  &lt;p&gt;Create a directory called generated and run &lt;code&gt;apt&lt;/code&gt; (Annotation Processing Tool): &lt;/p&gt;  &lt;pre&gt;apt -cp .;mysql.jar -d generated com\adobe\services\EmployeeService.java&lt;br /&gt;Run your class: java -cp generated;mysql.jar com.adobe.services.EmployeeService&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;That's it, you have a web server running on localhost:8080 and you can access the WSDL by navigating to &lt;code&gt;&lt;a href="http://localhost:8080/employee?wsdl"&gt;http://localhost:8080/employee?wsdl&lt;/a&gt;&lt;/code&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&
