tag:blogger.com,1999:blog-10409339933945747272020-08-09T19:42:41.904-07:00Pavel's BlogA selfish technical blog that I use entirely for my personal growth.Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.comBlogger25125tag:blogger.com,1999:blog-1040933993394574727.post-50859415275655182322015-01-10T11:42:00.000-08:002015-01-10T11:45:46.497-08:00We're hiring again!Our team is expanding an we're growing again.<br />We need to fill 9 positions. If you're interested (or know anyone interested) send me your resume at pavel@fusu.us<br /><br /><br />About things we do:<br /><a href="http://www.allthingsdistributed.com/2014/11/apollo-amazon-deployment-engine.html" target="_blank">http://www.allthingsdistributed.com/2014/11/apollo-amazon-deployment-engine.html</a> Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com25Seattle, WA, USA47.6062095 -122.332070847.2636815 -122.9775178 47.9487375 -121.68662379999999tag:blogger.com,1999:blog-1040933993394574727.post-59435841919037575112014-11-12T18:18:00.000-08:002014-11-12T18:20:49.007-08:00Apollo-based CodeDeploy is out!We're proud to announce our new service <a href="http://aws.amazon.com/codedeploy" target="_blank">CodeDeploy</a>, based on our internal tool Apollo:<br /><a href="http://www.allthingsdistributed.com/2014/11/apollo-amazon-deployment-engine.html" target="_blank">http://www.allthingsdistributed.com/2014/11/apollo-amazon-deployment-engine.html</a>Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com14Seattle, WA, USA47.6062095 -122.332070847.2636815 -122.9775178 47.9487375 -121.68662379999999tag:blogger.com,1999:blog-1040933993394574727.post-31762124709321939842014-05-08T20:03:00.001-07:002014-10-12T23:28:24.532-07:00We're hiring!Update:<br />Position has been filled but we'll be hiring again soon!<br /><br />Builder Tools team @ Amazon is hiring:<br /><br /><a href="http://www.amazon.com/gp/jobs/254131" target="_blank">http://www.amazon.com/gp/jobs/254131</a><br /><br />Do you think you have what it takes? We need you!<br />Apply online or send me your resume and we'll get you an interview.Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com13Seattle, WA, USA47.6062095 -122.332070847.2636815 -122.9775178 47.9487375 -121.68662379999999tag:blogger.com,1999:blog-1040933993394574727.post-19462118821600227562013-07-28T11:14:00.002-07:002014-05-10T13:54:12.060-07:00Finding The Median In Large Sets Of Numbers Split Across 1000 ServersHow would you find the median across a thousand servers with a billion of numbers each?<br /><br /><div style="text-align: justify;">This is a question that involves lots of discussion because it may be quite vague and may require taking some assumptions. Obviously we can't do any kind of in-memory sorting because we don't have enough memory. We may possibly fit one set at a time, which would be under 1 GB of memory.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">The first obvious solution is an external merge sort and then a look up of the n/2 element (or the average of n/2 and n/2 + 1 on even n's). This solution would be <i>n log n</i>, but you may look for an <i>O (n) </i>solution, which someone may find impossible, since you need a sorted set to determine the median that involves element comparison which leads to the <i>n log n </i>complexity. But, who says we need sort by comparison.. ?</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Another solution comes from our observation that we need to find the n/2 element in a sorted array, which can be generalized to the order statistics algorithm of finding the kth smallest number in an unsorted array. This algorithm is also called quick select or the <a href="https://en.wikipedia.org/wiki/Selection_algorithm" rel="nofollow" target="_blank">selection algorithm</a>. I talked about an implementation of this algorithm in <a href="http://www.fusu.us/2013/06/p4-order-statistics-kth-smallest-or.html" target="_blank">this article</a>. The problem is that this is an in-memory algorithm and we would not have enough memory to maintain all the sets. So we will need a distributed version of this algorithm which will basically need a routine to swap numbers across servers. We have to assume that this is an optimized and efficient routine, because there's just too many factors that can affect the efficiency of this routine (network latency, connection problems). This is a <i>O(n)</i> solution and the distributed version of kth smallest may look like this:</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><script src="https://gist.github.com/pavelfusu/55ef897e4776d32bc4e2.js"></script>Another solution comes from the second observation that we shouldn't use a comparison sort. We could use a histogram of the counts of the numbers across the servers, like in the <a href="http://en.wikipedia.org/wiki/Counting_sort" rel="nofollow" target="_blank">counting sort</a>. But here we would have to assume a few more things. First we have to know something about the range of the numbers. If we have ranges of order of billions we could store an array of a few billions cells or at least one billion since the problem statement allows us to process a billion numbers in memory, which is the second assumption. Again this is a <i>O(n) </i>solution because we compute the histogram by going once through all the numbers and then find the kth number (the median) by going through the elements of the histogram. In code it may look similar to this:<br /><br /></div><script src="https://gist.github.com/pavelfusu/f4ab476b19bf5c99ee21.js"></script> <br /><div style="text-align: justify;"><br /></div><div style="text-align: justify;">We could think of more solutions if we could assume even more facts. Let's say we know that the numbers across all servers are uniformly distributed. Having this large amount of numbers we could say the median is also the average of all numbers. So if we also know the range of distribution, say 0.. 1 billion, then computing the median is a <i>O(1)</i> operation and all we need to do is to compute <i>0 + (1 billion - 0) / 2 </i>which is 500 million. If we don't know the range we can compute the median of medians in <i>O(n) </i>by using order statistics on each server.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">If the distribution is not uniform but we know some information about it we could still calculate the median with some probability. Of course we can find other different solutions if we take various assumptions, but the above solutions can probably satisfy any interviewer or whoever asked this question.</div>Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com17tag:blogger.com,1999:blog-1040933993394574727.post-37113444773611467112013-07-19T17:56:00.000-07:002013-07-20T10:53:59.968-07:00Printing Outside Nodes (Boundary) Of A Binary Tree<div style="text-align: justify;">Contrary to many solutions I found online for this problem - I'm going to say a breadth first search (while printing the first and the last element) is not appropriate here and it won't help in cases where the tree has leafs at levels closer to the root, and those algorithms would miss that. The solution here is a <a href="http://en.wikipedia.org/wiki/Tree_traversal#Pre-order" rel="nofollow" target="_blank">depth first pre-order</a> algorithm, but let me explain the problem statement first. So having the tree below (it is a binary search tree), print the boundary nodes of the tree anticlockwise. Printing the boundary nodes for our tree should result in the following sequence: <i>[60, 41, 16, 25, 42, 55, 62, 64, 70, 65, 74].</i></div><div style="text-align: justify;"><br /></div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody><tr><td style="text-align: center;"><a href="http://2.bp.blogspot.com/-31SLzJBPgj0/UbpLFhCVvyI/AAAAAAAABkc/aYRpMDI92MA/s1600/bstexample.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img alt="Simple Binary Search Tree" border="0" src="http://2.bp.blogspot.com/-31SLzJBPgj0/UbpLFhCVvyI/AAAAAAAABkc/aYRpMDI92MA/s1600/bstexample.png" title="Simple Binary Search Tree" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="background-color: white; color: #666666; font-family: 'Trebuchet MS', Trebuchet, Verdana, sans-serif; font-size: 11px; line-height: 14px;">Fig. 1 - Simple Binary Search Tree</span></td></tr></tbody></table><div style="text-align: justify;"><br /></div><div style="text-align: justify;">A pre-order traversal is more appropriate here because it traverses branches in the way we need; for example, it will traverse <i>[60, 41, 16, 25] </i>first which is what we need and we print them. The next sequences are of no interest to us, so we could add a flag that determines whether nodes should be printed out or not (ex: print only leaf nodes). So out of <i>[53, 46, 42, 55, 74, 65, 63, 62, 64, 70] </i>we would print only leaf nodes <i>[42, 55, 62, 64, 70]. </i>We're left with right branch only, where we have to print all the non-leaf nodes. For this we could have a separate stack where, while traversing we add the non-leaf nodes. In code this may look like this:</div><div style="text-align: justify;"><br /></div><br /><script src="https://gist.github.com/pavelfusu/a113ccb169e43a08b8d2.js"></script><br /><br />Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com23tag:blogger.com,1999:blog-1040933993394574727.post-43041308509842105342013-07-17T18:42:00.000-07:002013-07-17T18:43:25.106-07:00Matching An Input String With A Given Dictionary<div style="text-align: justify;">Questions about string matching are pretty often in interviews and this is one of them. I've seen two variations online: one to split the string in meaningful words and another one to determine all possible meaningful words that can be produced out of sub-strings of the input.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">First one seems easy - you just go once through the string and output what you've matched, thus O(n) where n is the number of the input string's characters. Second one would involve trying all possible sub-words, which would be O(nk) where k is the length of the longest word.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Now another problem is how would you represent a dictionary. A <a href="http://en.wikipedia.org/wiki/Hash_table" target="_blank">hash map</a> won't work here because there will be too many collisions and the hash map would loose its efficiency. The first solution here would be <a href="http://en.wikipedia.org/wiki/Trie" target="_blank">a trie</a> and that's what I used in both mentioned problems. An example of an implementation of a trie can be found <a href="https://gist.github.com/pavelfusu/cf0fa70af468b3fe5abe" target="_blank">here</a>. </div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><br /></div><script src="https://gist.github.com/pavelfusu/1d8b33bde6146a121cf7.js"></script><br /><br /><div style="text-align: justify;">Now can it be done faster? Yes, we could use the <a href="http://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_string_matching_algorithm" rel="nofollow" target="_blank">Aho-Corasick string matching algorithm</a> which uses a <a href="http://en.wikipedia.org/wiki/Finite_state_machine" rel="nofollow" target="_blank">finite state machine</a> that resembles to a trie. That will improve our time in the second problem from O(nk) to O(n), but don't forget that we have the pre-processing time. This may not be asked to be implemented in an interview, but it's good to mention.</div>Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com10tag:blogger.com,1999:blog-1040933993394574727.post-56719416873722903602013-07-16T17:11:00.001-07:002013-07-30T16:54:21.111-07:00Re-creating A Binary Search Tree Given Its Pre-order Traversal<div style="text-align: justify;">As a reminder a <a href="http://en.wikipedia.org/wiki/Tree_traversal#Pre-order" target="_blank">pre-order traversal</a> consists of visiting the root first then the left and right child. Thus, the first element in a pre-order traversal will always be the root of the tree. It may be easier to find a quick solution by looking at <a href="http://www.fusu.us/2013/06/p10-validating-binary-search-tree.html" target="_blank">this post</a> where I showed how to validate a tree. Our interest lays in the recursive solution. We could traverse the array and create the tree in the same manner it is traversed in the above mentioned solution, by maintaining a minimum and maximum value.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><br /></div><script src="https://gist.github.com/pavelfusu/aeaec8760dc3d35b7ecf.js"></script><br /><br /><br />Note: <br />Using the external index here is because passing it as an integer argument will not work, since Java passes arguments by value. To avoid using the external index, we could have a mutable integer class or an array of ints, where the first cell is the index value: <br /><br /><script src="https://gist.github.com/pavelfusu/5880c720c754275d0911.js"></script><br />Implementing a mutable integer class is fairly easy but it is mostly re-inventing the wheel - Apache Commons has <a href="http://commons.apache.org/proper/commons-lang/javadocs/api-2.4/org/apache/commons/lang/mutable/MutableInt.html">an implementation</a> of this class that could be used here.Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com1tag:blogger.com,1999:blog-1040933993394574727.post-47599443438755907932013-07-10T18:26:00.002-07:002013-07-11T06:52:48.387-07:00Fast Fibonacci<div style="text-align: justify;">I was looking for a faster Fibonacci algorithm and stumbled across this <a href="http://nayuki.eigenstate.org/page/fast-fibonacci-algorithms">one</a>. The doubling method assumes we know F(n) and F(n+1) to produce F(2n) and F(2n+1) out of the following formulas:</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">F(2n) = F(n) * (2*F(n+1) - F(n))</div><div style="text-align: justify;">F(2n+1) = F(n+1)<sup>2</sup> + F(n)<sup>2</sup></div><br /><div style="text-align: justify;"><span style="font-size: 13px;"><br /></span></div><div style="text-align: justify;">The formulas can be extracted from the matrix exponentiation algorithm mentioned in the above article and reduced of redundant calculations.</div><br /><div style="text-align: justify;"><br /></div><script src="https://gist.github.com/pavelfusu/15554c9c036880d79217.js"></script><br /><br />Note:<br /><div style="text-align: justify;">- <span class="n" style="background-color: white; color: #333333; font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 12px; line-height: 16px; white-space: pre;">Integer</span><span class="o" style="background-color: white; font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 12px; font-weight: bold; line-height: 16px; white-space: pre;">.</span><span class="na" style="background-color: white; color: teal; font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 12px; line-height: 16px; white-space: pre;">numberOfLeadingZeros </span>is a Java shortcut to get to the highest set bit. It may be implemented in some other way in different languages. It is needed here to avoid a few iterations; in our case considering we have 32 iterations and we can get an n up to 92, we can save up to 24 iterations.</div>Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com2tag:blogger.com,1999:blog-1040933993394574727.post-24468731720134449872013-07-07T18:38:00.000-07:002013-07-11T06:54:05.794-07:00Verifying A Sudoku Solution<div style="text-align: justify;">This one may be easy for <a href="http://en.wikipedia.org/wiki/Sudoku" target="_blank">Sudoku</a> fans. I used to play it so it wasn't difficult to come up with the rules that would define the algorithm. For those who haven't played, <i>Sudoku </i>is a number game where you have a board (usually 9 by 9) divided in 9 sub-grids (blocks 3 by 3). Every cell of the board can contain a number from 1 to 9. The goal is to fill the board with numbers in such a way that numbers are unique column-wise, row-wise and block-wise. The board comes with some numbers filled in. For more information check the <a href="http://en.wikipedia.org/wiki/Sudoku" target="_blank">Wikipedia page</a>. The above Sudoku game was of order 3, but we may want our problem to be a little less restrictive and accept higher orders too. Thus if it would accept a Sudoku board of order <i>n, </i>the board would have <i>n<sup>2 </sup></i>columns and <i>n<sup>2</sup></i> rows; the blocks would be <i>n by n, </i>and we would have to fill in numbers from 1 to <i>n<sup>2</sup></i>.<br /><br />Our algorithm will have at least <i>O(n<sup>2</sup>)</i> time complexity since we have to check each element on the board. It sounds like we could one table traversal and check each element's validity. But how would you check one element when it depends on other elements that may be on the same row/column/block? We could use a <a href="http://en.wikipedia.org/wiki/Set_(abstract_data_type)" target="_blank">Set </a>array of size <i>n<sup>2 </sup></i>for columns, another array of same size for rows and a bi-dimensional array of size <i>n </i>for blocks. Thus, while traversing the board we note each element in the associated set or if it is already there then we stop the algorithm since we have an invalid solution.<br /><br />In code looks like this:<br /><br /></div><br /><script src="https://gist.github.com/pavelfusu/6d1843996a2ed4a4dd38.js"></script><br /><br /><div style="text-align: justify;">It seems like we could improve the code a little by using some bit patterns instead of sets. We could set a bit for each existing number. For example if we already have <i>3</i> and <i>1</i> our bit pattern should look like this: <i>101</i>. We would need some simple bit manipulation logic to check and set a bit. I used <i>long</i>s so that I can allow Sudoku boards of orders up to <i>8:</i></div><div style="text-align: justify;"><i><br /></i></div><div style="text-align: justify;"><i><br /></i></div><script src="https://gist.github.com/pavelfusu/fa88016af62aa51a7db1.js"></script><br /><br />Helper <i>initialize</i> method and test data for convenience:<br /><br /><script src="https://gist.github.com/pavelfusu/d687d9b1aae6e9b8b772.js"></script><br /><br />Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com0tag:blogger.com,1999:blog-1040933993394574727.post-41291510614114588982013-07-06T17:35:00.003-07:002013-07-11T06:55:21.926-07:00Array Rotation By A Particular Amount<div style="text-align: justify;">John Bentley's "<a href="http://www.amazon.com/Programming-Pearls-2nd-Edition-Bentley/dp/0201657880" target="_blank">Programming Pearls</a>" again, with the rotated array problem. So we have a one-dimensional array of <i>N </i>elements that we have to rotate left by <i>I </i>positions. We have to do this in time proportional to <i>N </i>and in O(1) space. So for example if we have the array <i>[1 2 3 4 5 6 7 8 9]</i> and we want to rotate it by 3 positions we should result in this array: <i>[4 5 6 7 8 9 1 2 3]. </i>Note that the array is not necessarily sorted.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">There's plenty of solutions that may come in our minds at first but they probably require additional temporary space. For example we could copy the <i>I </i>elements into a temporary vector, then move to the left all the rest in the original array by <i>I </i>positions and finally copy the elements from the temporary array to the end of the original array. As you see this is too expensive space wise.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">To solve this problem we will require a different view of the problem and some time until you hit that <i>aha! </i>moment. We basically need to transform <i>AB</i> to <i>BA</i>, where <i>A</i> and <i>B</i> are sub-arrays of the original array. Imagine we have a routine that can reverse the elements in a specific portion of the original array (<i>A,</i> <i>B</i>)<i>. </i>So we reverse elements in <i>A</i> first resulting in <i>A<sup>R</sup>B</i> and then reverse the elements in <i>B</i> resulting in <i>A<sup>R</sup>B<sup>R</sup></i>. And finally we need to reverse <i>A<sup>R</sup>B<sup>R</sup></i> to produce <i>(A<sup>R</sup>B<sup>R</sup>)<sup>R</sup></i> which is in fact the array that we were looking for <i>BA.</i><br /><i><br /></i>This may be easier to understand with an example. So we have our array <i>arr =</i> <i>[1, 2, 3, 4, 5, 6, 7, 8, 9] </i>that we need to rotate left by 3. We need to first rotate the sub-array <i>[1, 2, 3] </i>resulting in <i>[3, 2, 1, 4, 5, 6, 7, 8, 9] </i>and then we rotate <i>[4, 5, 6, 7, 8, 9]</i> resulting in <i>[3, 2, 1, 9, 8, 7, 6, 5, 4]. </i>And finally we reverse the whole array resulting in <i>[4, 5, 6, 7, 8, 9, 1, 2, 3] </i>which is the array we were looking for.<br /><br />In code it looks even easier to understand, considering that we already have the routine that does the reversal of the elements in an array:</div><br /><script src="https://gist.github.com/pavelfusu/32de94bbe12855793466.js"></script><br />The routine for reversing is just a <i>for</i> loop that is of O(n/2) complexity, where n is the number of elements it has to reverse. The routine may look like this:<br /><br /><script src="https://gist.github.com/pavelfusu/c112c16d2ba548984ceb.js"></script><br /><br />Note:<br />- the swap routine is trivial and may look like this:<br /><br /><script src="https://gist.github.com/pavelfusu/cdd04be7bddd7efa193b.js"></script>Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com2tag:blogger.com,1999:blog-1040933993394574727.post-10570912053318107372013-07-04T11:00:00.000-07:002013-07-11T06:56:27.924-07:00The Subset Problem<div style="text-align: justify;">There are many variations of this problem, I will stay on the general problem of finding all <a href="https://en.wikipedia.org/wiki/Subset" target="_blank">subsets</a> of a set. For example if our set is <i>[1, 2, 3]</i> - we would have <i>8</i> (2 to the power of 3) subsets: <i>{[], [1], [2], [3], [1, 2], [1, 3], [1, 2, 3], [2, 3]}</i>. So basically our algorithm can't be faster than O(<i>2^n</i>) since we need to go through all possible <a href="http://en.wikipedia.org/wiki/Combination" target="_blank">combinations</a>.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">There's a few ways of doing this. I'll mention two ways here - the recursive way, that we've been taught in high schools; and using a bit string.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Using a bit string involves some bit manipulation but the final code can be found easy to understand. The idea is that all the numbers from <i>0</i> to <i>2^n</i> are represented by unique bit strings of <i>n</i> bit width that can be translated into a subset. So for example in the above mentioned array we would have <i>8</i> numbers from <i>0</i> to <i>7</i> inclusive that would have a bit representation that is translated using the bit index as the index of the array.</div><div style="text-align: justify;"><br /></div><table border="1" style="text-align: justify;"><tbody><tr><th>Nr</th><th>Bits</th><th>Combination</th></tr><tr><td>0</td><td>000</td><td>{}</td></tr><tr><td>1</td><td>001</td><td>{1}</td></tr><tr><td>2</td><td>010</td><td>{2}</td></tr><tr><td>3</td><td>011</td><td>{1, 2}</td></tr><tr><td>4</td><td>100</td><td>{3}</td></tr><tr><td>5</td><td>101</td><td>{1, 3}</td></tr><tr><td>6</td><td>110</td><td>{2, 3}</td></tr><tr><td>7</td><td>111</td><td>{1, 2, 3}</td></tr></tbody></table><br /><br />It may look easier in the code:<br /><script src="https://gist.github.com/pavelfusu/ce9436631d5f1753173a.js"></script><br /><br />Note:<br />- The <i>Math.pow </i>call computes 2 to the power of n<br />- The <i>findLowestBitSet </i>is a method I mentioned earlier. In Java it could look like this:<br /><br /><script src="https://gist.github.com/pavelfusu/88b8c1033f3c53427415.js"></script><br /><br />Or it could be optimized to look like this:<br /><script src="https://gist.github.com/pavelfusu/d50040e7881cedaed8c0.js"></script><br /><br />For comparison find below the code for the recursive algorithm:<br /><br /><script src="https://gist.github.com/pavelfusu/edfa5d627dc15b58cc79.js"></script>Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com3tag:blogger.com,1999:blog-1040933993394574727.post-3385708972463710212013-06-30T15:03:00.000-07:002013-07-11T06:57:22.452-07:00Generalized Tower of Hanoi - K Pegs, N Disks<div style="text-align: justify;">I recently found online that Facebook is giving a fast track job opportunity for those who would solve a specific problem which may be a little more complex than a simple interview question. The process happens online - you login with your Facebook id and have a specific amount of time to write a solution in the language you desire. Here's the <a href="https://www.interviewstreet.com/recruit/test/start/4e769c33164d9/7fd4c30a54635e103b893a363b91a55d/" target="_blank">url</a> in case you want to try it. They also provide a sample problem and an allotted 45 minutes to solve it. The problem is the well known <a href="http://en.wikipedia.org/wiki/Tower_of_Hanoi" target="_blank">Tower of Hanoi</a> puzzle but generalized to k pegs and n disks. Given an initial and final configuration of the pegs you have to output the minimal number of steps needed to reach the final configuration. To make it easier they bound the k and n to <i>1 <= n <= 8, 3 <= k <= 5</i> and let you assume that there's a solution that takes at least 7 steps.<br /><br />One basic approach here is brute forcing all possible configurations which results in an undirected graph. And how do you find something in a graph ? Using a search strategy - in my case I chose <a href="https://en.wikipedia.org/wiki/Breadth-first_search" target="_blank">Breath-First Search</a>.<br /><br />Before going into the details of this approach let's see what other approaches we can find. Brute forcing will generate in the worst case 5^8 (390625) different configurations and then we'll have to search through them. Is there any other way where we wouldn't do a brute force, but go through the minimal path the first time? It seems like we could use different strategies of navigating through the pegs of a Hanoi Tower. There's basically 3 configuration cases that will take different approaches in navigation. Those are n < k, n=k and n > k. The point is there's a specific move pattern for each of these cases, that you could do to achieve the desired configuration. <a href="http://www-rci.rutgers.edu/~cfs/472_html/ProblemSolving/TOH_Strategies/TOH_KpegsNDisks.html" target="_blank">This article</a> goes into much detail on that. There's also the way of multi-threading - we could parallelize this in some way - for example we could start from both ends.<br /><br />I chose the brute force this time. While coding this approach a few problems may arise; mainly the problem of how would you represent the pegs. I chose to use a <i>byte</i> array, where each cell represents a peg and each bit of a peg represents a disk (if set).<br /><br /><br />Code looks like this:<br /><br /></div><script src="https://gist.github.com/pavelfusu/4dcf6cd4e5fb8b0bf7b5.js"></script> Note:<br />The <i>findLowestBitSet </i>method basically returns the number of trailing zeros. In Java there's a util method for that:<br /><br /><script src="https://gist.github.com/pavelfusu/88b8c1033f3c53427415.js"></script> The method could be optimized to look like this: <br /><script src="https://gist.github.com/pavelfusu/d50040e7881cedaed8c0.js"></script>Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com135tag:blogger.com,1999:blog-1040933993394574727.post-44640123982843363032013-06-24T18:25:00.003-07:002013-08-10T12:20:48.695-07:00Bitmap Sort<div style="text-align: justify;">I was looking through John Bentley's "<a href="http://www.amazon.com/Programming-Pearls-2nd-Edition-Bentley/dp/0201657880">Programming Pearls</a>" and I found a problem that I encountered before and decided to make note of it here. So you have a large amount of numbers that you're supposed to sort, but there's not enough memory to do that in. Obvious answer is to look for methods of sorting on disk aka <a href="http://en.wikipedia.org/wiki/External_sorting">external sorting</a>. There's a few methods known that include taking multiple passes through the input or parallelism. But this article is not about any of those - it should be faster than any of those. Consider that you have a set of 48000 unique numbers in the range of 1..64000. Does this make a difference? Also consider that you have 10K available memory, so you can't load all the numbers in memory. </div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">The idea here is that we can represent the input as a <a href="http://en.wikipedia.org/wiki/Bit_array">bit sequence (bit map)</a> - the only thing left is to think of a data type. We could use a number of integers, and map our input to the integers bits. In Java an integer takes 4 bytes or 32 bits, this means we could map 32 input numbers to each integer. So, for example an input number 16 will map to bit 16 of integer 1, where an input number 37 will map to bit 5 of integer 2. </div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">To represent all possible input numbers we'll need 64000 / 32 integers - 2000 which will take 2000 * 4 = 8K, which fits into our limitations. Second step would be to output the sorted array. Since mapping the integers to a specific bit, makes the sequence sorted, we just need to traverse the bits and output every bit that has been turned on. In the end we get a linear time algorithm that takes O(n) or precisely O(64000) and it looks like this:</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><br /></div><script src="https://gist.github.com/pavelfusu/283185ccd9eb1c9f30e6.js"></script> <br />Note:<br /><div style="text-align: justify;">- The IO operations are there just to simulate input and output, instead of loading array into memory. Those are trivial, and implementation depends upon language.<br />- BUCKET_SIZE = 32 in our case</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">I will go through the code a little to make sure it is clear. So first step is the <i>createBitMap </i>method which creates a bitmap out of the input. On line 11 we decide which bit to set, whereas in line 12 we decide which bucket (integer) this bit should go to. It also does get set on line 12, through an OR operation.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Second step is going through the created bitmap and showing the actual integer values. On line 24 we go through each bit of an integer and check if it's set or not (line 25). If set we convert to the actual integer values on line 27.</div>Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com44tag:blogger.com,1999:blog-1040933993394574727.post-25446569823863600312013-06-23T13:32:00.001-07:002013-07-11T06:58:59.204-07:00Unsorted Array Converted To "E1 < E2 > E3 < E4..." Format<div style="text-align: justify;">I had a question once where given an unsorted array of integers, you have to produce an array in the "E1 < E2 > E3 < E4..." format. For example {3, 2, 6, 4, 9, 7, 1, 10, 8, 5} would produce {2, 6, 3, 9, 4, 7, 1, 10, 5, 8} and this is because 2 < 6 > 3 < 9 > 4 < 7 > 1 < 10 > 5 < 8. </div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">This is a tricky question where if asked by an interviewer you have to ask follow-up questions! I'm saying this out of my experience, my first solutions that I had in mind when I first had this question asked were involving sorting or priority queues, thus at least O(n log n) running time and involving additional space. But when I asked about the desired running time I was told that it should be faster, so I revisited my thoughts. It is in fact a very simple problem where you have to traverse the array just once resulting in O(n) running time. Note that you also have to ask about how to handle duplicates, in this case I considered an array of unique numbers.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">The idea is that while traversing the array, you maintain a flag that indicates whether a "less than" or a "greater than" comparison should succeed. If it does not succeed you swap the previous index with the current index. </div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">This will be easier to understand through an example. Let's say we have the above array {3, 2, 6, 4, 9, 7, 1, 10, 8, 5}. We traverse the array starting at the second element (2), since we'll be comparing the current with the previous element. We also have to maintain the above mentioned flag, say a flag named <i>lessThan </i>which will be defaulted to true since our format definition indicates that we have to start from a less than sign. So we have our element "2" and previous element "3". Is element "3" less than element "2" ? No, then we swap the elements which results in the array {2, 3, 6, 4, 9, 7, 1, 10, 8, 5}. We also have to switch the <i>lessThan</i> flag and then go to the next index. Now the flag tells us that we have to use the "greater than" sign. Is element "3" greater than element "6" ? No, then swap the numbers. But wait, what happens to the previous comparison ? The second index has to stay greater than the first one, but note that this did get verified by the condition that we just imposed by comparing "6" with "3". "6" is greater than "3", which is greater than "2" - this means "6" is greater than "2", thus it's safe swapping "6" with "3". This results in {2, 6, 3, 4, 9, 7, 1, 10, 8, 5}. This goes on until the end of the array. Here's a table that may add more sense to how the algorithm works:</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><br /></div><table border="1" cellpadding="3" cellspacing="3"><tbody><tr><th>Iteration</th><th>Array</th><th>LessThan flag</th><th>Swapped</th></tr><tr><td>0</td><td>2, 3, 6, 4, 9, 7, 1, 10, 8, 5</td><td>true</td><td>true</td></tr><tr><td>1</td><td>2, 6, 3, 4, 9, 7, 1, 10, 8, 5</td><td>false</td><td>true</td></tr><tr><td>2</td><td>2, 6, 3, 4, 9, 7, 1, 10, 8, 5</td><td>true</td><td>false</td></tr><tr><td>3</td><td>2, 6, 3, 9, 4, 7, 1, 10, 8, 5</td><td>false</td><td>true</td></tr><tr><td>4</td><td>2, 6, 3, 9, 4, 7, 1, 10, 8, 5</td><td>true</td><td>false</td></tr><tr><td>5</td><td>2, 6, 3, 9, 4, 7, 1, 10, 8, 5</td><td>false</td><td>false</td></tr><tr><td>6</td><td>2, 6, 3, 9, 4, 7, 1, 10, 8, 5</td><td>true</td><td>false</td></tr><tr><td>6</td><td>2, 6, 3, 9, 4, 7, 1, 10, 8, 5</td><td>false</td><td>false</td></tr><tr><td>7</td><td>2, 6, 3, 9, 4, 7, 1, 10, 5, 8</td><td>true</td><td>true</td></tr></tbody></table><br /><br /><br />The code is pretty simple. See it below: <script src="https://gist.github.com/pavelfusu/e3d4eaa3eedd4e56e0e4.js"></script><br />Note:<br />- The swap function is trivial and it may look like in the code below. <script src="https://gist.github.com/pavelfusu/cdd04be7bddd7efa193b.js"></script>Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com5tag:blogger.com,1999:blog-1040933993394574727.post-73258230204711824352013-06-20T18:31:00.000-07:002013-07-11T07:00:45.145-07:00Validating a Binary Search Tree<div style="text-align: justify;">This is one of those problems that deceives you into thinking that you got it and if you're not careful enough you'll make a mistake. The problem is that when deciding if a tree is a <a href="http://en.wikipedia.org/wiki/Binary_search_tree" target="_blank">binary search tree</a> or not, you not only have to make sure that the left child is lesser than the root and right child larger; you also have to make sure that the left child is lesser than all its ancestors and right child is larger than all its ancestors. To do this we'll at least need to go through all the nodes. Thus we need a <a href="http://en.wikipedia.org/wiki/Tree_traversal" target="_blank">traversal algorithm</a> either recursive or iterative. Both the recursive and the iterative will take up to O(n) space (the call stack for recursive and a stack data structure for iterative).<br /><br />I'll start with the iterative algorithm, and I'll take as an example the tree below.<br /><br /><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody><tr><td style="text-align: center;"><a href="http://2.bp.blogspot.com/-31SLzJBPgj0/UbpLFhCVvyI/AAAAAAAABkc/aYRpMDI92MA/s1600/bstexample.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img alt="Simple Binary Search Tree" border="0" src="http://2.bp.blogspot.com/-31SLzJBPgj0/UbpLFhCVvyI/AAAAAAAABkc/aYRpMDI92MA/s1600/bstexample.png" title="" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">Fig. 1 - Simple Binary Search Tree</td></tr></tbody></table><br />The idea of the iterative one is that a binary search tree traversed <a href="http://en.wikipedia.org/wiki/Tree_traversal#In-order" target="_blank">in-order</a> will always output a ordered sequence. So the above tree traversed in-order will look like this: 16, 25, 41, 42, 46, 53, 55, 60, 62, 63, 64, 65, 70, 74. And, as a reminder, this happens because an in-order traversal visits the left branch first, then the root, and only in the end the right child - which, according to the binary search tree property where the left branch is less than the root, and the right one is larger than the root; leads us into a sorted sequence. Having this said, it would be enough for us to maintain the last visited node's value and keep traversing while the current node's value is larger than the node that we traversed last. If not - this is not a sorted sequence, thus definitely not a binary search tree.</div><br /><script src="https://gist.github.com/pavelfusu/36394e1e78f334fd0569.js"></script> <br /><div style="text-align: justify;">The recursive version may be slightly difficult to understand, but it may be clearer in a way of showing how the ancestors are checked against the binary search property. This means that the children of a specific node are always limited by an upper and lower limit, so that a binary search tree can happen. For example, the children on the left branch of node "65" in the picture above have to be less than 65 and greater than 60 in order to conform to the binary search property. But what are the children of the left/right branches of root node? Well, left children of the root node, have to be less than 60 and greater than -infinity, and children on the right, have to be greater than 60 and less than infinity. </div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">I still prefer the iterative version over the recursive one - it's clean enough and besides in a production environment it may be a better idea to use the iterative version. But anyways here's the code below. <script src="https://gist.github.com/pavelfusu/f624261797ac1daccc38.js"></script></div>Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com35tag:blogger.com,1999:blog-1040933993394574727.post-50966424609440425892013-06-19T17:45:00.003-07:002013-07-11T07:02:01.327-07:00Coin Flipping and Die Rolls<div style="text-align: justify;">Given a function that outputs a coin toss (a random number from 1 to 2) describe an algorithm that outputs a die roll (a random number from 1 to 6). Each outcome should be equally likely.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">One solution is tossing the coin 3 times and using the outcome as bits of a three-bit number. So our outcomes would be 000, 001, 010, 011, 100, 101, 110, 111 (0 to 7). If our outcome is 110 or 111 we would have to discard it and toss again.</div><br /><script src="https://gist.github.com/pavelfusu/30983c88e027e9fd37df.js"></script> <br /><div style="text-align: justify;">I found this solution widely accepted on the web, but it seems that discarding 3 tosses is a waste. It seems like we could reuse the lastly tossed number (in the case of 110 and 111 - 0 and 1), and we would need only 2 additional tosses instead of 3. This would change the algorithm to something like this:<br /><br /></div><script src="https://gist.github.com/pavelfusu/9f79b20144b7904617eb.js"></script> The toss function could reuse the random number method mentioned <a href="http://www.fusu.us/2013/06/p5-deck-shuffling.html">earlier</a>.<br /><br /><script src="https://gist.github.com/pavelfusu/0d5d6038eb0dee225bed.js"></script> <script src="https://gist.github.com/pavelfusu/dcc8d07b9f934943ee76.js"></script>Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com6tag:blogger.com,1999:blog-1040933993394574727.post-85718419372514217712013-06-19T11:27:00.002-07:002013-07-11T07:03:40.309-07:00Minimum Stack<div style="text-align: justify;">Another common interview question is implementing a <a href="http://simple.wikipedia.org/wiki/Stack_(data_structure)">stack</a> like data structure that besides supporting the traditional push() and pop() operations would provide a O(1) operation able to find the minimum element in the stack. The question is easy but I still decided to write it down because it seemed elegant to me.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">So the solution consists in maintaining 2 stacks: one for performing the traditional operations as before, and another one that will always have the minimum on top for fast O(1) access. Imagine you have a set of numbers - 5, 2, 8, 5, 7, 13, 3, 11, 1, 6 and you start pushing them on the stack. Every time you push one number on the stack you compare it with the number on top of the other stack, and if it's smaller, you push it on the second stack too. This way after pushing all the numbers on both stacks you should have the second stack looking like this: 5, 2, 1 and the first stack looking as mentioned before like this 5, 2, 8, 5, 7, 13, 3, 11, 1, 6.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">We also need to maintain the minimum when pop-ing elements from our new data structure. We pop from the first stack unconditionally, but from the second stack we check first if the element we just popped from the first stack is actually the element on top of the second stack; if so - we pop it from the second too.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">This is how it would look if we pop all elements from the stack:</div><div style="text-align: justify;"><br /></div><table border="1" cellpadding="3" cellspacing="3"><tbody><tr><th>Iteration</th><th>Stack 1</th><th>Stack 2</th><th>Current minimum</th></tr><tr><td>0</td><td>5, 2, 8, 5, 7, 13, 3, 11, 1, 6</td><td>5, 2, 1</td><td>1</td></tr><tr><td>1</td><td>5, 2, 8, 5, 7, 13, 3, 11, 1</td><td>5, 2, 1</td><td>1</td></tr><tr><td>2</td><td>5, 2, 8, 5, 7, 13, 3, 11</td><td>5, 2</td><td>2</td></tr><tr><td>3</td><td>5, 2, 8, 5, 7, 13, 3</td><td>5, 2</td><td>2</td></tr><tr><td>8</td><td>5, 2</td><td>5, 2</td><td>2</td></tr><tr><td>9</td><td>5</td><td>5</td><td>5</td></tr><tr><td>10</td><td></td><td></td><td></td></tr></tbody></table><br />In code it looks very simple and short:<br /><br /><script src="https://gist.github.com/pavelfusu/ff370c604de0b72b5503.js"></script> Note:<br />- It is not necessary to use 2 stacks here. Another way was making MinimumStack a child of Stack. I just like symmetry and encapsulation. Besides it seems easier to show the idea this way.Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com0tag:blogger.com,1999:blog-1040933993394574727.post-21374521638651850902013-06-17T18:51:00.002-07:002013-07-11T07:04:51.399-07:00Shortest Path in a Binary Search Tree<div style="text-align: justify;">The shortest path in a <a href="http://en.wikipedia.org/wiki/Binary_search_tree">binary search tree</a> is not much more different than the shortest path in a <a href="http://en.wikipedia.org/wiki/Binary_tree">binary tree</a> that I handled <a href="http://www.fusu.us/2013/06/p6-shortest-path-in-binary-tree.html">here</a>. The difference consists in reusing the binary search tree property where the left branch nodes are less than the root, and the right branch nodes are larger than the root. As in the binary tree shortest path, the procedure consists of finding the lowest common ancestor which I described <a href="http://www.fusu.us/2013/06/p3-lowest-common-ancestor-in-binary.html?zx=209fc12f6a011e63">here</a> and then "drawing" the path from the ancestor to the nodes. Let's use the below tree as an example.<br /><br /><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody><tr><td style="text-align: center;"><a href="http://2.bp.blogspot.com/-31SLzJBPgj0/UbpLFhCVvyI/AAAAAAAABkc/aYRpMDI92MA/s1600/bstexample.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img alt="Simple Binary Search Tree" border="0" src="http://2.bp.blogspot.com/-31SLzJBPgj0/UbpLFhCVvyI/AAAAAAAABkc/aYRpMDI92MA/s1600/bstexample.png" title="" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">Fig. 1 - Simple Binary Search Tree</td></tr></tbody></table><br /></div><div style="text-align: justify;">Let's say we want to find the smallest path from "25" to "46". It is obvious that it has to go through the lowest common ancestor that we identify as "41". Now we have 2 basic cases: either one of the nodes we're looking for is the LCA and the other is in either of the branches; or each of the nodes is in a separate branch. Now if we don't have the first case, we can look for the minimum of the two nodes in the left branch and for the maximum in the right branch. And again, this is because of the binary search tree property - that the lower element goes on the left and higher on the right. So we go down on the left binary searching for our node "25" and same with "46". I say binary searching because at each step we compare the current node with the node we're looking for and decide whether to go to the right or to the left. Managing the path is tricky, since the path for the second element will come inverted with recursion. In my implementation I used a stack to manage that as I did when looking for the shortest path in a simple binary tree:</div><br /><script src="https://gist.github.com/pavelfusu/28c306db21e3dfc36ccb.js"></script> Note:<br />- First stack is not really needed, a simple list would do. I just like symmetry.<br />- For the lowestCommonAncestor method see <a href="http://www.fusu.us/2013/06/p3-lowest-common-ancestor-in-binary.html?zx=209fc12f6a011e63">here</a>.Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com1tag:blogger.com,1999:blog-1040933993394574727.post-72855586985115588832013-06-17T17:59:00.002-07:002013-07-11T07:05:10.459-07:00Shortest Path in a Binary Tree<div style="text-align: justify;">This one may be from the tougher category, but it becomes easy when you understand the idea. So the question is "what is the shortest route between 2 unique nodes in a <a href="http://en.wikipedia.org/wiki/Binary_tree">binary tree</a>". I'll handle the same problem but in a <a href="http://en.wikipedia.org/wiki/Binary_search_tree">binary search tree</a> in a separate article. I knew the answer before, since I reviewed the lowest common ancestor in a binary tree first. Yes, the idea is to find first the lowest common ancestor of the two nodes and then "draw the path" from the LCA to the 2 nodes. I explained finding the LCA in a binary tree in <a href="http://www.fusu.us/2013/06/p2-lowest-common-ancestor-in-binary-tree.html?zx=788b554269b281d2">this article</a>, so I'll go straight to the second part: identifying the path. Let's use the below tree as an example.<br /><br /></div><div style="text-align: justify;"><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody><tr><td style="text-align: center;"><a href="http://4.bp.blogspot.com/-dhL3ReoGlxw/UbpgJm7WcvI/AAAAAAAABkc/nUg1h8nWhcQ/s1600/binarytree.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img alt="Simple Binary Tree" border="0" src="http://4.bp.blogspot.com/-dhL3ReoGlxw/UbpgJm7WcvI/AAAAAAAABkc/nUg1h8nWhcQ/s1600/binarytree.png" title="" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">Fig. 1 - A Simple Binary Tree</td></tr></tbody></table></div><div style="text-align: justify;"><br />Let's say we want to find the shortest path between nodes "2" (last one) and "11". Having identified the LCA as "7", we can now assume that the nodes are either located each in its separate branch or one node <u><b>is</b></u> the LCA and the second is either in the left or right branch. Second option is not the case, so we traverse the left and right branch (recursion possible) until we find any of the two nodes by comparing the branch node with the nodes. First one is easy we go onto the left branch and we find the "2" node right away. Then we go onto the right branch - "6", not our node, recurse on the left and right branch of "6". "5" is not our node, "11" is, so we add it to our path. The final path is "2, 7, 6, 11". How would we manage to maintain the path in code though? If we use recursion we'll get a path like "2, 7, 11, 6", so we need some little adjustments. </div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">You may notice that the first path is ok up to the root, then we have an inverted path. How do we handle this? There's probably multiple ways, I chose having a stack to invert the list as you can see in the code below.</div><br /><script src="https://gist.github.com/pavelfusu/c52e8a1869a2f42ed550.js"></script> Note:<br />- First stack is not really needed, a simple list would do - I just like symmetry.<br />- For the lowestCommonAncestor method see <a href="http://www.fusu.us/2013/06/p2-lowest-common-ancestor-in-binary-tree.html?zx=788b554269b281d2">here</a>.Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com7tag:blogger.com,1999:blog-1040933993394574727.post-14093424384389813042013-06-16T15:47:00.000-07:002013-07-11T07:05:40.790-07:00Deck Shuffling<div style="text-align: justify;">How would you <a href="https://en.wikipedia.org/wiki/Shuffling">shuffle </a>a deck of cards or generally an array of distinct integers? The shuffle should happen in such a way that every possible reordering is equally likely. Of course it sounds like we should use a random number, but how to ensure the equal probability of each reordering?</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">There's probably multiple solutions, but the one I liked is a solution that takes O(n) time and no additional space. It is the <a href="http://en.wikipedia.org/wiki/Fisher-Yates_shuffle">Fisher-Yates shuffle</a>. What we have to do is traverse the array once and swap each element with a random element that doesn't appear before the current index.</div><br />The code is very simple and looks like this:<br /><br /><br /><script src="https://gist.github.com/pavelfusu/162cab1d10cf1eb7de1d.js"></script> Note:<br />- The random method is a method that generates a random number within the specified range. This is trivial and could look like this:<br /><script src="https://gist.github.com/pavelfusu/dcc8d07b9f934943ee76.js"></script>- The swap method is a method that swaps 2 elements in an array with the specified index, the implementation is also trivial. It may look like this:<br /><script src="https://gist.github.com/pavelfusu/cdd04be7bddd7efa193b.js"></script>Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com0tag:blogger.com,1999:blog-1040933993394574727.post-72033892235124343702013-06-16T14:13:00.000-07:002013-07-11T07:06:22.902-07:00Order Statistics. Kth Smallest or Largest Element in an Un-ordered Array<div style="text-align: justify;">Finding the largest or smallest element in an un-ordered array in O(n) is a trivial and obvious task, but what about finding the third largest or sixth or abstractly speaking kth? In statistics this is called <a href="http://en.wikipedia.org/wiki/Order_statistic">Order Statistic</a>. One way of solving this is using the <a href="http://en.wikipedia.org/wiki/Quicksort">QuickSort </a>partitioning technique. To remind you how this works: you choose a pivot element, and then move all elements less than the pivot element to the left and all elements that are larger than the pivot element to the right. After this operation the pivot element is on the right spot and if the k you're looking for equals to the pivot's index, you don't have to sort anymore and just return the pivot.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">As an example, let's say I have the following array:</div><div style="text-align: justify;">[<span style="color: red;">42</span>, 92, 31, 73, 46, 11, 29, 53, 64, 4]</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">I chose the first element as the pivot (or we could use a random element) - 42.</div><div style="text-align: justify;">Now we iterate from the start and end of the array and swap the elements<a href="https://en.wikipedia.org/wiki/In-place_algorithm"> in-place</a> as they don't conform to our property (less than the pivot on the left and larger on the right). Is 92 less than 42 ? No, whom do we switch it with ? Let's look at the elements at the end of the array, since in the end we want to have an ascending order sorted array (if we need to). Is 4 larger than 42 ? No! Swap this with 92. Is 31 less than 42? Good. 73? No, then swap, and so on.</div><div style="text-align: justify;">After the first operation the array should look like this:</div><div style="text-align: justify;">[11, 4, 31, 29, <span style="color: red;">42</span>, 46, 73, 53, 64, 92]</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Now, 42 is in the right spot and it's the 5th number (index 4). Let's say we're looking for the 2nd smallest (index 1) so 5 is no good to us. What we do now, is doing the same thing just over the smaller array that we have: </div><div style="text-align: justify;">[<span style="color: red;">11</span>, 4, 31, 29]</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Again, chose a pivot - 11, and move all lesser values to the left and larger to the right:</div><div style="text-align: justify;">[4, <span style="color: red;">11</span>, 31, 29]</div><div style="text-align: justify;">11 is the element with index number 1 thus it is the second smallest element so we can stop the algorithm here.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Computing the kth largest element is the same task, with the only difference that we have to translate the k value, since the largest elements are located at the end of the array. So we'll have to translate k using this formula: k = array.length - k + 1.</div>Here's the algorithm in code:<br /><br /><script src="https://gist.github.com/pavelfusu/02ade20cbf23bd2a4c0f.js"></script>Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com2tag:blogger.com,1999:blog-1040933993394574727.post-48082919580037473852013-06-13T18:35:00.000-07:002013-07-11T07:07:06.259-07:00Lowest Common Ancestor in a Binary Search Tree<div style="text-align: justify;">For the LCA in a simple binary tree go <a href="http://www.fusu.us/2013/06/p2-lowest-common-ancestor-in-binary-tree.html">here</a>.<br /><br />Finding the lowest common ancestor (LCA) in a <a href="http://en.wikipedia.org/wiki/Binary_search_tree">binary search tree</a> may be easy, but coming up with an elegant and quick solution may take some thought.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">A binary search tree is a ordered <a href="http://en.wikipedia.org/wiki/Binary_tree">binary tree</a> where the nodes on the left are always less than the root node and the nodes on the right are always larger than the root node.</div><br />Let's use the tree below as an example.<br /><br /><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody><tr><td style="text-align: center;"><a href="http://2.bp.blogspot.com/-31SLzJBPgj0/UbpLFhCVvyI/AAAAAAAABkc/aYRpMDI92MA/s1600/bstexample.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img alt="Simple Binary Search Tree" border="0" src="http://2.bp.blogspot.com/-31SLzJBPgj0/UbpLFhCVvyI/AAAAAAAABkc/aYRpMDI92MA/s1600/bstexample.png" title="" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">Fig.1 - Simple Binary Search Tree</td></tr></tbody></table><br /><div style="text-align: justify;">Now keeping in mind that property that makes the binary search tree different that a binary tree we can come up with something that is faster than finding the <a href="http://www.fusu.us/2013/06/p2-lowest-common-ancestor-in-binary-tree.html">LCA for a simple binary tree</a>, precisely we'll make it O(h) - where h is the height of the tree. This means that in the tree above it will take maximum 5 iterations to find the LCA.</div><br />Let's say we're looking for the nodes 62 and 70.<br /><div style="text-align: justify;">We take the maximum of these two and compare it to the root, if the maximum is less than the root then our values are in the left branch, and we can forget about the right branch at all. Again, we can do this because of the binary search tree property that all the nodes on the left of the root are less than the root. Since the maximum of our two numbers is not less than the root we can move to the second case.</div><div style="text-align: justify;">Now, we take the minimum of those two and compare it with the root. If the minimum is greater than the root, then we go on with the right branch. You may ask: why minimum (or maximum) ? The answer is that the nodes we're looking for might be each other's parent. So if our comparison would not be greater than the root, we would go to our third case which i'll take note later. Since this is the case the minimum of these two is 62 and it is greater the the root - we go further to 74. </div><div style="text-align: justify;">We keep doing our previous conditions, until both of them don't succeed:</div><div style="text-align: justify;">- max is less than 74, we go on the left to 65</div><div style="text-align: justify;">- max is not less than 65, then is min greater than 65 - not even that!</div><div style="text-align: justify;">Here we go to our third case: if both previous cases failed, this could not mean anything but that the root is the LCA of nodes we're looking for, which is 65.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Again in code this is nothing but a binary search:</div><br /><script src="https://gist.github.com/pavelfusu/3199cb49ed4aa6d901c2.js"></script> Note:<br />- Math.max and Math.min = determines the maximum and minimum of two numbers<br />- In a balanced binary search tree h = log n, so then we have O(log n)<br />For the node representation we'll use the following class:<br /><br /><script src="https://gist.github.com/pavelfusu/6f43ca8d65d54a513e48.js"></script> <br /><div class="separator" style="clear: both; text-align: center;"></div><br />Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com0tag:blogger.com,1999:blog-1040933993394574727.post-8493425540097584932013-06-13T18:01:00.000-07:002013-07-11T07:07:26.761-07:00Lowest Common Ancestor in a Binary Tree<div style="text-align: justify;">Another popular interview problem and the start of my tree review is finding the <a href="http://en.wikipedia.org/wiki/Lowest_common_ancestor">LCA </a>in a binary tree. Now, pay attention - not a <a href="http://en.wikipedia.org/wiki/Binary_search_tree">binary search tree</a>, that is a different case and will be handled in a <a href="http://www.fusu.us/2013/06/p3-lowest-common-ancestor-in-binary.html">separate article</a>; now will use a simple <a href="http://en.wikipedia.org/wiki/Binary_tree">binary tree</a>. For those who don't remember a binary search tree is a sorted binary tree where the left child is always smaller than the root and the right child is always larger. A simple binary tree, as in our case, doesn't have that property.</div><br />Let's use the tree below as an example.<br /><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody><tr><td style="text-align: center;"><a href="http://4.bp.blogspot.com/-dhL3ReoGlxw/UbpgJm7WcvI/AAAAAAAABkc/nUg1h8nWhcQ/s1600/binarytree.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img alt="Simple Binary Tree" border="0" src="http://4.bp.blogspot.com/-dhL3ReoGlxw/UbpgJm7WcvI/AAAAAAAABkc/nUg1h8nWhcQ/s1600/binarytree.png" title="" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">Fig.1 - Simple Binary Tree</td></tr></tbody></table><br /><div style="text-align: justify;">Now let's say we want to find the LCA for nodes 4 and 9, we will need to traverse the whole tree to compare each node so that we can locate the nodes. Now considering that we start the traversal with the left branch (<a href="http://en.wikipedia.org/wiki/Tree_traversal#Pre-order">pre-order traversal</a>) - we have the worst case here with O(n) running time.</div><div style="text-align: justify;">Traversing the tree we compare the current node with both of the nodes and if one of them match, it means that one is the LCA on the respective branch. Let's say after traversing the above tree in pre-order the first node that matches our nodes is 9 (2, 7, 2, 6, 5, 11, 5, 9). So the first obvious thought is that the 4 must be a child of 9, since we're already on the right child of node 5 and the pre-order traversal looks at the node first, then the left child and lastly the right child. Then we note node 9 as the LCA and we don't have to look further anymore.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Let's use another case, say we're looking for the LCA of 7 and 9. The first node in our pre-order traversal (2, 7, 2, 6, 5, 11, 5, 9, 4) is 7. Now here we can say that the LCA for the left branch is 7 because again, if the second node is in the same branch, independently of where and how deep it will be in this branch, the LCA will still be 7; thus we don't have to look in this branch anymore. But we still did not look at the right branch, so we keep traversing in a pre-order manner, but now omitting the other nodes: 2, 7, 5, 9. Now we can say that the LCA for that branch is 9. We can also affirm that the LCA for the branch with the root in node 5 is also 9. And in the end we have our nodes both in separate branches, which means that the LCA is the root of those branches - node 2.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">The algorithm looks as a modified version of a pre-order tree traversal :</div><div style="text-align: justify;"><script src="https://gist.github.com/pavelfusu/32cff556a403ae1f3cca.js"></script>For the node representation we'll use the following class: </div><div><script src="https://gist.github.com/pavelfusu/6f43ca8d65d54a513e48.js"></script></div><div style="text-align: justify;"><br /></div>Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com19tag:blogger.com,1999:blog-1040933993394574727.post-50367917816769122402013-06-12T18:05:00.001-07:002013-07-11T07:08:39.732-07:00Finding If N Is Palindrome<div style="text-align: justify;">Today I've decided that I want to review my basic computer science knowledge. And since I have a feeling that I may want to review this in the future again - I thought of making out of it a blogging experience. This way I kill two birds with one stone: blogging and review. Also even though there are lots of solutions and code sites on the web, this still may be of help to other colleagues of mine that plan to review.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">The way I'm going to do the review is by solving popular programming problems which may be also a good preparation for a job interview. For my review I'm going to be using the Java programming language, but it should not matter for experienced programmers, since I'm going to use basic language structures and no external libraries.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Today I'll start with a classic popular problem: <i>the palindrome</i></div><div style="text-align: justify;">For those who do not know what a palindrome is - it is a sequence of characters that can be interpreted in the same way independently of what direction is used to read them. For example "abba" is a palindrome because you can interpret this sequence in the same way independently of the direction you read the sequence.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">One known problem is determining if a number is a palindrome. It is usually limited to no extra memory. Let's say we have two numbers: 1781 and 42124. The latter one is the palindrome, and the way we do it programmatically would be by comparing the first and last digit and moving on to the second and n-1 digit and so on. Now the main problem is determining the first and last digit which consists of numerical operations. Last digit is easy - just get the reminder of the division by 10 (modulus) but first digit requires a little bit of thought. So in our number 1781, for the first digit we basically need to divide it by 1000, and we get 1 (781 reminder). How do you find this 1000 number ? - just go through the digits and multiply 1 with 10 n-1 times. (Line 7-9).<br /><br /></div><br /><script src="https://gist.github.com/pavelfusu/93829d3a3d91b8104d42.js"></script> <br /><div style="text-align: justify;">Now as we have the first and last number we can compare them and fail right away if they're not equal. Truncating the number from the last and first digit is fairly easy : 1781 % 1000 reminder is 781, and then just divide by 10, and we have the 78 number. (Line 17). Lastly decrease div by 2 digits (Line 18). All in all you get an O(n) solution.</div>Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com3tag:blogger.com,1999:blog-1040933993394574727.post-32533926589168656952010-01-20T15:51:00.000-08:002013-07-11T07:09:05.775-07:00What Dreams May Come<blockquote>“<i>To accomplish great things, we must not only act, but also dream; not only plan, but also believe.</i>”</blockquote><div style="text-align: right;"><b>Anatole France</b></div><br /><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Dream and Believe – these two words have been around me for the last five years. They have helped me reach goals, which years before I had never thought they could have been even possible for me to achieve. They have helped me transform “Setting and reaching goals” into a challenge, which finally made me understand my way of being and employed me in a continuously growing environment.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">And the best part about this is when you accomplish that goal, which seemed to be impossible to reach and when you get that feeling which makes you say “I can do everything” or “Nothing is impossible” and challenge yourself to new and more difficult to achieve goals, which might seem impossible at all for other people, but not for you. Cause you… you <b>can do everything</b>.<br /><br /><br /><br /></div><div style="text-align: justify;"><div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-JbYs4Ne3c0A/S1eWI6L6JEI/AAAAAAAABkc/VbKhU_kiqgQ/s1600/NYC-Skyline-1.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" height="133" src="http://4.bp.blogspot.com/-JbYs4Ne3c0A/S1eWI6L6JEI/AAAAAAAABkc/VbKhU_kiqgQ/s200/NYC-Skyline-1.jpg" width="200" /></a></div><b>First, it was the dream.</b></div><div style="text-align: justify;">I love New York, I’m loving it since I was 14 years old. I was collecting images and movie clips about New York; I had a lot of souvenirs. I really loved it, and I never realized I could ever visit it. Victor Hugo said “<i>There is nothing like a dream to create the future</i>”; I had a dream, you might think it’s a small one; for me it was the most wanted and the most beautiful dream I ever had till that day. I didn’t realize that my dream is happening even when I was landing in J.F.Kennedy International Airport, in New York. I only realized it, when I was in that yellow Cab, heading to New York City, going up a hill. We were on the top of the hill, and I was talking to my friends, when all of the sudden everyone got quiet. I looked out of the window, and I was stunned. At that moment all the pictures that I had collected, the clips I had seen, seemed to be put together like a jigsaw puzzle...and here it was: the unbelievable - New York City. What I saw printed itself in my memory forever. All that immensely tall skyscrapers that make you feel so small and enclosed; the neon lights, and all that gorgeous bridges... I could almost see the busy streets, people running to work or getting stuck in the traffic. It was breathtaking. There is so much history hidden behind those places. At times it felt like all I wanted to do was to get out of the car and cry out of happiness. I have to admit that it gave me chills and a sense of accomplishment that I had never felt before. At that moment I realized: dreams may become reality, if you truly believe in them and do anything in your power to make that happen.<br /><br /></div><div style="text-align: justify;">After that experience, I became confident that if there is something I can put my mind up to, I can achieve it. I have to admit that it’s not as easy as it sounds. We have to go through experiences of trials, disappointment and face mistakes, but the idea behind it is that these are the things that make us stronger. These are the facts that made me feel confident about what I want, and when things went wrong, all I had to do was to find a better way to get there.<br /><br /></div><div style="text-align: justify;">Dream – everything starts with a dream. Don’t be afraid to dream big. So many people struggle to free their imagination. Einstein said “<i>Imagination is more important than knowledge.</i>” Napoleon said “<i>Imagination rules the world.</i>” We have a tremendous power in our dreams and our imagination and our creativity. Everybody needs a dream for life. It is psychologically healthy. Start living your dream. Make it a challenge. I dare you to dream! Be who you were designed to be.<br /><br /></div><div style="text-align: justify;"><b>What Dreams May Come.</b></div><div style="text-align: justify;">Last year, I had a dream. I enjoyed visiting the States, and afterwards I wanted to study there; I wanted something different for my education, something that would improve my skills, and something that would offer me a better future. I wanted to do a Master degree in the United States. I believed I have what it takes to get there. I knew I can make things possible; all I had to do is keep dreaming and doing my best to get there. That was my dream, and everything starts with a dream.</div><script type="text/javascript">var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script><br /><script type="text/javascript">try { var pageTracker = _gat._getTracker("UA-8352991-3"); pageTracker._trackPageview(); } catch(err) {}</script>Pavel Fusuhttp://www.blogger.com/profile/06602587189923519719noreply@blogger.com2