<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>techpuzzl</title>
	<atom:link href="http://techpuzzl.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://techpuzzl.wordpress.com</link>
	<description>Technology, Puzzles, everything you want for an interview.</description>
	<lastBuildDate>Sat, 10 Dec 2011 06:21:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='techpuzzl.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>techpuzzl</title>
		<link>http://techpuzzl.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://techpuzzl.wordpress.com/osd.xml" title="techpuzzl" />
	<atom:link rel='hub' href='http://techpuzzl.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Longest increasing subsequence</title>
		<link>http://techpuzzl.wordpress.com/2011/12/10/longest-increasing-subsequence/</link>
		<comments>http://techpuzzl.wordpress.com/2011/12/10/longest-increasing-subsequence/#comments</comments>
		<pubDate>Sat, 10 Dec 2011 05:52:51 +0000</pubDate>
		<dc:creator>Upnishad</dc:creator>
				<category><![CDATA[Amazon]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[Interview Question]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Longest increasing subsequence]]></category>

		<guid isPermaLink="false">http://techpuzzl.wordpress.com/?p=284</guid>
		<description><![CDATA[Longest Increasing Subsequence has been a classic Dynamic Programming problem. O(N^2) has been around for a while but more interesting is the following O(n log n) solution. For example : We have sequence : 1,8,2,7,3,6,4,5 which has the longest increasing &#8230; <a href="http://techpuzzl.wordpress.com/2011/12/10/longest-increasing-subsequence/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=284&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a title="Longest Increasing Subsequence" href="http://en.wikipedia.org/wiki/Longest_increasing_subsequence">Longest Increasing Subsequence </a>has been a classic Dynamic Programming problem. O(N^2) has been around for a while but more interesting is the following O(n log n) solution.</p>
<p>For example : We have sequence : 1,8,2,7,3,6,4,5 which has the longest increasing subsequence : 1,2,3,4,5. How to arrive at this in O(n log n) ?</p>
<p><span id="more-284"></span><br />
The O(nlogn) algorithm works as follows:<br />
Let the array be A[1...n] i.e elements are A[1], A[2], .. , A[n], assume all are distinct.<br />
Idea is to<span style="text-decoration:underline;"> maintain for each k, the smallest element A[ik] such that the maximum length monotone subsequence that ends at A[ik] is of length k</span>.</p>
<p>Lets walk through the solution.</p>
<ol>
<li>Start off with A[1] = 1. For this array we get i1 = 1. Now add A[2]=8 to the list.</li>
<ul>
<li>Since A[2]&gt;A[1], we get i2=2. Now the list contains [1,8]</li>
</ul>
<li>Try adding A[3]=2 to the list.</li>
<ul>
<li>Since 1&lt;2&lt;8, we set i2=3. List now has [1,2]</li>
</ul>
<li>Try adding A[4]=7 to the list.</li>
<ul>
<li>Since 7&gt;2, we set i3=4. List now has [1,2,7]</li>
</ul>
<li>Try adding A[5]=3 to the list.</li>
<ul>
<li>Since 2&lt;3&lt;7, we set i3=5. List now has [1,2,3]</li>
</ul>
<li>Try adding A[6]=6 to the list.</li>
<ul>
<li>Since 6&gt;3, we set i4=6. List now has [1,2,3,6]</li>
</ul>
<li>Try adding A[7]=4 to the list.</li>
<ul>
<li>Since 3&lt;4&lt;6, we set i4=7. List now has [1,2,3,4]</li>
</ul>
<li>Try adding A[8]=5 to the list.</li>
<ul>
<li>Since 5&gt;4, we set i5=8. List now has [1,2,3,4,5]</li>
</ul>
</ol>
<p><strong>Note</strong> that in this case walking the tree gave you the right sequence, but that need not be true for all sequences. The algorithm I gave gives the length of the longest sequence, you will have to modify it to get the exact sequence.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/techpuzzl.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/techpuzzl.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/techpuzzl.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/techpuzzl.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/techpuzzl.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/techpuzzl.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/techpuzzl.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/techpuzzl.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/techpuzzl.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/techpuzzl.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/techpuzzl.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/techpuzzl.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/techpuzzl.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/techpuzzl.wordpress.com/284/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=284&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://techpuzzl.wordpress.com/2011/12/10/longest-increasing-subsequence/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0d95fe3795b73e39db2c0a4233e7f850?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">upnishad</media:title>
		</media:content>
	</item>
		<item>
		<title>Reverse Linked Lists &#8211; phew!</title>
		<link>http://techpuzzl.wordpress.com/2011/06/07/reverse-linked-lists-phew/</link>
		<comments>http://techpuzzl.wordpress.com/2011/06/07/reverse-linked-lists-phew/#comments</comments>
		<pubDate>Tue, 07 Jun 2011 10:56:56 +0000</pubDate>
		<dc:creator>Upnishad</dc:creator>
				<category><![CDATA[Interview Question]]></category>
		<category><![CDATA[Linked List]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://techpuzzl.wordpress.com/?p=282</guid>
		<description><![CDATA[I am not really a fan of this question but it seems to be quite a favorite question during technical interview. So, lets discuss reversing a linked-list problem at length. Basically there are two methods of reversing a linked list &#8230; <a href="http://techpuzzl.wordpress.com/2011/06/07/reverse-linked-lists-phew/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=282&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I am not really a fan of this question but it seems to be quite a favorite question during technical interview. So, lets discuss reversing a linked-list problem at length.<br />
Basically there are two methods of reversing a linked list &#8211; iterative and recursive. Lets discuss the iterative method :<br />
<strong>Logic:</strong> Iterate trough the linked list. In loop, change next to prev, prev to current and current to next.</p>
<pre><code>
private static void reverseIterative()
        {
                Node prev=null;
                Node current= head;
                Node next=null;
                while(current != null)
                {
                        next = current.next;
                        current.next = prev;
                        prev = current;
                        current = next;
                }
                head = prev;
        }
</code></pre>
<p>This is simple and has the following complexity :<br />
Time Complexity: O(n)<br />
Space Complexity: O(1)</p>
<p>The recursive method needs a bit of thinking &#8211; lets visualize our problem first.<br />
<img class="aligncenter" title="ReverseList" src="http://geeksforgeeks.org/wp-content/uploads/2009/07/Linked-List-Rverse.gif" alt="" width="400" height="420" /></p>
<p>The logic looks like this :<br />
1) Divide the list in two parts &#8211; first node and rest of the linked list.<br />
2) Call reverse for the rest of the linked list.<br />
3) Link rest to first.<br />
4) Fix head pointer</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/techpuzzl.wordpress.com/282/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/techpuzzl.wordpress.com/282/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/techpuzzl.wordpress.com/282/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/techpuzzl.wordpress.com/282/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/techpuzzl.wordpress.com/282/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/techpuzzl.wordpress.com/282/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/techpuzzl.wordpress.com/282/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/techpuzzl.wordpress.com/282/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/techpuzzl.wordpress.com/282/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/techpuzzl.wordpress.com/282/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/techpuzzl.wordpress.com/282/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/techpuzzl.wordpress.com/282/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/techpuzzl.wordpress.com/282/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/techpuzzl.wordpress.com/282/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=282&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://techpuzzl.wordpress.com/2011/06/07/reverse-linked-lists-phew/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0d95fe3795b73e39db2c0a4233e7f850?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">upnishad</media:title>
		</media:content>

		<media:content url="http://geeksforgeeks.org/wp-content/uploads/2009/07/Linked-List-Rverse.gif" medium="image">
			<media:title type="html">ReverseList</media:title>
		</media:content>
	</item>
		<item>
		<title>Reverse a Linked List in groups of given size</title>
		<link>http://techpuzzl.wordpress.com/2011/06/07/reverse-a-linked-list-in-groups-of-given-size/</link>
		<comments>http://techpuzzl.wordpress.com/2011/06/07/reverse-a-linked-list-in-groups-of-given-size/#comments</comments>
		<pubDate>Tue, 07 Jun 2011 09:51:42 +0000</pubDate>
		<dc:creator>Upnishad</dc:creator>
				<category><![CDATA[Amazon]]></category>
		<category><![CDATA[Linked List]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[group]]></category>
		<category><![CDATA[pair]]></category>
		<category><![CDATA[Reverse linked list]]></category>

		<guid isPermaLink="false">http://techpuzzl.wordpress.com/?p=280</guid>
		<description><![CDATA[Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Example: Inputs: 1-&#62;2-&#62;3-&#62;4-&#62;5-&#62;6-&#62;7-&#62;8-&#62;NULL and k = 3 Output: 3-&#62;2-&#62;1-&#62;6-&#62;5-&#62;4-&#62;8-&#62;7-&#62;NULL. Inputs: 1-&#62;2-&#62;3-&#62;4-&#62;5-&#62;6-&#62;7-&#62;80-&#62;NULL and k = 5 Output: 5-&#62;4-&#62;3-&#62;2-&#62;1-&#62;8-&#62;7-&#62;6-&#62;NULL. Algorithm: reverse(head, k) &#8230; <a href="http://techpuzzl.wordpress.com/2011/06/07/reverse-a-linked-list-in-groups-of-given-size/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=280&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Given a linked list, write a function to reverse every k nodes (where k is an input to the function).</p>
<p><strong>Example:</strong><br />
Inputs: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;NULL and k = 3<br />
Output: 3-&gt;2-&gt;1-&gt;6-&gt;5-&gt;4-&gt;8-&gt;7-&gt;NULL.</p>
<p>Inputs: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;80-&gt;NULL and k = 5<br />
Output: 5-&gt;4-&gt;3-&gt;2-&gt;1-&gt;8-&gt;7-&gt;6-&gt;NULL.</p>
<p><strong>Algorithm: reverse(head, k)</strong><br />
1) Reverse the first sub-list of size k. While reversing keep track of the next node and previous node. Let the pointer to the next node be next and pointer to the previous node be prev.<br />
2) head-&gt;next = reverse(next, k) /* Recursively call for rest of the list and link the two sub-lists */<br />
3) return prev /* prev becomes the new head of the list (see the diagrams of iterative method of this post) */</p>
<pre><code>class ReverseKLL
{
        static class Node
        {
                int data;
                Node next;
                Node(int data)
                {
                        this.data = data;
                        next =null;
                }
        }
        public static Node head = null; //Create head node
        //Recursive Reversal of a group of k elements
        //1. Reverse the first sub-list of k element and keep track of next and previous nodes
        //2. head.next = reverse(next,k ) - recursively call this function on sub-groups
        //3. return prev  - prev becomes new next
        private static Node reverse(Node node, int k)
        {
                Node current = node;
                Node next= node, prev=null;
                int count = 0;
                //reverse first k elements
                while(current != null &amp;&amp; count &lt; k)
                {
                        next = current.next;
                        current.next = prev;
                        prev = current;
                        current=next;
                        count ++;
                }
                //next is now pointer to k+1th node. Recursively call reverse
                //for the list starting from that point
                if (next!=null)
                {
                        node.next = reverse(next,k);
                }
                //prev is the new head
                return prev;
        }
        //utility methods
        //push a node in the ll
        private static void push(int data)
        {
                Node newnode= new Node(data);
                newnode.next = head;
                head = newnode;

        }
        public static void printList(Node node)
        {
                while (node != null)
                {
                        System.out.println(node.data);
                        node = node.next;
                }
        }
        public static void main(String args[])
        {
               ReverseKLL kll = new ReverseKLL();
               kll.push(8);
               kll.push(7);
               kll.push(6);
               kll.push(5);
               kll.push(4);
               kll.push(3);
               kll.push(2);
               kll.push(1);

               kll.printList(head);
               head=kll.reverse(head, 3);
               System.out.println(&quot;===========================&quot;);
               kll.printList(head);

        }
}
</code></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/techpuzzl.wordpress.com/280/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/techpuzzl.wordpress.com/280/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/techpuzzl.wordpress.com/280/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/techpuzzl.wordpress.com/280/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/techpuzzl.wordpress.com/280/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/techpuzzl.wordpress.com/280/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/techpuzzl.wordpress.com/280/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/techpuzzl.wordpress.com/280/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/techpuzzl.wordpress.com/280/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/techpuzzl.wordpress.com/280/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/techpuzzl.wordpress.com/280/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/techpuzzl.wordpress.com/280/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/techpuzzl.wordpress.com/280/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/techpuzzl.wordpress.com/280/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=280&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://techpuzzl.wordpress.com/2011/06/07/reverse-a-linked-list-in-groups-of-given-size/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0d95fe3795b73e39db2c0a4233e7f850?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">upnishad</media:title>
		</media:content>
	</item>
		<item>
		<title>Intersection of two Arrays</title>
		<link>http://techpuzzl.wordpress.com/2011/06/07/intersection-of-two-arrays/</link>
		<comments>http://techpuzzl.wordpress.com/2011/06/07/intersection-of-two-arrays/#comments</comments>
		<pubDate>Tue, 07 Jun 2011 06:14:17 +0000</pubDate>
		<dc:creator>Upnishad</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Yahoo]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[intersection]]></category>

		<guid isPermaLink="false">http://techpuzzl.wordpress.com/?p=269</guid>
		<description><![CDATA[Very simple algorithm but somehow interviewers love this. So, here it is: For example, if the input arrays are: arr1[] = {1, 3, 4, 5, 7} arr2[] = {2, 3, 5, 6} Then your program should print Intersection as {3, &#8230; <a href="http://techpuzzl.wordpress.com/2011/06/07/intersection-of-two-arrays/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=269&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Very simple algorithm but somehow interviewers love this. So, here it is:<br />
For example, if the input arrays are:<br />
arr1[] = {1, 3, 4, 5, 7}<br />
arr2[] = {2, 3, 5, 6}<br />
Then your program should print Intersection as {3, 5}.</p>
<p><strong>Algorithm</strong>:<br />
For Intersection of two arrays, print the element only if the element is present in both arrays.<br />
1) Use two index variables i and j, initial values i = 0, j = 0<br />
2) If arr1[i] is smaller than arr2[j] then increment i.<br />
3) If arr1[i] is greater than arr2[j] then increment j.<br />
4) If both are same then print any of them and increment both i and j.</p>
<p><strong>Code:</strong></p>
<pre><code>a1 = [1,3,4,5,7]
a2 = [2,3,5,6]
i,j = 0,0

#Get length of the larger array as the count
count = (max(len(a1), len(a2))) 

for x in range(0,count):
        if (a1[i]  a2[j]):
                j = j+1
        elif (a1[i] == a2[j]):
                print a1[i]
                i = i+1
                j = j+1</code></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/techpuzzl.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/techpuzzl.wordpress.com/269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/techpuzzl.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/techpuzzl.wordpress.com/269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/techpuzzl.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/techpuzzl.wordpress.com/269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/techpuzzl.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/techpuzzl.wordpress.com/269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/techpuzzl.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/techpuzzl.wordpress.com/269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/techpuzzl.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/techpuzzl.wordpress.com/269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/techpuzzl.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/techpuzzl.wordpress.com/269/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=269&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://techpuzzl.wordpress.com/2011/06/07/intersection-of-two-arrays/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0d95fe3795b73e39db2c0a4233e7f850?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">upnishad</media:title>
		</media:content>
	</item>
		<item>
		<title>Group all anagrams</title>
		<link>http://techpuzzl.wordpress.com/2011/06/06/group-all-anagrams/</link>
		<comments>http://techpuzzl.wordpress.com/2011/06/06/group-all-anagrams/#comments</comments>
		<pubDate>Mon, 06 Jun 2011 08:54:46 +0000</pubDate>
		<dc:creator>Upnishad</dc:creator>
				<category><![CDATA[Amazon]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://techpuzzl.wordpress.com/?p=266</guid>
		<description><![CDATA[Given a set of words in a dictionary, write a program to group all words which are anagrams of each other in to sets. input: &#8220;bat&#8221;, &#8220;tar&#8221;, &#8220;xyz&#8221; , &#8220;tab&#8221;, &#8220;tar&#8221;           output:[["bat", tab"], ["tar", rat"],&#8221;xyz&#8221; ] The simpler logic would &#8230; <a href="http://techpuzzl.wordpress.com/2011/06/06/group-all-anagrams/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=266&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Given a set of words in a dictionary, write a program to group all words which are anagrams of each other in to sets.<br />
input: &#8220;bat&#8221;, &#8220;tar&#8221;, &#8220;xyz&#8221; , &#8220;tab&#8221;, &#8220;tar&#8221;           output:[["bat", tab"], ["tar", rat"],&#8221;xyz&#8221; ]</p>
<p>The simpler logic would be to :</p>
<ol>
<li>Use some sort of a hash structure</li>
<li>Sort all words alphabetically and use the sorted word as the key</li>
<li>If the key is found, keep on adding the original word to the &#8216;value&#8217; list of strings</li>
</ol>
<p><span id="more-266"></span></p>
<p>Something like this :</p>
<pre><code>

def compare_sort(x):
        sorted_key_list = list(x)
        sorted_key_list.sort()
        sorted_str = ','.join(str(n) for n in sorted_key_list)
        sorted_str = sorted_str.replace(',','')
        return sorted_str

source = ['tab','bat','atb','tac','cat','xyz']

target_hash = {}

for x in source:
        sorted_key = compare_sort(x)
        print sorted_key

        if target_hash.has_key(sorted_key):
                existing_list = target_hash[sorted_key]
                existing_list.append(x)
                target_hash[sorted_key] = existing_list
        else:
                target_list =[x]
                target_hash[sorted_key] = target_list

print target_hash
</code></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/techpuzzl.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/techpuzzl.wordpress.com/266/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/techpuzzl.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/techpuzzl.wordpress.com/266/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/techpuzzl.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/techpuzzl.wordpress.com/266/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/techpuzzl.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/techpuzzl.wordpress.com/266/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/techpuzzl.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/techpuzzl.wordpress.com/266/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/techpuzzl.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/techpuzzl.wordpress.com/266/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/techpuzzl.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/techpuzzl.wordpress.com/266/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=266&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://techpuzzl.wordpress.com/2011/06/06/group-all-anagrams/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0d95fe3795b73e39db2c0a4233e7f850?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">upnishad</media:title>
		</media:content>
	</item>
		<item>
		<title>Mastering Programming Interview Questions</title>
		<link>http://techpuzzl.wordpress.com/2010/08/26/mastering-programming-interview-questions/</link>
		<comments>http://techpuzzl.wordpress.com/2010/08/26/mastering-programming-interview-questions/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 14:09:44 +0000</pubDate>
		<dc:creator>Upnishad</dc:creator>
				<category><![CDATA[Interview Question]]></category>
		<category><![CDATA[Programming Pearls]]></category>

		<guid isPermaLink="false">http://techpuzzl.wordpress.com/?p=262</guid>
		<description><![CDATA[http://courses.csail.mit.edu/iap/interview/materials.php &#8211; Pure Gold also, a must read &#8211; http://news.ycombinator.com/item?id=1633892<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=262&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>http://courses.csail.mit.edu/iap/interview/materials.php &#8211; Pure Gold</p>
<p>also, a must read &#8211; <a href="http://news.ycombinator.com/item?id=1633892">http://news.ycombinator.com/item?id=1633892</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/techpuzzl.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/techpuzzl.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/techpuzzl.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/techpuzzl.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/techpuzzl.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/techpuzzl.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/techpuzzl.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/techpuzzl.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/techpuzzl.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/techpuzzl.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/techpuzzl.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/techpuzzl.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/techpuzzl.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/techpuzzl.wordpress.com/262/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=262&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://techpuzzl.wordpress.com/2010/08/26/mastering-programming-interview-questions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0d95fe3795b73e39db2c0a4233e7f850?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">upnishad</media:title>
		</media:content>
	</item>
		<item>
		<title>Phone Interview Silverbullet</title>
		<link>http://techpuzzl.wordpress.com/2010/06/30/phone-interview-silverbullet/</link>
		<comments>http://techpuzzl.wordpress.com/2010/06/30/phone-interview-silverbullet/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 16:43:14 +0000</pubDate>
		<dc:creator>Upnishad</dc:creator>
				<category><![CDATA[Interview Question]]></category>

		<guid isPermaLink="false">http://techpuzzl.wordpress.com/?p=250</guid>
		<description><![CDATA[Steve Yegge talks about the difficulties of a phone interview.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=250&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://sites.google.com/site/steveyegge2/five-essential-phone-screen-questions" target="_blank">Steve Yegge </a>talks about the difficulties of a phone interview.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/techpuzzl.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/techpuzzl.wordpress.com/250/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/techpuzzl.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/techpuzzl.wordpress.com/250/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/techpuzzl.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/techpuzzl.wordpress.com/250/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/techpuzzl.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/techpuzzl.wordpress.com/250/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/techpuzzl.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/techpuzzl.wordpress.com/250/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/techpuzzl.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/techpuzzl.wordpress.com/250/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/techpuzzl.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/techpuzzl.wordpress.com/250/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=250&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://techpuzzl.wordpress.com/2010/06/30/phone-interview-silverbullet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0d95fe3795b73e39db2c0a4233e7f850?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">upnishad</media:title>
		</media:content>
	</item>
		<item>
		<title>Card Shuffle.</title>
		<link>http://techpuzzl.wordpress.com/2010/06/30/card-shuffle/</link>
		<comments>http://techpuzzl.wordpress.com/2010/06/30/card-shuffle/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 09:27:24 +0000</pubDate>
		<dc:creator>Upnishad</dc:creator>
				<category><![CDATA[Amazon]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[Interview Question]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://techpuzzl.wordpress.com/?p=240</guid>
		<description><![CDATA[Write a program to shuffle an pack of cards in the most efficient way. This question can be asked in several flavors. Knuth Shuffle / Fisher-Yates Shuffle(Modified version by Durstenfeld) algorithm is the answer to all the trick questions. So &#8230; <a href="http://techpuzzl.wordpress.com/2010/06/30/card-shuffle/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=240&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Write a program to shuffle an pack of cards in the most efficient way. This question can be asked in several flavors.</p>
<p><a href="http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle" target="_blank">Knuth Shuffle / Fisher-Yates Shuffle(Modified version by Durstenfeld)</a> algorithm is the answer to all the trick questions. So how does this algorithm work ?<span id="more-240"></span></p>
<p>Fisher  and Yates&#8217; original method was designed to be  implemented using pencil and paper, with a precomputed table of random  numbers as the source of randomness. Modern approach suggested by &#8211; <a title="Richard Durstenfeld (page does not exist)" href="http://en.wikipedia.org/w/index.php?title=Richard_Durstenfeld&amp;action=edit&amp;redlink=1">Richard  Durstenfeld</a> is as follows:</p>
<p>To shuffle an array <em>a</em> of <em>n</em> elements:</p>
<pre><strong>for</strong> <em>i</em> <strong>from</strong> <em>n</em> - 1 <strong>downto</strong> 1 <strong>do</strong><em><strong>
         </strong>j</em> ← random integer with 0 ≤ <em>j</em> ≤ <em>i
        </em> exchange <em>a</em>[<em>j</em>] and <em>a</em>[<em>i</em>]
</pre>
<p>Reduced time complexity to <em>O</em>(<em>n</em>), compared to <em>O</em>(<em>n</em><sup>2</sup>) for  the naive implementation. A sample java implementation :</p>
<p><pre class="brush: java;">
import java.util.Random;

static Random rng = new Random();
public static void shuffle(int[] array) {
    // i is the number of items remaining to be shuffled.
    for (int i = array.length; i &amp;gt; 1; i--) {
        // Pick a random element to swap with the i-th element.
        int j = rng.nextInt(i);  // 0 &amp;lt;= j &amp;lt;= i-1 (0-based array)
        // Swap array elements.
        int tmp = array[j];
        array[j] = array[i-1];
        array[i-1] = tmp;
    }
}
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/techpuzzl.wordpress.com/240/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/techpuzzl.wordpress.com/240/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/techpuzzl.wordpress.com/240/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/techpuzzl.wordpress.com/240/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/techpuzzl.wordpress.com/240/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/techpuzzl.wordpress.com/240/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/techpuzzl.wordpress.com/240/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/techpuzzl.wordpress.com/240/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/techpuzzl.wordpress.com/240/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/techpuzzl.wordpress.com/240/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/techpuzzl.wordpress.com/240/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/techpuzzl.wordpress.com/240/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/techpuzzl.wordpress.com/240/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/techpuzzl.wordpress.com/240/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=240&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://techpuzzl.wordpress.com/2010/06/30/card-shuffle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0d95fe3795b73e39db2c0a4233e7f850?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">upnishad</media:title>
		</media:content>
	</item>
		<item>
		<title>TRIE implementation in Java</title>
		<link>http://techpuzzl.wordpress.com/2010/06/29/trie-implementation-in-java/</link>
		<comments>http://techpuzzl.wordpress.com/2010/06/29/trie-implementation-in-java/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 17:17:13 +0000</pubDate>
		<dc:creator>Upnishad</dc:creator>
				<category><![CDATA[Binary Tree]]></category>

		<guid isPermaLink="false">http://techpuzzl.wordpress.com/?p=230</guid>
		<description><![CDATA[Its been a very long time since I have been looking for a good explanation/implementation of TRIEs in Java. Unfortunately, internet is full of incomplete or too academic articles. Finally, today I stumbled upon a very practical article : Source &#8230; <a href="http://techpuzzl.wordpress.com/2010/06/29/trie-implementation-in-java/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=230&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Its been a very long time since I have been looking for a good explanation/implementation of TRIEs in Java. Unfortunately, internet is full of incomplete or too academic articles.</p>
<p>Finally, today I stumbled upon a <a href="http://www.technicalypto.com/2010/04/trie-in-java.html" target="_blank">very practical article</a> : Source <a href="http://jump.fm/PODEM" target="_blank">Uploaded here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/techpuzzl.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/techpuzzl.wordpress.com/230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/techpuzzl.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/techpuzzl.wordpress.com/230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/techpuzzl.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/techpuzzl.wordpress.com/230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/techpuzzl.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/techpuzzl.wordpress.com/230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/techpuzzl.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/techpuzzl.wordpress.com/230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/techpuzzl.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/techpuzzl.wordpress.com/230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/techpuzzl.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/techpuzzl.wordpress.com/230/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=230&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://techpuzzl.wordpress.com/2010/06/29/trie-implementation-in-java/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0d95fe3795b73e39db2c0a4233e7f850?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">upnishad</media:title>
		</media:content>
	</item>
		<item>
		<title>Vertical Sum of a Binary Tree</title>
		<link>http://techpuzzl.wordpress.com/2010/06/29/vertical-sum-of-a-binary-tree/</link>
		<comments>http://techpuzzl.wordpress.com/2010/06/29/vertical-sum-of-a-binary-tree/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 14:09:14 +0000</pubDate>
		<dc:creator>Upnishad</dc:creator>
				<category><![CDATA[Amazon]]></category>
		<category><![CDATA[Binary Tree]]></category>
		<category><![CDATA[Interview Question]]></category>

		<guid isPermaLink="false">http://techpuzzl.wordpress.com/?p=227</guid>
		<description><![CDATA[Find vertical sum of given binary tree. Example: Code: 1 / \ 2 3 / \ / \ 4 5 6 7 The tree has 5 vertical lines Vertical-1: nodes-4 =&#62; vertical sum is 4 Vertical-2: nodes-2 =&#62; vertical sum &#8230; <a href="http://techpuzzl.wordpress.com/2010/06/29/vertical-sum-of-a-binary-tree/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=227&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Find vertical sum of given binary tree.</p>
<p>Example:</p>
<div>
<div>Code:</div>
<pre>     1
    / \
  2     3
 / \   / \
4   5 6   7</pre>
</div>
<p>The tree has 5 vertical lines<br />
Vertical-1: nodes-4     =&gt; vertical sum is 4<br />
Vertical-2: nodes-2     =&gt; vertical sum is 2<br />
Vertical-3: nodes-1,5,6 =&gt; vertical sum is 1+5+6 = 12<br />
Vertical-4: nodes-3     =&gt; vertical sum is 3<br />
Vertical-5: nodes-7     =&gt; vertical sum is 7<br />
We need to output: 4 2 12 3 7</p>
<p><span id="more-227"></span>We can do an inorder traversal and hash the column. We call  Traverse(root, 0) which means the root is at column 0. As we are doing  our traversal, we can hash the column and increase its value by T.data. A  rough sketch of my function looks like this -</p>
<p>Traverse(Tree T, int column)<br />
{<br />
if(T==NULL) return;<br />
Traverse(T.left, column-1);<br />
Hash(column) += T.data;<br />
Traverse(T.right, column+1);<br />
}</p>
<p>Traverse(root, 0);<br />
Print Hash</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/techpuzzl.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/techpuzzl.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/techpuzzl.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/techpuzzl.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/techpuzzl.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/techpuzzl.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/techpuzzl.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/techpuzzl.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/techpuzzl.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/techpuzzl.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/techpuzzl.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/techpuzzl.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/techpuzzl.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/techpuzzl.wordpress.com/227/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=techpuzzl.wordpress.com&amp;blog=8848764&amp;post=227&amp;subd=techpuzzl&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://techpuzzl.wordpress.com/2010/06/29/vertical-sum-of-a-binary-tree/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0d95fe3795b73e39db2c0a4233e7f850?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">upnishad</media:title>
		</media:content>
	</item>
	</channel>
</rss>
