<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-8584007095819742992</atom:id><lastBuildDate>Wed, 08 Apr 2026 08:33:19 +0000</lastBuildDate><category>algorithms</category><category>Google interview</category><category>tricks</category><category>bit operations</category><category>binary trees</category><category>Microsoft interview</category><category>heap</category><category>linked list</category><category>sort</category><category>complexity</category><category>hash tables</category><category>pattern matching</category><title>World of BROCK</title><description>some algorithmic puzzles, tutorials, interview questions... and stuff...</description><link>http://worldofbrock.blogspot.com/</link><managingEditor>noreply@blogger.com (Unknown)</managingEditor><generator>Blogger</generator><openSearch:totalResults>30</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-6535873935616898260</guid><pubDate>Sun, 05 Jun 2011 17:54:00 +0000</pubDate><atom:updated>2011-06-05T20:54:25.125+03:00</atom:updated><title>More links :D</title><description>Once again, I&#39;d like to share one of my other websites that I&#39;ve build. Forex apparently wasn&#39;t such a great idea, didn&#39;t work out as expected, so I&#39;m trying something else. I&#39;m building a website for product reviews (either hardware or software). Check it out if you&#39;re interested: &lt;a href=&quot;http://www.bestpricedata.com&quot;&gt;Best price info&lt;/a&gt;. I&#39;m hoping it will go much better than the previous one (which still works, btw, just not as good). Online SEO tutorials have really helped.</description><link>http://worldofbrock.blogspot.com/2011/06/more-links-d.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>6</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-8819056518209677572</guid><pubDate>Sun, 05 Jun 2011 13:20:00 +0000</pubDate><atom:updated>2011-06-05T16:20:46.927+03:00</atom:updated><title>More info :D</title><description>Again, I&#39;d like to share another website I&#39;m working on. I&#39;ve designed it to post reviews for different hardware and software stuff which I find interesting. Here&#39;s a link to my first post! :) Enjoy! &lt;a href=&quot;http://www.bestpricedata.com/&quot;&gt;Patriot XPorter XT Boost review&lt;/a&gt;</description><link>http://worldofbrock.blogspot.com/2011/06/more-info-d.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-7885610816402066523</guid><pubDate>Tue, 02 Nov 2010 20:11:00 +0000</pubDate><atom:updated>2010-11-22T20:42:40.878+02:00</atom:updated><title>Forex trading</title><description>Hi everyone!!! Sorry for the lack of posts, I know it looks like this blog was abandoned, but it&#39;s not (not completely :) ).&lt;br /&gt;
&lt;br /&gt;
I&#39;ve been busy with trying out some online Forex trading with some degree of success.&lt;br /&gt;
If anyone out there is interested, please make sure to check out this awesome MetaTrader 4 indicator that I wrote (using my full expertise as a software developer):&lt;br /&gt;
&lt;blockquote&gt;&lt;b&gt;&lt;a href=&quot;http://www.intelligentsoftware.info/&quot;&gt;http://www.intelligentsoftware.info/&lt;/a&gt;&lt;/b&gt;&lt;/blockquote&gt;&lt;br /&gt;
I took these algorithms to the next level by using neural networks to build the ultimate indicator which tells you if the price goes up or down.&lt;br /&gt;
&lt;br /&gt;
Good luck and have fun!</description><link>http://worldofbrock.blogspot.com/2010/11/forex-trading.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-6835523806949833997</guid><pubDate>Sat, 13 Mar 2010 15:54:00 +0000</pubDate><atom:updated>2010-03-13T17:58:18.369+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><category domain="http://www.blogger.com/atom/ns#">tricks</category><title>How to print a spiral of numbers 1 through N</title><description>Printing the numbers 1 through N in a spiral is asked on interviews sometimes and is a interesting puzzle to solve. The reverse process is also simple but I will not write it here.&lt;br /&gt;
&lt;br /&gt;
The code here is quite simple, but it does not print the numbers  thorough N in a spiral, instead it prints a matrix with size N in which numbers are arranged into a spiral. Assuming N is given (16 for example), identifying the matrix size is simply of matter of finding sqrt(n).  The idea is to print the outer layer and move inward progressively until reaching the most inner layer.&lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;preproc&quot;&gt;#define&lt;/span&gt; N 4&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; mat[N][N];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; fillSpiral() {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; start = 0;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; end = N;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; k = 0;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;while&lt;/span&gt; (end - start &amp;gt;= 1) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = start; i &amp;lt; end; i++) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;            mat[start][i] = k;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;            ++k;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = start + 1; i &amp;lt; end; i++) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;            mat[i][end - 1] = k;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;            ++k;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = end - 2; i &amp;gt;= start; i--) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;            mat[end - 1][i] = k;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;            ++k;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  20:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  21:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = end - 2; i &amp;gt;= start + 1; i--) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  22:  &lt;/span&gt;            mat[i][start] = k;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  23:  &lt;/span&gt;            ++k;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  24:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  25:  &lt;/span&gt;        ++start;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  26:  &lt;/span&gt;        --end;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  27:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  28:  &lt;/span&gt;}&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  29:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  30:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  31:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; main() {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  32:  &lt;/span&gt;    memset(mat, 0, &lt;span class=&quot;kwrd&quot;&gt;sizeof&lt;/span&gt;(mat));&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  33:  &lt;/span&gt;    fillSpiral();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  34:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; N; i++) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  35:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; j = 0; j &amp;lt; N; j++) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  36:  &lt;/span&gt;            cout &amp;lt;&amp;lt; mat[i][j] &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;\t&quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  37:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  38:  &lt;/span&gt;        cout &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  39:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  40:  &lt;/span&gt;    cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  41:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; 0;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  42:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
And the result is something like this (notice the spiral):&lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;0       1       2       3&lt;/pre&gt;&lt;pre&gt;11      12      13      4&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;10      15      14      5&lt;/pre&gt;&lt;pre&gt;9       8       7       6&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;</description><link>http://worldofbrock.blogspot.com/2010/03/how-to-print-spiral-of-numbers-1.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-4652692465818648536</guid><pubDate>Sat, 27 Feb 2010 09:55:00 +0000</pubDate><atom:updated>2010-02-27T11:55:23.180+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">complexity</category><category domain="http://www.blogger.com/atom/ns#">Google interview</category><category domain="http://www.blogger.com/atom/ns#">hash tables</category><title>Data structures</title><description>&lt;b&gt;What is a linked list? &lt;/b&gt;&lt;br /&gt;
&lt;b&gt;What is the structure like? &lt;/b&gt;&lt;br /&gt;
See &lt;a href=&quot;http://worldofbrock.blogspot.com/2009/09/test1.html&quot;&gt;my first post&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What is the complexity of finding a specific element in a linked list? &lt;/b&gt;&lt;br /&gt;
Hint: &lt;i&gt;It&#39;s O(n).&lt;/i&gt;&lt;br /&gt;
• An excellent answer will discuss implementation, structure, operational complexity of common operations, and examples of usage. &lt;br /&gt;
• A good answer will discuss an example implementation or usage, but will fail to include internal details or complexity analysis. &lt;br /&gt;
• A poor answer would be not knowing about linked lists or confusing them with another data structure. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Compare a linked-list to a vector. &lt;/b&gt;&lt;br /&gt;
Hint: &lt;i&gt;A vector is a single contiguous memory block containing all the elements whereas a linked list is made up of several memory blocks (elements) linked together through the &quot;next&quot; pointer. Access to an element of the array can be made directly in O(1) but in a list, to find the n&lt;sup&gt;th&lt;/sup&gt; element, you must iterate over the previous n elements, therefore the complexity is O(n). An array is fixed in size so to extend it you must reallocate a different memory block and copy all the existing data to the new allocated block; in a linked list you can simply add a new element which is linked to the last element, so it&#39;s much faster.&lt;/i&gt;&lt;br /&gt;
• An excellent answer will discuss the implementation differences, different pattern of memory usage and access, and mention some use cases where one is more appropriate than the other. &lt;br /&gt;
• A good answer will discuss the implementation differences and elaborate on some examples. &lt;br /&gt;
• An acceptable answer will merely mention the access time differences. &lt;br /&gt;
• A poor answer will make a technical mistake or not know one or both data structures. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What is a hash table? What is it used for? &lt;/b&gt;&lt;br /&gt;
Hint: &lt;i&gt;The following description applies for all questions about maps. A hash table is a structure which uses hash functions to store and read items. When a key-value pair is inserted, the key is transformed through a hash function into an integer which represents the index in an array where the key-value pair will be stored. Because of this hash function, the insertion and deletions are O(1) in complexity. Therefore, most hash tables are implemented using an array. Hash tables are particularly efficient when the maximum number of entries can be predicted in advance, so that the array can be allocated once with the optimum size and never resized. If hash collisions occur, one usual solution is to store a list of items at each index in the array. This list will contains key-value pairs for which all the keys have the same hash value. &lt;/i&gt;&lt;br /&gt;
• An excellent answer will describe an O(1) read and write data structure with scaling memory usage and give several examples of where it is useful (e.g. caching, lookup tables with irregular keys). &lt;br /&gt;
• A good answer will describe an O(1) read and write data structure and give an example of where it is useful. &lt;br /&gt;
• An acceptable answer will mention constant-time access. &lt;br /&gt;
• A poor answer will give the access time as something other than constant-time, or confuse it with a tree or other structure. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;How can a Map data structure be implemented? (By following the question regarding hash tables, this should be a gimme.) &lt;/b&gt;&lt;br /&gt;
• An excellent answer will cite at least the hash table-backed and the tree-backed implementations and note the different performance characteristics of each, possibly mentioning that the tree-backed implementation can provide some features not available in a hash-backed implementation (e.g. sorting). &lt;br /&gt;
• A good answer will cite both hash table and tree implementations. &lt;br /&gt;
• An acceptable answer will cite either the hash table or tree implementation. &lt;br /&gt;
• Being unable to answer this question is unacceptable. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What is a binary tree?&lt;/b&gt;(This question follows the Map question to allow people who forgot non-hash table solutions to realize and revise their omission. It is good for candidates who only mentioned the hash table implementation of a map to point out that a tree is also an alternative implementation after this question is asked.) &lt;br /&gt;
• An excellent answer will describe a binary tree as distinct from a ternary or general tree, and will note that a binary search tree or balanced tree are distinct sub-categories of the generic binary tree. &lt;br /&gt;
• A good answer will describe a generic tree where every node has no more than two children and will cite the O(log N) access time where N is the depth/height of the tree. &lt;br /&gt;
• An acceptable answer will describe a tree where every node has no more than two children. &lt;br /&gt;
• A poor answer will respond by describing a binary search tree or a non-binary tree, and will not respond to gentle hinting or prodding back to the specific question that was asked. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What is the operational complexity of inserting an element into a hash table? &lt;/b&gt;&lt;br /&gt;
• An excellent answer will correctly cite O(1) complexity and then consider details include about cases in which a collision occurs and common methodologies for resolving hash collisions (i.e. chaining, linear or quadratic probing, secondary hash functions, etc.) are needed. &lt;br /&gt;
• A good answer will cite O(1) complexity but will not include collision considerations. &lt;br /&gt;
• A poor answer will be incorrect. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;How is a hash table implemented? &lt;/b&gt;&lt;br /&gt;
• An excellent answer will discuss selecting a hashing algorithm, dividing the address space into buckets and tuning the number of buckets to the use-case, hash collisions and bucket overflow/conflict resolution (possibly mentioning several strategies for dealing with collisions), dynamic resizing of the hash table, and might even discuss performance optimizations and interactions with hardware caching. &lt;br /&gt;
• A good answer will note the use of a hashing algorithm on the keys and an array or vector of buckets, and will identify hash collisions as a problem and may cite at least one method of dealing with them. The candidate will be able to answer a follow-up about dynamic resizing. &lt;br /&gt;
• An acceptable answer will mention an array of buckets and using a hashing function on the key to index into the array. &lt;br /&gt;
• A poor answer will be technically incorrect or impossible, or will be for a different data structure than a hash table. &lt;br /&gt;
What is the operational complexity of (various operations) on a binary tree?If the candidate already answered this as part of the first phone screen, this question may be skipped. It may also be asked to ensure the candidate is consistent in responding correctly. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What is operational complexity? &lt;/b&gt;&lt;br /&gt;
Hint: &lt;i&gt;These notations describe different kinds of bounds on asymptotic growth rates. Search the web. You really should know what they all mean, if you want to work at GOOGLE.&lt;/i&gt;&lt;br /&gt;
• An excellent answer will distinguish between Big Oh, Big Omega, and Big Theta notations and describe each in detail, including how they relate to measures of run-time and space trade-offs. &lt;br /&gt;
• A good answer will describe Big Oh notation and how it relates to run-time of algorithms and common operations of abstract data types (ADTs). &lt;br /&gt;
• A poor answer will include vague or incorrect ideas regarding Big Oh notation and usage. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What is the operational complexity of inserting an element into a linked list? &lt;/b&gt;(Asking a clarifying question about singly-linked lists versus doubly-linked lists is unnecessary in this case, but clarifying requirements is always a good thing.) &lt;br /&gt;
• An excellent answer will correctly cite O(N) and note the edge cases such as empty lists and the beginning/ending of the list. &lt;br /&gt;
• An acceptable answer will correctly cite O(N). &lt;br /&gt;
• A poor answer will be incorrect.</description><link>http://worldofbrock.blogspot.com/2010/02/data-structures.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>4</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-7365513717807774911</guid><pubDate>Fri, 26 Feb 2010 18:59:00 +0000</pubDate><atom:updated>2010-02-26T20:59:36.397+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Google interview</category><title>Verbal communication tips</title><description>&lt;blockquote&gt;&lt;b&gt;Why are you leaving your current position? &lt;/b&gt;&lt;br /&gt;
• A good answer will be generally positive, seeking better challenges or goals. Contract expirations and familial obligations are also common. &lt;br /&gt;
• A poor answer would be ranting against the current employer or generally being negative. Honesty is not necessarily negativity. The key is how they view their departure, whether they seek self-improvement whatever the situation. &lt;/blockquote&gt;&lt;blockquote&gt;&lt;b&gt;Why do you want to work for GOOGLE? &lt;/b&gt;&lt;br /&gt;
• An excellent answer will be enthusiastic about GOOGLE and its business and problem space, demonstrating knowledge of our sites and familiarity with its use. &lt;br /&gt;
• A good answer will be enthusiastic about GOOGLE and show a desire to work in this environment and on our sorts of problems. &lt;br /&gt;
• A mediocre answer will demonstrate a lack of knowledge of our site and operations &lt;/blockquote&gt;&lt;blockquote&gt;&lt;b&gt;Tell me about a recent project that you have worked on. &lt;/b&gt;&lt;br /&gt;
• An excellent answer will discuss an in-depth project involving a complicated problem, have a clear description of the problem that explains why it was difficult, describe the solution chosen, and reference how the candidate was involved in the entire process and implementation. &lt;br /&gt;
• A good answer may involve a small project that was well-executed, or a large project working on a simple problem, or a complex problem for which a sub-optimal solution was chosen. &lt;br /&gt;
• An acceptable answer will describe a project in detail from problem to implementation. &lt;br /&gt;
• A poor answer will not give a clear explanation of what the purpose of the project was, or will neglect to mention a solution at all, or will say the candidate was not actually involved in the project being described. &lt;/blockquote&gt;</description><link>http://worldofbrock.blogspot.com/2010/02/verbal-communication-tips.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-7165705927695794726</guid><pubDate>Tue, 22 Dec 2009 09:38:00 +0000</pubDate><atom:updated>2009-12-22T12:30:06.865+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">tricks</category><title>How to hack daily puzzles on www.gameknot.com</title><description>What better way of wasting two hours of your life then hacking a website? :) Actually what I&#39;m going to show here is just a simple example of javascript function overriding, it doesn&#39;t even deserve the name &quot;hack&quot;. The site is not very secured, but the basic stuff (moves, canceling games etc.) is checked server side, so there&#39;s not much anybody could do.&lt;br /&gt;
&lt;br /&gt;
You need Firefox with the extension &quot;Firebug&quot; installed. Now, assuming you like chess and you have an account at &lt;a href=&quot;http://www.gamespot.com&quot;&gt;http://www.gamespot.com&lt;/a&gt;, notice that if &lt;a href=&quot;http://gameknot.com/chess-puzzle.pl?pz=1439&amp;daily=3&quot;&gt;you open a daily puzzle&lt;/a&gt;, then open the right-click menu and click &quot;View source&quot;, the whole solution is encrypted. However the javascript files, which handle moving and verifying your moves, are not encrypted in any way. Pay attention to this line:&lt;br /&gt;
&amp;lt;script type=&quot;text/javascript&quot; src=&quot;/js/chess-puzzle.js?122109&quot;&gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
You can use &lt;a href=&quot;http://javascript.about.com/library/blformat.htm&quot;&gt;the javascript formatter&lt;/a&gt; to make the file chess-puzzle.js a little more readable. &lt;br /&gt;
There are some interesting functions you can pay attention to. For example: &lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;function&lt;/span&gt; show_move_hint() //this displays a hint &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;str&quot;&gt;&quot;contest mode&quot;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;function&lt;/span&gt; display_move_hint(countdown) //this one ensures the hint &lt;span class=&quot;kwrd&quot;&gt;is&lt;/span&gt; displayed only once&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;function&lt;/span&gt; report_wrong_move() //this one tells the server you made a wrong move&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;function&lt;/span&gt; callback_record_move_solve() //this one records the puzzle &lt;span class=&quot;kwrd&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;str&quot;&gt;&quot;unsolved&quot;&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; you made more than 2 mistakes&lt;/pre&gt;&lt;/div&gt;What would happen if I would chose to override one of them? :) Or all, for that matter.  Well, I could not try it myself since I would probably get my account deleted, but you could: just open Firebug, as in the screen-shot below, and edit the last &amp;lt;script&amp;gt; tag in the &quot;head&quot; section, to fit the following:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&amp;lt;script type=&lt;span class=&quot;str&quot;&gt;&quot;text/javascript&quot;&lt;/span&gt;&amp;gt;&amp;lt;!--&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (top.location!=location) { top.location.href = location.href; }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;function&lt;/span&gt; show_move_hint() {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;//////&lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(contest_mode())&lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;//////&lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(show_move_hint_count &amp;gt; 0)count_hints2++;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;//////&lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt; count_hints1++;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;//////&lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(!node_hints[cur_solution_node])node_hints[cur_solution_node] = 1;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;//////&lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt; node_hints[cur_solution_node]++;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;//////&lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(count_hints1 == 1 &amp;amp;&amp;amp; count_hints2 == 0)report_solved(0);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;//////show_move_hint_count++;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;   display_move_hint(5);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;//////disable_hint_button();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;   }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;function&lt;/span&gt; display_move_hint(countdown) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;//////&lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(cur_mode != modes.SOLVE)&lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;true&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;//////&lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(cur_solution_node &amp;lt; 0 || cur_solution_node &amp;gt;= solution.length)&lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;   var sn = solution[cur_solution_node];&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  20:  &lt;/span&gt;   &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(!sn.moves.length)&lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  21:  &lt;/span&gt;   var cc = sn.moves[0].coords;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  22:  &lt;/span&gt;   var mv = bob.chess_decode_move(cc);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  23:  &lt;/span&gt;   var b_on = countdown % 2 ? 1 : 0;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  24:  &lt;/span&gt;   bob.update_cell_image(mv[0], mv[1], b_on);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  25:  &lt;/span&gt;   &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(show_move_hint_count &amp;gt; 1)bob.update_cell_image(mv[2], mv[3], b_on);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  26:  &lt;/span&gt;   countdown--;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  27:  &lt;/span&gt;   &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(countdown &amp;gt;= 0) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  28:  &lt;/span&gt;      display_hint_timer = window.setTimeout(&lt;span class=&quot;rem&quot;&gt;&#39;display_move_hint(&#39; + countdown + &#39;)&#39;, 300);&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  29:  &lt;/span&gt;      }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  30:  &lt;/span&gt;   }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  31:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  32:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;function&lt;/span&gt; report_wrong_move() {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  33:  &lt;/span&gt;//////   count_wrong_moves++;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  34:  &lt;/span&gt;//////   &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(contest_mode(1)) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  35:  &lt;/span&gt;//////      bob.b_allow_new_moves = 0;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  36:  &lt;/span&gt;//////      &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(count_wrong_moves &amp;gt;= 4) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  37:  &lt;/span&gt;//////         report_contest_wrong_move();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  38:  &lt;/span&gt;//////         alert(&lt;span class=&quot;rem&quot;&gt;&#39;Sorry, you have failed to solve this puzzle.\nPlease check back tomorrow for the correct solution and\na new puzzle and another chance to win the Grand Prize!&#39;);&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  39:  &lt;/span&gt;//////         top.location.href = location.href;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  40:  &lt;/span&gt;//////         &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  41:  &lt;/span&gt;//////         }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  42:  &lt;/span&gt;//////      pop_msg(&lt;span class=&quot;rem&quot;&gt;&#39;Processing, please wait...&#39;, 10000, 10000);&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  43:  &lt;/span&gt;//////      setTimeout(&lt;span class=&quot;kwrd&quot;&gt;function&lt;/span&gt;() {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  44:  &lt;/span&gt;//////         report_contest_wrong_move(); }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  45:  &lt;/span&gt;//////      , 1);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  46:  &lt;/span&gt;//////      &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  47:  &lt;/span&gt;//////    }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  48:  &lt;/span&gt;//////    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(count_wrong_moves == 1 || count_wrong_moves == 6)report_solved(0);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  49:  &lt;/span&gt;   var m = [&lt;span class=&quot;rem&quot;&gt;&#39;Wrong move&#39;, &#39;Not quite&#39;, &#39;Sorry&#39;, &#39;Incorrect&#39;];&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  50:  &lt;/span&gt;   pop_msg(m[Math.floor(Math.random() * m.length)] + &lt;span class=&quot;rem&quot;&gt;&#39;. Try again!&#39;, 1000);&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  51:  &lt;/span&gt;   }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  52:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  53:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;function&lt;/span&gt; callback_record_move_solve() {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  54:  &lt;/span&gt;   var sn = solution[cur_solution_node];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  55:  &lt;/span&gt;   &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(bob.b_checkmate) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  56:  &lt;/span&gt;      &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(!report_solved(1))&lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  57:  &lt;/span&gt;      var last_mv = split_move(bob.chess_get_last_move());&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  58:  &lt;/span&gt;      &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt;(var i = 0; i &amp;lt; sn.moves.length; i++) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  59:  &lt;/span&gt;         var mv = sn.moves[i];&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  60:  &lt;/span&gt;         &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(!same_move(mv, last_mv))continue;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  61:  &lt;/span&gt;         cur_solution_node = mv.goto_node;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  62:  &lt;/span&gt;         break;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  63:  &lt;/span&gt;         }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  64:  &lt;/span&gt;      start_solved_mode();&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  65:  &lt;/span&gt;      &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  66:  &lt;/span&gt;      }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  67:  &lt;/span&gt;   &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(bob.cur_to_move != puzzle_to_move) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  68:  &lt;/span&gt;      var last_mv = split_move(bob.chess_get_last_move());&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  69:  &lt;/span&gt;      var b_found = 0;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  70:  &lt;/span&gt;      var best_mtc =- 1;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  71:  &lt;/span&gt;      &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt;(var i = 0; i &amp;lt; sn.moves.length; i++) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  72:  &lt;/span&gt;         var mv = sn.moves[i];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  73:  &lt;/span&gt;         var mtc = solution[mv.goto_node].moves_to_checkmate;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  74:  &lt;/span&gt;         &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(best_mtc &amp;lt; 0)best_mtc = mtc;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  75:  &lt;/span&gt;         &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(best_mtc &amp;lt; mtc)break;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  76:  &lt;/span&gt;         &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(!same_move(mv, last_mv))continue;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  77:  &lt;/span&gt;         b_found = 1;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  78:  &lt;/span&gt;         break;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  79:  &lt;/span&gt;         }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  80:  &lt;/span&gt;      &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(b_found) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  81:  &lt;/span&gt;         cur_solution_node = mv.goto_node;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  82:  &lt;/span&gt;         sn = solution[cur_solution_node];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  83:  &lt;/span&gt;         var best_move = decide_best_move(cur_solution_node);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  84:  &lt;/span&gt;         mv = sn.moves[best_move];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  85:  &lt;/span&gt;         cur_solution_node = mv.goto_node;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  86:  &lt;/span&gt;         node_solution_moves[cur_solution_node] = 1;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  87:  &lt;/span&gt;         make_move(mv.coords, mv.promo);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  88:  &lt;/span&gt;         &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  89:  &lt;/span&gt;         }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  90:  &lt;/span&gt;/////////report_wrong_move();&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  91:  &lt;/span&gt;      bob.undo_last_move();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  92:  &lt;/span&gt;/////////&lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(!node_wrong_moves[cur_solution_node])node_wrong_moves[cur_solution_node] = 1;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  93:  &lt;/span&gt;/////////&lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt; node_wrong_moves[cur_solution_node]++;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  94:  &lt;/span&gt;      }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  95:  &lt;/span&gt;   bob.b_allow_new_moves = 1;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  96:  &lt;/span&gt;   update_last_move_cell_cursor();&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  97:  &lt;/span&gt;   }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  98:  &lt;/span&gt;// --&amp;gt;&amp;lt;/script&amp;gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;a href=&quot;http://img12.imageshack.us/i/screenshotdatefield.png/&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://img12.imageshack.us/img12/6382/screenshotdatefield.th.png&quot; border=&quot;0&quot; alt=&quot;Free Image Hosting at www.ImageShack.us&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
The lines prefixed with ////// need to be deleted. I just left them there so a comparison can be made: before and after. I wonder if the requests which go the server are as easily hacked as this.... Have fun!</description><link>http://worldofbrock.blogspot.com/2009/12/how-to-hack-daily-puzzles-on.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-4268756147660706945</guid><pubDate>Sat, 12 Dec 2009 17:40:00 +0000</pubDate><atom:updated>2009-12-12T19:41:50.154+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><category domain="http://www.blogger.com/atom/ns#">bit operations</category><category domain="http://www.blogger.com/atom/ns#">Google interview</category><category domain="http://www.blogger.com/atom/ns#">Microsoft interview</category><title>How to generate random numbers in [1...7] from random numbers in [1...5]</title><description>Well known as a Google/Microsoft interview question, the following problem is actually really complicated to solve correctly, although it looks easy:&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Given a function which produces a random integer in the range 1 to 5, write another function which uses it and produces a random integer in the range 1 to 7.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
I&#39;m going to modify this a little and make it generic: &lt;br /&gt;
&lt;i&gt;Given a function which produces a random integer in the range 1 to 5(M), write another function which uses it and produces a random integer in the range 0 to 7(N)&lt;/i&gt;.&lt;br /&gt;
&lt;br /&gt;
Of course it&#39;s easy to do it without generating a uniform distribution so I&#39;m not going to spend time on that. &lt;br /&gt;
What needs to be done is generate a uniform distribution. But consider this example:&lt;br /&gt;
If we add random(5) + random(5), assuming random(5) generates numbers from 0 to 4, we have:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;0+0&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;0+1, 1+0&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;0+2, 2+0, 1+1&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;0+3, 3+0, 1+2, 2+1&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;0+4, 4+0, 1+3, 3+1, 2+2&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;1+4, 4+1, 2+3, 3+2&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;2+3, 3+2, 3+3&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;3+4, 4+3&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;4+4&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
This obviously is not a uniform distribution because we have more chances of generating the number 4 than the number 8. So whatever algorithm is needed, it cannot simply use operations involving random(5). Or, actually it can, with some complicated math behind. Read about &lt;a href=&quot;http://en.wikipedia.org/wiki/Box-Muller_transform&quot;&gt;Box–Muller transformations&lt;/a&gt; and &lt;a href=&quot;http://en.wikipedia.org/wiki/Rejection_sampling&quot;&gt;Rejection sampling&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
What I think is a simpler algorithm is the following: since every number can be represented in X bits, generate X numbers from 1 to 5, and depending on the generated number, set bit X to 1 or 0. Number from 0 to 7 can be represented with 3 bits. Let me know what you think.&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; genRandom1to5() {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; (rand() % 5) + 1;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;}&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; genRandom1to7() {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; rand17 = 0;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; 3; i++) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; rand15 = 3;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;while&lt;/span&gt; (rand15 == 3) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;            rand15 = genRandom1to5();&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (rand15 &amp;lt; 3)&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;            rand17 |= (1 &amp;lt;&amp;lt; i);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; rand17;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;</description><link>http://worldofbrock.blogspot.com/2009/12/how-to-generate-random-numbers-in-17.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-6834999484908466137</guid><pubDate>Wed, 25 Nov 2009 18:15:00 +0000</pubDate><atom:updated>2009-11-25T20:17:22.343+02:00</atom:updated><title>Momentary break</title><description>M64FT7ZUTGK4&lt;br /&gt;
&lt;br /&gt;
Been very busy the last few weeks.  I will resume posting soon.&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;</description><link>http://worldofbrock.blogspot.com/2009/11/test.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-2127047328580381282</guid><pubDate>Sat, 31 Oct 2009 17:48:00 +0000</pubDate><atom:updated>2010-03-14T12:05:32.328+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><category domain="http://www.blogger.com/atom/ns#">Microsoft interview</category><category domain="http://www.blogger.com/atom/ns#">tricks</category><title>How to find the missing number from an array</title><description>This is what I&#39;ve been asked at an interview at Microsoft (over the phone): An array of length N (for example N = 6) is given, containing all the elements from 1 to N, except one which is duplicated. So instead of having:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;           1, 2, 3, 4, 5, 6&lt;/pre&gt;&lt;/div&gt;we have an array  which has one number duplicated:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;           1, 2, 3, 4, &lt;b&gt;4&lt;/b&gt;, 6&lt;/pre&gt;&lt;/div&gt;(notice a duplicated &quot;4&quot;). One additional detail: the array is not ordered. This is an final example of such an array:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;           3, 4, 1, 6, 4, 2&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
The question is: how do you find the number which is missing?&lt;br /&gt;
&lt;br /&gt;
There are multiple solutions:&lt;br /&gt;
&lt;br /&gt;
1. We could order the array as the first step. This would have &lt;a href=&quot;http://en.wikipedia.org/wiki/Computational_complexity_theory&quot;&gt;complexity&lt;/a&gt; O(n*log&lt;sub&gt;2&lt;/sub&gt;n) in the best case considering &lt;a href=&quot;http://worldofbrock.blogspot.com/2009/09/how-to-do-merge-sort.html&quot;&gt;merge sort&lt;/a&gt;, or &lt;a href=&quot;http://www.cs.auckland.ac.nz/software/AlgAnim/qsort.html&quot;&gt;quick sort&lt;/a&gt; in the average case. Next step would be to iterate through the array once, and check which number is the same as the previous one. We have the answer.&lt;br /&gt;
&lt;br /&gt;
2. Previous solution was O(n*log&lt;sub&gt;2&lt;/sub&gt;n) in time complexity and took no additional space to solve (depending on the sorting algorithm). A second solution would be to keep an extra array of boolean values, initialized with &lt;i&gt;false&lt;/i&gt;. We can iterate through the array once, and switch the corresponding boolean value from &lt;i&gt;false&lt;/i&gt; to &lt;i&gt;true&lt;/i&gt; as we go. Once we find a value which is already &lt;i&gt;true&lt;/i&gt;, we have the duplicated number. At the end, one of the values in the extra array will remain &lt;i&gt;false&lt;/i&gt;. This is the answer. This solution is O(n) in time complexity (much better), but also has O(n) space complexity.&lt;br /&gt;
&lt;br /&gt;
3. The last solution is the best but is a trick actually. Considering there are N numbers in the array, from 1 to N, it means their sum is (N*(N+1))/2. So if we do the sum in the array, it will be X, different than (N*(N+1))/2. Calculating the difference between the values gives us the answer to the questions in O(1) time complexity and O(1) space complexity.</description><link>http://worldofbrock.blogspot.com/2009/10/how-to-find-missing-number-from-array.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>9</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-104040521339241753</guid><pubDate>Fri, 30 Oct 2009 14:45:00 +0000</pubDate><atom:updated>2009-10-30T17:54:31.207+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><category domain="http://www.blogger.com/atom/ns#">Google interview</category><title>How to do integer partitioning</title><description>The problem statement: given a number N, for example 5, print &lt;a href=&quot;http://en.wikipedia.org/wiki/Partition_%28number_theory%29&quot;&gt;all the partitions of this number&lt;/a&gt;, meaning: the sum of all possible number that add up to N. In this example:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;        1+1+1+1+1&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;        1+1+1+2&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;        1+1+2+1&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;        1+1+3&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;        1+2+1+1&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;        1+2+2&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;        1+3+1&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;        1+4&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;        2+1+1+1&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;        2+1+2&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;        2+2+1&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;        2+3&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;        3+1+1&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;        3+2&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;        4+1&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;        5&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
The answer is obviously recursive, and the complexity is, of course O(2&lt;sup&gt;n - 1&lt;/sup&gt;), exponential.&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; doPartitions(&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; n, vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt;* partition) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (n &amp;lt;= 0) { &lt;span class=&quot;rem&quot;&gt;//the number cannot be partitioned further, so print&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; partition-&amp;gt;size() - 1; i++)&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;            cout &amp;lt;&amp;lt; partition-&amp;gt;at(i) &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;+&quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;        cout &amp;lt;&amp;lt; partition-&amp;gt;at(partition-&amp;gt;size() - 1) &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; size = partition-&amp;gt;size();&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 1; i &amp;lt;= n; i++) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;        &lt;span class=&quot;rem&quot;&gt;//add the current no to the partitions&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;        &lt;span class=&quot;rem&quot;&gt;//and generate partitions from the leftover number&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;        partition-&amp;gt;push_back(i);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;        doPartitions(n - i, partition);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;while&lt;/span&gt; (partition-&amp;gt;size() &amp;gt; size)&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;            partition-&amp;gt;pop_back(); &lt;span class=&quot;rem&quot;&gt;//reset the partitions each iteration&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
What&#39;s left here is to find a way to print only the distinct solutions, but this is a simple exercise.&lt;br /&gt;
&lt;br /&gt;</description><link>http://worldofbrock.blogspot.com/2009/10/how-to-do-integer-partitioning.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-3094670733591077185</guid><pubDate>Mon, 26 Oct 2009 19:03:00 +0000</pubDate><atom:updated>2009-10-26T21:06:14.295+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><category domain="http://www.blogger.com/atom/ns#">Google interview</category><title>How to find all possible words from a phone number</title><description>The problem statement: Most phone numbers these days are like 1-800-COOLSTUFF, which means you have to type 1-800-266578833 on your phone to dial. Given an array of numbers like &quot;266578833&quot; above, find all the words which could be formed from them. The problem is obviously &lt;a href=&quot;http://en.wikipedia.org/wiki/Exponential_growth&quot;&gt;exponential in complexity&lt;/a&gt;. Assuming each phone key holds 3 letters and having the array &quot;266578833&quot;, there are 3*3*3*3*3*3*3*3*3 possible combinations, which means 3&lt;sup&gt;9&lt;/sup&gt;. So any algorithm has to be at least O(3&lt;sup&gt;n&lt;/sup&gt;), where &lt;b&gt;n&lt;/b&gt; is the size of the array of numbers.&lt;br /&gt;
&lt;br /&gt;
The problem is easily solved in a recursive way: take each number and see which letters are assigned to it. Take each letter in turn, and each turn generate all possible combinations with the other letters:&lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;rem&quot;&gt;//keep here the vector of letters&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;&lt;span class=&quot;rem&quot;&gt;//like on position 5, the letters &quot;jkl&quot;, which are on key 5 of a phone&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt; words;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; generateWords(&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;* vec, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; size, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; index, vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;char&lt;/span&gt;&amp;gt;* currentWord) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (index == size) {&lt;span class=&quot;rem&quot;&gt;//print if we reached the end&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; size; i++)&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;            cout &amp;lt;&amp;lt; currentWord-&amp;gt;at(i);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;        cout &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//otherwise take each letter from the current key and add it to the stack&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//then generate combinations with the other keys&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; words[vec[index]].size(); i++) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;        currentWord-&amp;gt;push_back(words[vec[index]][i]);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;        generateWords(vec, size, index + 1, currentWord);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;        currentWord-&amp;gt;pop_back();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
To call this, &quot;words&quot; needs to be initialized, like this:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;    words.push_back(&lt;span class=&quot;str&quot;&gt;&quot;&quot;&lt;/span&gt;);    &lt;span class=&quot;rem&quot;&gt;//0&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;    words.push_back(&lt;span class=&quot;str&quot;&gt;&quot;.,&#39;&quot;&lt;/span&gt;); &lt;span class=&quot;rem&quot;&gt;//1&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    words.push_back(&lt;span class=&quot;str&quot;&gt;&quot;abc&quot;&lt;/span&gt;); &lt;span class=&quot;rem&quot;&gt;//2&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    words.push_back(&lt;span class=&quot;str&quot;&gt;&quot;def&quot;&lt;/span&gt;); &lt;span class=&quot;rem&quot;&gt;//3&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    words.push_back(&lt;span class=&quot;str&quot;&gt;&quot;ghi&quot;&lt;/span&gt;); &lt;span class=&quot;rem&quot;&gt;//4&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    words.push_back(&lt;span class=&quot;str&quot;&gt;&quot;jkl&quot;&lt;/span&gt;); &lt;span class=&quot;rem&quot;&gt;//5&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    words.push_back(&lt;span class=&quot;str&quot;&gt;&quot;mno&quot;&lt;/span&gt;); &lt;span class=&quot;rem&quot;&gt;//6&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;    words.push_back(&lt;span class=&quot;str&quot;&gt;&quot;pqrs&quot;&lt;/span&gt;);&lt;span class=&quot;rem&quot;&gt;//7&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;    words.push_back(&lt;span class=&quot;str&quot;&gt;&quot;tuv&quot;&lt;/span&gt;); &lt;span class=&quot;rem&quot;&gt;//8&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;    words.push_back(&lt;span class=&quot;str&quot;&gt;&quot;wxyz&quot;&lt;/span&gt;);&lt;span class=&quot;rem&quot;&gt;//9&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; size = 3;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; vec[] = { 2, 5, 8 };&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;    vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;char&lt;/span&gt;&amp;gt; currentWord;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;    generateWords(vec, size, 0, &amp;amp;currentWord);&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;</description><link>http://worldofbrock.blogspot.com/2009/10/how-to-find-all-possible-words-from.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-5857788316735409024</guid><pubDate>Fri, 23 Oct 2009 19:21:00 +0000</pubDate><atom:updated>2009-10-24T22:36:47.819+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><category domain="http://www.blogger.com/atom/ns#">bit operations</category><category domain="http://www.blogger.com/atom/ns#">Google interview</category><category domain="http://www.blogger.com/atom/ns#">tricks</category><title>How to find subsets of an array</title><description>The problem statement: given an array of integers, for example (2, 5, 8, 10), how do you print all the subsets of this array. By all the subsets, I mean:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;empty set&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;10&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;8&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;8 10&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;5&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;5 10&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;5 8&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;5 8 10&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;2&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;2 10&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;2 8&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;2 8 10&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;2 5&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;2 5 10&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;2 5 8&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;2 5 8 10&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
Well the answer is simple recursively: for each number in the array, compute recursively all the subsets without including it. Then include it and compute again all the subsets.&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; subsets_recursive(&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;* vec, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; size, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; index, vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt;* subset) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (index == size) { &lt;span class=&quot;rem&quot;&gt;//print if we reached the end&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (subset-&amp;gt;size() == 0)&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;            cout &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;empty set&quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; subset-&amp;gt;size(); i++)&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;            cout &amp;lt;&amp;lt; subset-&amp;gt;at(i) &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot; &quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;        cout &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; n = subset-&amp;gt;size();&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//first print all the subsets without including the number&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;    subsets_recursive(vec, size, index + 1, subset);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//clear the numbers that we added in the previous subsets&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;while&lt;/span&gt; (subset-&amp;gt;size() != n)&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;        subset-&amp;gt;pop_back();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//add the number to the subset and print all subsets including it&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;    subset-&amp;gt;push_back(vec[index]);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;    subsets_recursive(vec, size, index + 1, subset);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
Iteratively it&#39;s a different story. &lt;a href=&quot;http://en.wikipedia.org/wiki/Computational_complexity_theory&quot;&gt;Complexity&lt;/a&gt; for this (both recursively and iteratively) is O(2&lt;sup&gt;n&lt;/sup&gt;), where &quot;n&quot; is the size of the array. This means we have to iterate 2&lt;sup&gt;n&lt;/sup&gt; times. As it turns out, there&#39;s a trick involved: take each number i from 0 to 2&lt;sup&gt;n&lt;/sup&gt; and check each bit. If bit k from number i is 1, then consider array&lt;sub&gt;k&lt;/sub&gt; to be in the current subset. Too bad I didn&#39;t think of this during the interview. Here it is:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; subsets_iterative(&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;* vec, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; size) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; n = pow((&lt;span class=&quot;kwrd&quot;&gt;double&lt;/span&gt;)2, (&lt;span class=&quot;kwrd&quot;&gt;double&lt;/span&gt;)size);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt; currentSubset;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; n; i++) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;        currentSubset.clear();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; j = 0; j &amp;lt; size; j++)&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;            &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (((i &amp;gt;&amp;gt; j) &amp;amp; 1) != 0)&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;                currentSubset.push_back(vec[j]);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (currentSubset.empty())&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;            cout &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;empty set&quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; currentSubset.size(); i++)&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;            cout &amp;lt;&amp;lt; currentSubset.at(i) &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot; &quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;        cout &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;</description><link>http://worldofbrock.blogspot.com/2009/10/how-to-find-subsets-of-array.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>4</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-5488941070883142775</guid><pubDate>Mon, 19 Oct 2009 17:08:00 +0000</pubDate><atom:updated>2009-10-19T22:08:02.302+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><category domain="http://www.blogger.com/atom/ns#">Google interview</category><category domain="http://www.blogger.com/atom/ns#">tricks</category><title>How to do the product trick on a array</title><description>The problem is: given an array of numbers, replace each number with the &lt;b&gt;product&lt;/b&gt; of all numbers except itself,&lt;b&gt; without using the divide operator &quot;/&quot;&lt;/b&gt;.&lt;br /&gt;
Quite tricky. Of course the goal is to make the program as fast as possible.&lt;br /&gt;
&lt;br /&gt;
First solution: O(n&lt;sup&gt;2&lt;/sup&gt;) - having a temporary array, iterate through the array. At each iteration, iterate again through the array, skipping the current number and keeping the rest of the product in the temporary array. Kids stuff.&lt;br /&gt;
&lt;br /&gt;
Second solution: O(n) - having two temporary arrays, iterate once through the array and keep the product of all numbers smaller (in index) than the current number. Iterate again in reverse and keep a product of all the numbers bigger (in index) than the current number. At the end, the answer is the product of these two vectors. Example:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;array :  1  2  3  4&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;left  :  1  1  2  6 &amp;lt;--at each index &lt;span class=&quot;kwrd&quot;&gt;is&lt;/span&gt; the product of all the previous numbers&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;rights: 24 12  4  1 &amp;lt;--at each index &lt;span class=&quot;kwrd&quot;&gt;is&lt;/span&gt; the product of all the next numbers&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;final : 24 12  8  6 &amp;lt;--left[0]*right[0], left[1]*right[1]... etc.&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
The program is equally simple:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; product(&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;* vec, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; size) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;* left = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;[size]; &lt;span class=&quot;rem&quot;&gt;//left products&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;* right = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;[size]; &lt;span class=&quot;rem&quot;&gt;//right products&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; product = 1;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    left[0] = 1;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    right[size - 1] = 1;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 1; i &amp;lt; size; i++) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;        left[i] = product * vec[i - 1];&lt;span class=&quot;rem&quot;&gt;//keep the product of prev numbers&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;        product = left[i];&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;    product = 1;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = size - 2; i &amp;gt;= 0; i--) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;        right[i] = product * vec[i + 1];&lt;span class=&quot;rem&quot;&gt;//keep the product of next numbers&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;        product = right[i];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; size; i++)&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;        vec[i] = left[i] * right[i]; &lt;span class=&quot;rem&quot;&gt;// compute the final results&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;    delete[] left;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;    delete[] right;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  20:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;</description><link>http://worldofbrock.blogspot.com/2009/10/how-to-do-product-trick-on-array.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-295406754455583236</guid><pubDate>Sun, 18 Oct 2009 17:09:00 +0000</pubDate><atom:updated>2009-10-18T20:13:03.517+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><category domain="http://www.blogger.com/atom/ns#">Google interview</category><title>How to generate permutations of an array or string</title><description>I&#39;ve been asked this on interviews a number of times, so I think it counts as &quot;common questions&quot;. The answer is simple. &lt;br /&gt;
&lt;br /&gt;
It can be done recursively in a logical way: having a function which takes an array, swap the first element with all the others, in turn, and at each turn, call the function again for the rest of the array (skipping the first position):&lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;rem&quot;&gt;//head is the original array, unchanged between functions calls&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;&lt;span class=&quot;rem&quot;&gt;//this is so that we can print the full array&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;&lt;span class=&quot;rem&quot;&gt;//first is the &quot;partial&quot; array, which is being permuted now&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; permutations(&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;* head, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; totalLen, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;* first, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; len) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (len == 1) {&lt;span class=&quot;rem&quot;&gt;//we reached the end of the array&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; totalLen; i++)&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;            cout &amp;lt;&amp;lt; head[i] &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot; &quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;        cout &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//first do all the permutations&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//without swaping the first element with all the others&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;    permutations(head, totalLen, first + 1, len - 1);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; temp = first[0];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 1; i &amp;lt; len; i++) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;        first[0] = first[i]; &lt;span class=&quot;rem&quot;&gt;//swap the first element&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;        first[i] = temp; &lt;span class=&quot;rem&quot;&gt;//with all the others, in turn&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;        permutations(head, totalLen, first + 1, len - 1);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;        first[i] = first[0]; &lt;span class=&quot;rem&quot;&gt;//each turn generate permutations for&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  20:  &lt;/span&gt;        first[0] = temp;&lt;span class=&quot;rem&quot;&gt;// the rest of the array, then swap back the first elem&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  21:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  22:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
The function is called like this:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; size = 4;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; vec[] = { 1, 2, 3, 4 };&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    permutations(vec, size, vec, size);&lt;/pre&gt;&lt;/div&gt;</description><link>http://worldofbrock.blogspot.com/2009/10/how-to-generate-permutations-of-array.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-7566143510511981134</guid><pubDate>Sat, 10 Oct 2009 15:28:00 +0000</pubDate><atom:updated>2009-10-13T21:56:07.689+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><title>How to implement Dijkstra&#39;s algorithm</title><description>&lt;a href=&quot;http://www.algorithmist.com/index.php/Dijkstra%27s_algorithm&quot;&gt;Dijkstra&#39;s algorithm&lt;/a&gt; is a graph search algorithm, often used in routing. For a given node in the graph, the algorithm finds the path with lowest cost between that node and every other node. It can also be used for finding costs of shortest paths from a single node to a single destination node by stopping the algorithm once the shortest path (lowest cost) to the destination node has been determined.&lt;br /&gt;
&lt;br /&gt;
The algorithm is described on-line fairly good, so I won&#39;t spend time describing it again here. The main ideas are that we need to keep a list with all the nodes that we visited and another list with all the nodes that we still haven&#39;t visited. At each iteration we take the unvisited node with the lowest distance from the source (calculated so far) and calculate the distance from the source to each of it&#39;s neighbors. We stop when there are no unvisited nodes.&lt;br /&gt;
&lt;br /&gt;
I&#39;m just going to print the algorithm since it does all the talking by itself. Some comments are available in the code which should explain everything.&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;#include &amp;lt;iostream&amp;gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;#include &amp;lt;vector&amp;gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;#include &amp;lt;&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;#include &amp;lt;set&amp;gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;#include &amp;lt;queue&amp;gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;namespace&lt;/span&gt; std;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; SIZE = 5; &lt;span class=&quot;rem&quot;&gt;//number of nodes&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; graph[SIZE][SIZE];&lt;span class=&quot;rem&quot;&gt;//the graph itself&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; d[SIZE]; &lt;span class=&quot;rem&quot;&gt;//distances from the source to each node&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; pi[SIZE];&lt;span class=&quot;rem&quot;&gt;//contains the predecessor node for each node&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt; s;&lt;span class=&quot;rem&quot;&gt;//list of nodes which were already visited&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt; vs;&lt;span class=&quot;rem&quot;&gt;//list of nodes which were not visited yet&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; sort(vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt;* vec) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//sorts the vector of nodes according to &lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//distances from the source to each node&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (unsigned &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; vec-&amp;gt;size(); i++)&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (unsigned &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; j = i; j &amp;lt; vec-&amp;gt;size(); j++)&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  20:  &lt;/span&gt;            &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (d[vec-&amp;gt;at(i)] &amp;gt; d[vec-&amp;gt;at(j)]) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  21:  &lt;/span&gt;                &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; temp = vec-&amp;gt;at(i);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  22:  &lt;/span&gt;                vec-&amp;gt;at(i) = vec-&amp;gt;at(j);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  23:  &lt;/span&gt;                vec-&amp;gt;at(j) = temp;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  24:  &lt;/span&gt;            }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  25:  &lt;/span&gt;}&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  26:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  27:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; relax(vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt;* vec, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; u) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  28:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//updates the distances from the source to each node&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  29:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//and the predecessor for each node&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  30:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (unsigned &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; vec-&amp;gt;size(); i++) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  31:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; vi = vec-&amp;gt;at(i);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  32:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (graph[u][vi] == SHRT_MAX)&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  33:  &lt;/span&gt;            &lt;span class=&quot;kwrd&quot;&gt;continue&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  34:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (d[vi] &amp;gt; d[u] + graph[u][vi]) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  35:  &lt;/span&gt;            d[vi] = d[u] + graph[u][vi];&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  36:  &lt;/span&gt;            pi[vi] = u;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  37:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  38:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  39:  &lt;/span&gt;}&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  40:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  41:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; printShortestPathTo(&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; v) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  42:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//this simply prints the vector of predecessors&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  43:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//for the requested node (v)&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  44:  &lt;/span&gt;    cout &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;Distance to &quot;&lt;/span&gt; &amp;lt;&amp;lt; v &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot; is &quot;&lt;/span&gt; &amp;lt;&amp;lt; d[v] &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  45:  &lt;/span&gt;    cout &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;Shortest path: &quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  46:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; x = pi[v];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  47:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;while&lt;/span&gt; (x != -1) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  48:  &lt;/span&gt;        cout &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;&amp;lt;-&quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  49:  &lt;/span&gt;        x = pi[x];&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  50:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  51:  &lt;/span&gt;    cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  52:  &lt;/span&gt;}&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  53:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  54:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; main() {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  55:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//initialize all the costs in the graph&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  56:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; SIZE; i++)&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  57:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; j = 0; j &amp;lt; SIZE; j++)&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  58:  &lt;/span&gt;            graph[i][j] = SHRT_MAX;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  59:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  60:  &lt;/span&gt;    graph[0][1] = 20;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  61:  &lt;/span&gt;    graph[0][4] = 40;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  62:  &lt;/span&gt;    graph[1][4] = 50;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  63:  &lt;/span&gt;    graph[2][3] = 30;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  64:  &lt;/span&gt;    graph[2][4] = 60;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  65:  &lt;/span&gt;    graph[4][2] = 20;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  66:  &lt;/span&gt;    graph[4][3] = 100;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  67:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  68:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; SIZE; i++) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  69:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; j = 0; j &amp;lt; SIZE; j++) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  70:  &lt;/span&gt;            cout &amp;lt;&amp;lt; graph[i][j] &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;\t&quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  71:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  72:  &lt;/span&gt;        cout &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  73:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  74:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; SIZE; i++) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  75:  &lt;/span&gt;        vs.push_back(i);&lt;span class=&quot;rem&quot;&gt;//initialize all the variables&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  76:  &lt;/span&gt;        d[i] = SHRT_MAX;&lt;span class=&quot;rem&quot;&gt;//all distances to infinite&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  77:  &lt;/span&gt;        pi[i] = -1;&lt;span class=&quot;rem&quot;&gt;//nodes have no predecesors &lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  78:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  79:  &lt;/span&gt;    d[0] = 0; &lt;span class=&quot;rem&quot;&gt;//the distance of the source is 0&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  80:  &lt;/span&gt;    sort(&amp;amp;vs); &lt;span class=&quot;rem&quot;&gt;//we sort the nodes according to the distance&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  81:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  82:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;while&lt;/span&gt; (vs.size() &amp;gt; 0) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  83:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; x = vs.front();&lt;span class=&quot;rem&quot;&gt;//take the node with the shortest distance&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  84:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (unsigned &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; vs.size() - 1; i++) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  85:  &lt;/span&gt;            vs.at(i) = vs.at(i + 1);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  86:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  87:  &lt;/span&gt;        vs.pop_back();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  88:  &lt;/span&gt;        s.push_back(x);&lt;span class=&quot;rem&quot;&gt;//mark it as visited&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  89:  &lt;/span&gt;        relax(&amp;amp;vs, x);&lt;span class=&quot;rem&quot;&gt;//update the distances&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  90:  &lt;/span&gt;        sort(&amp;amp;vs); &lt;span class=&quot;rem&quot;&gt;//sort the nodes according to the new distances&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  91:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  92:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  93:  &lt;/span&gt;    printShortestPathTo(4);&lt;span class=&quot;rem&quot;&gt;//choose any node&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  94:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; 0;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  95:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;</description><link>http://worldofbrock.blogspot.com/2009/10/how-to-implement-dijkstras-algorithm.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>7</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-6973903008399337819</guid><pubDate>Sat, 03 Oct 2009 16:02:00 +0000</pubDate><atom:updated>2009-10-04T11:04:00.510+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><category domain="http://www.blogger.com/atom/ns#">Google interview</category><title>How to find the longest continuous increasing sequence</title><description>How to find &lt;a href=&quot;http://en.wikipedia.org/wiki/Longest_increasing_subsequence&quot;&gt;the longest continuous increasing sequence&lt;/a&gt; in a array of numbers is one of the questions I was asked in a interview at Microsoft, Copenhagen. The solution I provided then was not the best one, since I wasn&#39;t so well prepared (I gave a O(n*log&lt;sub&gt;2&lt;/sub&gt;n) solution which is better than the basic O(n&lt;sup&gt;2&lt;/sup&gt;) solution, but still not the optimum one). This can be done with O(n) time complexity. Hopefully writing these things here will help me in my future interviews.&lt;br /&gt;
&lt;br /&gt;
The idea for the O(n) solution is to keep track of the start of the longest continuous increasing sequence and it&#39;s length. At each item, if it&#39;s bigger than the previous, then we have a sequence. If it&#39;s longer then the current maximum length, then we have a new longest increasing sequence, longer than the previous one.&lt;br /&gt;
&lt;br /&gt;
The same principle can be applied to similar problem which I&#39;ll post later. Until then, here&#39;s the algorithm:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; longestContinuousIncrSeq(&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;* a, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; size) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; maxstart = 0;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; max = 1;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; start = 0;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 1; i &amp;lt; size; i++) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (a[i] &amp;gt; a[i - 1]) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;            &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (i - start + 1 &amp;gt; max) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;                max = i - start + 1;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;                maxstart = start;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;            }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;        } &lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt; {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;            start = i;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;    cout &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;Longest sequence starts at &quot;&lt;/span&gt; &amp;lt;&amp;lt; maxstart &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot; and is &quot;&lt;/span&gt; &amp;lt;&amp;lt; max &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot; numbers long.&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; max; i++) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;        cout &amp;lt;&amp;lt; a[maxstart + i] &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot; &quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;    cout &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  20:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;</description><link>http://worldofbrock.blogspot.com/2009/10/how-to-find-longest-continuous.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>36</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-3859340691265523549</guid><pubDate>Fri, 02 Oct 2009 15:47:00 +0000</pubDate><atom:updated>2009-10-02T18:47:36.958+03:00</atom:updated><title>How to reverse the order of words in a sentence but also each word</title><description>This is a common trick which can be done using pointers in C/C++, but the principle is the same for other languages: first reverse the whole sentence, disregarding the words, then reverse each word by itself (words are separated by whitespace).&lt;br /&gt;
&lt;br /&gt;
The method to reverse a string is easy:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; reverseWord(&lt;span class=&quot;kwrd&quot;&gt;char&lt;/span&gt;*&amp;amp; word, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; len) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; len / 2; i++) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;char&lt;/span&gt; temp = word[i];&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;char&lt;/span&gt; temp2 = word[len - i - 1];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;        *(word +i) = temp2;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;        word[len - i - 1] = temp;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
So we could use the above function to reverse the order of words in a sentence and also the letters in each word:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; s = &lt;span class=&quot;str&quot;&gt;&quot;This is a test run&quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;char&lt;/span&gt;* str = const_cast&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;char&lt;/span&gt;*&amp;gt;(s.c_str());&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; size = s.size();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    cout &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;Initial string is: &quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; size; i++)&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;        cout &amp;lt;&amp;lt; str[i];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    cout &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;    reverseWord(str, size);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;    cout &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;Whole string reversed is: &quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; size; i++)&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;        cout &amp;lt;&amp;lt; str[i];&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;    cout &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;// do stuff here&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; k = 0;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; start = 0;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;char&lt;/span&gt;* tmp = str;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;while&lt;/span&gt; (k &amp;lt;= size) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (str[k] == &lt;span class=&quot;str&quot;&gt;&#39; &#39;&lt;/span&gt; || str[k] == &lt;span class=&quot;str&quot;&gt;&#39;\0&#39;&lt;/span&gt;) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;            reverseWord(tmp, k - start);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  20:  &lt;/span&gt;            tmp = str + k + 1;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  21:  &lt;/span&gt;            start = k + 1;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  22:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  23:  &lt;/span&gt;        ++k;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  24:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  25:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  26:  &lt;/span&gt;    cout &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;After processing, string is: &quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  27:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; size; i++)&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  28:  &lt;/span&gt;        cout &amp;lt;&amp;lt; str[i];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  29:  &lt;/span&gt;    cout &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;
The complexity is... O(n) ?</description><link>http://worldofbrock.blogspot.com/2009/10/how-to-reverse-order-of-words-in.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-1805783438013483035</guid><pubDate>Sun, 27 Sep 2009 10:59:00 +0000</pubDate><atom:updated>2009-09-27T13:59:55.960+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><category domain="http://www.blogger.com/atom/ns#">sort</category><title>How to do merge sort</title><description>&lt;a href=&quot;http://en.wikipedia.org/wiki/Merge_sort&quot;&gt;Merge sort&lt;/a&gt; is a O(n*log&lt;sub&gt;2&lt;/sub&gt;n) complexity algorithm which is why it&#39;s preferred in various interviews. It is also a divide and conquer algorithm and the best way to sort linked lists, &lt;a href=&quot;http://worldofbrock.blogspot.com/2009/09/test1.html&quot;&gt;which I discussed in my previous post&lt;/a&gt;. The Java implementation of sorting collections (linked lists)&lt;a href=&quot;http://java.sun.com/docs/books/tutorial/collections/algorithms/&quot;&gt; is done using merge sort&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
So here is how it&#39;s done. It&#39;s quite a lot of code, but it&#39;s easy to understand:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; mergeSort(&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; arr[], &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; first, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; last) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; SIZE = last - first;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (SIZE &amp;lt;= 1)&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//if only 2 elements exist, just swap if necessary&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (SIZE == 2) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (arr[first] &amp;gt; arr[last - 1]) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;            &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; tmp = arr[first];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;            arr[first] = arr[last - 1];&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;            arr[last - 1] = tmp;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//otherwise find the middle &lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//and apply merge sort for each half&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; half = first + SIZE / 2;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;    mergeSort(arr, first, half);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;    mergeSort(arr, half, last);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  20:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  21:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = first;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  22:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; j = half;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  23:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;* temp = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;[SIZE];&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  24:  &lt;/span&gt;    memset(temp, 0, &lt;span class=&quot;kwrd&quot;&gt;sizeof&lt;/span&gt;(&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;) * SIZE);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  25:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; k = 0;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  26:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//after both halfs are sorted&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  27:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//merge them together in one big sorted list&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  28:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;while&lt;/span&gt; (i &amp;lt; half || j &amp;lt; last) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  29:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (i &amp;lt; half &amp;amp;&amp;amp; j &amp;lt; last) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  30:  &lt;/span&gt;            &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (arr[i] &amp;lt; arr[j]) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  31:  &lt;/span&gt;                temp[k] = arr[i];&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  32:  &lt;/span&gt;                i++;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  33:  &lt;/span&gt;            } &lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt; {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  34:  &lt;/span&gt;                temp[k] = arr[j];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  35:  &lt;/span&gt;                j++;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  36:  &lt;/span&gt;            }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  37:  &lt;/span&gt;        } &lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (i &amp;lt; half) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  38:  &lt;/span&gt;            temp[k] = arr[i];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  39:  &lt;/span&gt;            i++;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  40:  &lt;/span&gt;        } &lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt; {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  41:  &lt;/span&gt;            temp[k] = arr[j];&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  42:  &lt;/span&gt;            j++;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  43:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  44:  &lt;/span&gt;        ++k;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  45:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  46:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//overrite the original array with the sorted elements&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  47:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; m = 0; m &amp;lt; SIZE; m++)&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  48:  &lt;/span&gt;        arr[first + m] = temp[m];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  49:  &lt;/span&gt;    delete[] temp;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  50:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;</description><link>http://worldofbrock.blogspot.com/2009/09/how-to-do-merge-sort.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-1992656462215149921</guid><pubDate>Sat, 26 Sep 2009 11:42:00 +0000</pubDate><atom:updated>2009-09-26T14:43:01.385+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><category domain="http://www.blogger.com/atom/ns#">pattern matching</category><title>How to do string matching</title><description>There are numerous algorithms for finding matches between strings and patterns, &lt;a href=&quot;http://en.wikipedia.org/wiki/String_searching_algorithm&quot;&gt;see wikipedia&lt;/a&gt; for detailed explanations. In the example implementation below, I chose a simpler variation of &lt;a href=&quot;http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm&quot;&gt;the Boyer-Moore algorithm&lt;/a&gt;, just because it&#39;s fast to implement and understand, and good enough in practice. The algorithm is called &lt;a href=&quot;http://en.wikipedia.org/wiki/Boyer-Moore-Horspool_algorithm&quot;&gt;Boyer–Moore–Horspool algorithm or Horspool&#39;s algorithm&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
It uses only the first table from Boyer-Moore, which is why it&#39;s easy to implement. &lt;br /&gt;
First, here&#39;s the function which build the table:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; SIZE = 255;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; computeTable(&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; arr[], &lt;span class=&quot;kwrd&quot;&gt;char&lt;/span&gt;* pattern, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; len) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; SIZE; i++) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;        arr[i] = len;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; len - 1; i++) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;        arr[pattern[i]] = len - i - 1;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
And the actual string matching uses the table which we build above:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; findOccurences(&lt;span class=&quot;kwrd&quot;&gt;char&lt;/span&gt;* pattern, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; patternLen, &lt;span class=&quot;kwrd&quot;&gt;char&lt;/span&gt;* str, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; strLen, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;* table) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; pos = 0;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; last = patternLen - 1;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;while&lt;/span&gt; (patternLen &amp;lt;= strLen) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (i = last; str[i] == pattern[i] &amp;amp;&amp;amp; i &amp;gt;= 0; i--);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (i == -1)&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;            &lt;span class=&quot;rem&quot;&gt;//return pos;&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;            cout &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;found one at position: &quot;&lt;/span&gt; &amp;lt;&amp;lt; pos &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (strLen &amp;lt;= table[str[last]])&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;            &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; -1;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; offset = table[str[last]];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;        str += offset;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;        strLen -= offset;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;        pos += offset;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; -1;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
You can use it simply by typing this:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; main() {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;char&lt;/span&gt; pattern[] = &lt;span class=&quot;str&quot;&gt;&quot;gcagagag&quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;char&lt;/span&gt; str[] = &lt;span class=&quot;str&quot;&gt;&quot;gcatcgcagagagtatacagtacg&quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; patternSize = &lt;span class=&quot;kwrd&quot;&gt;sizeof&lt;/span&gt;(pattern) - 1;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; strSize = &lt;span class=&quot;kwrd&quot;&gt;sizeof&lt;/span&gt;(str) - 1;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; arr[SIZE];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    computeTable(arr, pattern, patternSize);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;    cout &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;Finding occurences... &quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;    cout &amp;lt;&amp;lt; findOccurences(pattern, patternSize, str, strSize, arr) &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; 0;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;</description><link>http://worldofbrock.blogspot.com/2009/09/how-to-do-string-matching.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-4562418118153721257</guid><pubDate>Sat, 26 Sep 2009 08:34:00 +0000</pubDate><atom:updated>2009-09-26T14:44:46.923+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><category domain="http://www.blogger.com/atom/ns#">heap</category><category domain="http://www.blogger.com/atom/ns#">sort</category><title>How to sort an array using heap sort</title><description>Heap-sort means using the heap structure and heap operations, as defined in &lt;a href=&quot;http://worldofbrock.blogspot.com/2009/09/how-to-implement-heap.html&quot;&gt;my previous post&lt;/a&gt;, to sort a container such as an array. The complexity for heap sort is O(n * log&lt;sub&gt;2&lt;/sub&gt;n), explained next. &lt;br /&gt;
&lt;br /&gt;
Take each element from the unsorted array and put it into a heap, restoring the heap property each time. Restoring the heap property is O(log&lt;sub&gt;2&lt;/sub&gt; n) complexity and you have to multiply that for each element of the array, meaning O(n*log&lt;sub&gt;2&lt;/sub&gt;n). After you finish, remove one element at a time from the heap (the root each time, because it&#39;s the biggest element), restoring the heap property after each removal. Again, this is O(n*log&lt;sub&gt;2&lt;/sub&gt;n). &lt;br /&gt;
&lt;br /&gt;
The full algorithm also requires O(n) additional space.&lt;br /&gt;
Here it is:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;template &amp;lt;&lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; T&amp;gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; sortArray(vector&amp;lt;T&amp;gt;* vec) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt;* heap = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; vector&amp;lt;T&amp;gt;();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (unsigned &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; vec-&amp;gt;size(); i++) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;        addToHeap(heap, vec-&amp;gt;at(i));&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    vec-&amp;gt;clear();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;    T elem = deleteFromHeap(heap);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;while&lt;/span&gt; (elem != -1) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;        vec-&amp;gt;push_back(elem);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;        elem = deleteFromHeap(heap);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;    delete heap;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;</description><link>http://worldofbrock.blogspot.com/2009/09/how-to-aort-array-using-heap-sort.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-8420622326776609205</guid><pubDate>Tue, 22 Sep 2009 14:46:00 +0000</pubDate><atom:updated>2009-09-26T11:37:35.534+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">heap</category><title>How to implement a heap</title><description>Doing basic operations on&lt;a href=&quot;http://en.wikipedia.org/wiki/Heap_%28data_structure%29&quot;&gt; a heap &lt;/a&gt;is a more complicated than implementing&lt;a href=&quot;http://en.wikipedia.org/wiki/Binary_tree&quot;&gt; a binary tree&lt;/a&gt;, but is a good thing to know just in case. The easiest and most efficient way to implement a heap is using an array. &lt;br /&gt;
&lt;br /&gt;
Inserting to a heap means adding a new element to the last position, than restoring the heap property, by moving up the tree. The child of a node k in the array is at positions 2k+1 and 2k+2, so moving up the tree is just basic arithmetic.&lt;br /&gt;
&lt;br /&gt;
Deleting from the heap simply means replacing the root (the element at position k 0) with the last element, delete the last element, and restore the heap property.&lt;br /&gt;
Without further delays, here&#39;s the code.&lt;br /&gt;
&lt;br /&gt;
For insertion:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;template &amp;lt;&lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; T&amp;gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; addToHeap(vector&amp;lt;T&amp;gt;* heap, T val) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    heap-&amp;gt;push_back(val);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; pos = heap-&amp;gt;size() - 1;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (pos == 0)&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; fixingHeap = &lt;span class=&quot;kwrd&quot;&gt;true&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;do&lt;/span&gt; {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; parent = 0;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (pos % 2 == 0)&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;            parent = (pos - 2) / 2;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;            parent = (pos - 1) / 2;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (heap-&amp;gt;at(parent) &amp;lt; heap-&amp;gt;at(pos)) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;            T temp = heap-&amp;gt;at(pos);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;            heap-&amp;gt;at(pos) = heap-&amp;gt;at(parent);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;            heap-&amp;gt;at(parent) = temp;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;            pos = parent;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;        } &lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt; {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  20:  &lt;/span&gt;            fixingHeap = &lt;span class=&quot;kwrd&quot;&gt;false&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  21:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  22:  &lt;/span&gt;    } &lt;span class=&quot;kwrd&quot;&gt;while&lt;/span&gt; (fixingHeap &amp;amp;&amp;amp; pos != 0);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  23:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
For deleting:&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;template &amp;lt;&lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; T&amp;gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;T deleteFromHeap(vector&amp;lt;T&amp;gt;* heap) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; size = heap-&amp;gt;size();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (size == 0) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; -1;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    T ret = heap-&amp;gt;front();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (size == 1) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;        heap-&amp;gt;clear();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; ret;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (size == 2) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;        heap-&amp;gt;at(0) = heap-&amp;gt;at(size - 1);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;        heap-&amp;gt;pop_back();&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; ret;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;    heap-&amp;gt;at(0) = heap-&amp;gt;at(size - 1);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;    heap-&amp;gt;pop_back();&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;    size--;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  20:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; fixingHeap = &lt;span class=&quot;kwrd&quot;&gt;true&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  21:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; pos = 0;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  22:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;do&lt;/span&gt; {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  23:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; child1 = 2 * pos + 1;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  24:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; child2 = 2 * pos + 2;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  25:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; switchPosition = 0;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  26:  &lt;/span&gt;        T max = 0;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  27:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (child2 &amp;lt; size) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  28:  &lt;/span&gt;            &lt;span class=&quot;rem&quot;&gt;//2 children&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  29:  &lt;/span&gt;            &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (heap-&amp;gt;at(child1) &amp;gt; heap-&amp;gt;at(child2)) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  30:  &lt;/span&gt;                switchPosition = child1;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  31:  &lt;/span&gt;                max = heap-&amp;gt;at(child1);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  32:  &lt;/span&gt;            }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  33:  &lt;/span&gt;            &lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt; {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  34:  &lt;/span&gt;                switchPosition = child2;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  35:  &lt;/span&gt;                max = heap-&amp;gt;at(child2);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  36:  &lt;/span&gt;            }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  37:  &lt;/span&gt;            &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (heap-&amp;gt;at(pos) &amp;lt; max) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  38:  &lt;/span&gt;                T temp = heap-&amp;gt;at(pos);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  39:  &lt;/span&gt;                heap-&amp;gt;at(pos) = heap-&amp;gt;at(switchPosition);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  40:  &lt;/span&gt;                heap-&amp;gt;at(switchPosition) = temp;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  41:  &lt;/span&gt;                pos = switchPosition;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  42:  &lt;/span&gt;            } &lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt; {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  43:  &lt;/span&gt;                fixingHeap = &lt;span class=&quot;kwrd&quot;&gt;false&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  44:  &lt;/span&gt;            }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  45:  &lt;/span&gt;        } &lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (child1 &amp;lt; size) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  46:  &lt;/span&gt;            &lt;span class=&quot;rem&quot;&gt;//1 child&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  47:  &lt;/span&gt;            &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (heap-&amp;gt;at(pos) &amp;lt; heap-&amp;gt;at(child1)) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  48:  &lt;/span&gt;                T temp = heap-&amp;gt;at(pos);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  49:  &lt;/span&gt;                heap-&amp;gt;at(pos) = heap-&amp;gt;at(child1);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  50:  &lt;/span&gt;                heap-&amp;gt;at(child1) = temp;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  51:  &lt;/span&gt;                pos = child1;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  52:  &lt;/span&gt;            } &lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt; {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  53:  &lt;/span&gt;                fixingHeap = &lt;span class=&quot;kwrd&quot;&gt;false&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  54:  &lt;/span&gt;            }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  55:  &lt;/span&gt;        } &lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt; {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  56:  &lt;/span&gt;            &lt;span class=&quot;rem&quot;&gt;//0 children;&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  57:  &lt;/span&gt;            fixingHeap = &lt;span class=&quot;kwrd&quot;&gt;false&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  58:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  59:  &lt;/span&gt;    } &lt;span class=&quot;kwrd&quot;&gt;while&lt;/span&gt; (fixingHeap);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  60:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; ret;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  61:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;</description><link>http://worldofbrock.blogspot.com/2009/09/how-to-implement-heap.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-2593458725655323578</guid><pubDate>Mon, 07 Sep 2009 17:48:00 +0000</pubDate><atom:updated>2009-09-07T20:52:53.667+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><title>How to find the sub-array with the maximum sum</title><description>Suppose you have an array of integers, and you want to find the sub-array with the maximum sum. One obvious solution would be to take each index, go through the rest of the numbers, find the local sub-array with the maximum sum and compare with the global found. But that would mean O(n&lt;sup&gt;2&lt;/sup&gt;), which although polynomial, is time expensive.&lt;br /&gt;
&lt;br /&gt;
There is a O(n) solution: go through each index of the array, remembering what the max sum is until now, from where it starts and where it ends. At each step, remember a local sub-array and check if it&#39;s larger than the maximum. If it is, the maximum becomes the current sub-array. If the local sum ever becomes &lt;0, just restart the sub-array with the next index.
Here&#39;s the algorithm:
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; arr[10] = {3, 1, 6, -1, -7, 3, 10, -5, 0, 1};&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; maxsum = arr[0];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; maxstart = 0;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; maxend = 0;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; sum = arr[0];&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; start = 0;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 1; i &amp;lt; 10; i++) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;        sum += arr[i];&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (sum &amp;gt; maxsum) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;            maxsum = sum;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;            maxend = i;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;            maxstart = start;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (sum &amp;lt; 0) {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;            sum = 0;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;            start = i + 1;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;    cout &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;maxsum=&quot;&lt;/span&gt; &amp;lt;&amp;lt; maxsum &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  20:  &lt;/span&gt;    cout &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;sequence: &quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  21:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i =  maxstart; i &amp;lt;= maxend; i++) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  22:  &lt;/span&gt;        cout &amp;lt;&amp;lt; arr[i] &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot; &quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  23:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  24:  &lt;/span&gt;    cout &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
Here&#39;s what this prints:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;      maxsum=15&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;      sequence: 3 1 6 -1 -7 3 10&lt;/pre&gt;&lt;/div&gt;</description><link>http://worldofbrock.blogspot.com/2009/09/how-to-find-sub-array-with-maximum-sum.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-1832050266960336850</guid><pubDate>Sun, 06 Sep 2009 15:30:00 +0000</pubDate><atom:updated>2009-09-26T11:38:50.994+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">bit operations</category><title>How to convert a number from little endian to big endian</title><description>Converting from&lt;a href=&quot;http://en.wikipedia.org/wiki/Endianness&quot;&gt; little endian&lt;/a&gt; to &lt;a href=&quot;http://en.wikipedia.org/wiki/Endianness&quot;&gt;big endian&lt;/a&gt; means actually reversing the bits in a number, like in a mirror. This can be done iteratively, by removing one bit at a time and building another number with the bits reversed. I don&#39;t know any other version, but I don&#39;t think there&#39;s a better solution.&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;//little endian to big endian&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;    unsigned &lt;span class=&quot;kwrd&quot;&gt;char&lt;/span&gt; littleEndianN = 5;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    cout &amp;lt;&amp;lt; (unsigned &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;)littleEndianN &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;(size=&quot;&lt;/span&gt; &amp;lt;&amp;lt; &lt;span class=&quot;kwrd&quot;&gt;sizeof&lt;/span&gt;(littleEndianN) * 8 &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;) little endian is &quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    unsigned &lt;span class=&quot;kwrd&quot;&gt;char&lt;/span&gt; bigEndianN = 0;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; &lt;span class=&quot;kwrd&quot;&gt;sizeof&lt;/span&gt;(littleEndianN) * 8; i++) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;        cout &amp;lt;&amp;lt; (littleEndianN &amp;amp; 1);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;        bigEndianN |= (littleEndianN &amp;amp; 1);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;        littleEndianN &amp;gt;&amp;gt;= 1;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (i &amp;lt; (&lt;span class=&quot;kwrd&quot;&gt;sizeof&lt;/span&gt;(littleEndianN) * 8 - 1))&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;            bigEndianN &amp;lt;&amp;lt;= 1;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;    cout &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot; meaning &quot;&lt;/span&gt; &amp;lt;&amp;lt; (unsigned &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;)bigEndianN &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot; in big endian&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
Here&#39;s what this prints:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;5(size=8) little endian &lt;span class=&quot;kwrd&quot;&gt;is&lt;/span&gt; 10100000 meaning 160 &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; big endian&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
What happens if the number is negative? Well, if we change all the unsigned char&#39;s to char&#39;s, the program prints this:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;-5(size=8) little endian &lt;span class=&quot;kwrd&quot;&gt;is&lt;/span&gt; 11011111 meaning -33 &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; big endian&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
Do you know why?&lt;br /&gt;
Also since we&#39;re on the subject, how do you tell if the computer representation is little endian or big endian? Spoiler below :)&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; isLittleEndian = ((1 &amp;lt;&amp;lt; 1) == 2);&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;</description><link>http://worldofbrock.blogspot.com/2009/09/how-to-convert-number-from-little.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8584007095819742992.post-2030346456888616909</guid><pubDate>Sun, 06 Sep 2009 08:33:00 +0000</pubDate><atom:updated>2009-09-06T20:46:47.701+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">bit operations</category><category domain="http://www.blogger.com/atom/ns#">Google interview</category><title>How to check if a number is a power of 2 (Google phone screening)</title><description>This is actually one of the questions I was asked in my Google first phone screening, and it&#39;s really easy, because there&#39;s a trick for that: given a number N, you can check if it&#39;s a power of two if N &amp; (N-1) == 0.&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; n = 1024;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;cout &amp;lt;&amp;lt; n &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot; is &quot;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    &amp;lt;&amp;lt; ((n &amp;amp; (n-1)) ? &lt;span class=&quot;str&quot;&gt;&quot;NOT &quot;&lt;/span&gt; : &lt;span class=&quot;str&quot;&gt;&quot;&quot;&lt;/span&gt; )&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;power of 2&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;/div&gt;You could also do the iterative version, where you repeatedly check the first byte (N &amp; 1), then shift to the right (N &gt;&gt; 1), and again, for all 32 bits (assuming it&#39;s a 32bit integer). If the total number of bits with value 1 is just one, it means it&#39;s a power of two. I gave this as an alternative to the trick above and everything was fine.</description><link>http://worldofbrock.blogspot.com/2009/09/how-to-check-if-number-is-power-of-2.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item></channel></rss>