<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>My Tech Interviews</title>
	
	<link>http://www.mytechinterviews.com</link>
	<description>PREPARE FOR A TECHNICAL INTERVIEW</description>
	<lastBuildDate>Sun, 24 Apr 2011 04:16:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/MyTechInterviews" /><feedburner:info uri="mytechinterviews" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Challenge – 50 trucks with payload</title>
		<link>http://feedproxy.google.com/~r/MyTechInterviews/~3/fFZgRFF_Tng/challenge-50-trucks-with-payload</link>
		<comments>http://www.mytechinterviews.com/challenge-50-trucks-with-payload#comments</comments>
		<pubDate>Tue, 19 Apr 2011 03:18:57 +0000</pubDate>
		<dc:creator>Doggie</dc:creator>
				<category><![CDATA[Challenge]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Puzzles]]></category>
		<category><![CDATA[50]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[fuel]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[palantir]]></category>
		<category><![CDATA[trucks]]></category>

		<guid isPermaLink="false">http://www.mytechinterviews.com/?p=1145</guid>
		<description><![CDATA[Question: Given a fleet of 50 trucks, each with a full fuel tank and a range of 100 miles, how far can you deliver a payload? You can transfer the payload from truck to truck, and you can transfer fuel from truck to truck. Assume all the payload will fit in one truck. Challenge: Do [...]


Related posts:<ol><li><a href='http://www.mytechinterviews.com/challenge-camel-and-bananas' rel='bookmark' title='Permanent Link: Challenge &#8211; Camel and Bananas'>Challenge &#8211; Camel and Bananas</a></li>
<li><a href='http://www.mytechinterviews.com/globe-walker' rel='bookmark' title='Permanent Link: Globe Walker'>Globe Walker</a></li>
<li><a href='http://www.mytechinterviews.com/4-quarts-of-water' rel='bookmark' title='Permanent Link: 4 Quarts of Water'>4 Quarts of Water</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fchallenge-50-trucks-with-payload"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fchallenge-50-trucks-with-payload&amp;source=mytechinterview&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><strong>Question:</strong> Given a fleet of 50 trucks, each with a full fuel tank and a range of 100 miles, how far can you deliver a payload? You can transfer the payload from truck to truck, and you can transfer fuel from truck to truck. Assume all the payload will fit in one truck.</p>
<p><strong>Challenge:</strong> Do you know the answer to this question? Post in the comments. Answers will be posted on <strong> April 22th</strong>.</p>
<p>Thanks Ole Sandbu, Czikus and Vanta for the answers.</p>
<p>We want to use as little fuel as possible so we try minimize the number of trucks we use as we go along. Let&#8217;s say we start with all 50 trucks with full fuel (5000 miles range). For each mile, we lose 50 miles in range. After two miles, we lose 100 miles leaving us with 4900 miles. This can be supported by 49 trucks so we drop one truck.<br />
As you can see for every 100 miles we lose in range, we drop a truck.</p>
<p>50 trucks: 100/50<br />
49 trucks: 100/49<br />
&#8230;</p>
<p>Total distance = 100/50 + 100/49 + 100/48 + &#8230; + 100/2 + 100/1 (harmonic series) = 449.920533833</p>
<p>Meanwhile check out the challenges from previous weeks <strong><a href="http://www.mytechinterviews.com/category/challenge">here</a></strong>.</p>


<p>Related posts:<ol><li><a href='http://www.mytechinterviews.com/challenge-camel-and-bananas' rel='bookmark' title='Permanent Link: Challenge &#8211; Camel and Bananas'>Challenge &#8211; Camel and Bananas</a></li>
<li><a href='http://www.mytechinterviews.com/globe-walker' rel='bookmark' title='Permanent Link: Globe Walker'>Globe Walker</a></li>
<li><a href='http://www.mytechinterviews.com/4-quarts-of-water' rel='bookmark' title='Permanent Link: 4 Quarts of Water'>4 Quarts of Water</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=fFZgRFF_Tng:VN0xTWU0SKw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=fFZgRFF_Tng:VN0xTWU0SKw:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=fFZgRFF_Tng:VN0xTWU0SKw:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=fFZgRFF_Tng:VN0xTWU0SKw:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=fFZgRFF_Tng:VN0xTWU0SKw:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=fFZgRFF_Tng:VN0xTWU0SKw:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=fFZgRFF_Tng:VN0xTWU0SKw:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=fFZgRFF_Tng:VN0xTWU0SKw:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/MyTechInterviews/~4/fFZgRFF_Tng" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.mytechinterviews.com/challenge-50-trucks-with-payload/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://www.mytechinterviews.com/challenge-50-trucks-with-payload</feedburner:origLink></item>
		<item>
		<title>Implement strstr</title>
		<link>http://feedproxy.google.com/~r/MyTechInterviews/~3/j9yAwfY-BfI/implement-strstr</link>
		<comments>http://www.mytechinterviews.com/implement-strstr#comments</comments>
		<pubDate>Fri, 15 Apr 2011 19:57:51 +0000</pubDate>
		<dc:creator>Doggie</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[String Manipulation]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[strings]]></category>

		<guid isPermaLink="false">http://www.mytechinterviews.com/?p=1133</guid>
		<description><![CDATA[Question: Implement strstr in Java. Find the first instance of a string in another string. Answer: public int Search(String haystack, String needle){ for(int i = 0; i < haystack.length(); i++ ) { for(int j = 0; j < needle.length() &#038;&#038; i+j < haystack.length(); j++ ) { if(needle.charAt(j) != haystack.charAt(i+j)) { break; } else if (j [...]


Related posts:<ol><li><a href='http://www.mytechinterviews.com/combinations-of-a-string' rel='bookmark' title='Permanent Link: Combinations of a String'>Combinations of a String</a></li>
<li><a href='http://www.mytechinterviews.com/first-non-repeated-character' rel='bookmark' title='Permanent Link: First Non-Repeated Character'>First Non-Repeated Character</a></li>
<li><a href='http://www.mytechinterviews.com/reverse-the-order-of-words-in-a-string' rel='bookmark' title='Permanent Link: Reverse the Order of Words in a String'>Reverse the Order of Words in a String</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fimplement-strstr"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fimplement-strstr&amp;source=mytechinterview&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><strong>Question:</strong> Implement strstr in Java. Find the first instance of a string in another string. </p>
<p><strong>Answer:</strong> </p>
<pre>
public int Search(String haystack, String needle){
    for(int i = 0; i < haystack.length(); i++ ) {
        for(int j = 0; j < needle.length() &#038;&#038;
                        i+j < haystack.length(); j++ ) {
            if(needle.charAt(j) != haystack.charAt(i+j)) {
                break;
            } else if (j == needle.length()-1) {
                return i;
            }
        }
    }
    return -1;
}
</pre>
<p>Like this question? Follow <a href='http://www.mytechinterviews.com/feed'>our feed</a> and tweet it.</p>


<p>Related posts:<ol><li><a href='http://www.mytechinterviews.com/combinations-of-a-string' rel='bookmark' title='Permanent Link: Combinations of a String'>Combinations of a String</a></li>
<li><a href='http://www.mytechinterviews.com/first-non-repeated-character' rel='bookmark' title='Permanent Link: First Non-Repeated Character'>First Non-Repeated Character</a></li>
<li><a href='http://www.mytechinterviews.com/reverse-the-order-of-words-in-a-string' rel='bookmark' title='Permanent Link: Reverse the Order of Words in a String'>Reverse the Order of Words in a String</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=j9yAwfY-BfI:k29vczsVIzA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=j9yAwfY-BfI:k29vczsVIzA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=j9yAwfY-BfI:k29vczsVIzA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=j9yAwfY-BfI:k29vczsVIzA:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=j9yAwfY-BfI:k29vczsVIzA:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=j9yAwfY-BfI:k29vczsVIzA:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=j9yAwfY-BfI:k29vczsVIzA:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=j9yAwfY-BfI:k29vczsVIzA:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/MyTechInterviews/~4/j9yAwfY-BfI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.mytechinterviews.com/implement-strstr/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.mytechinterviews.com/implement-strstr</feedburner:origLink></item>
		<item>
		<title>Search in a sorted rotated array</title>
		<link>http://feedproxy.google.com/~r/MyTechInterviews/~3/iPp_L1awoLM/search-in-a-sorted-circular-array</link>
		<comments>http://www.mytechinterviews.com/search-in-a-sorted-circular-array#comments</comments>
		<pubDate>Thu, 07 Apr 2011 16:51:30 +0000</pubDate>
		<dc:creator>Doggie</dc:creator>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[circular]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[rotated]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[tricky]]></category>

		<guid isPermaLink="false">http://www.mytechinterviews.com/?p=1119</guid>
		<description><![CDATA[Question: Implement a search function for a sorted rotated array. Duplicates are allowed. Returning any one of the duplicates is acceptable. Answer: We can do a binary search with some modified checks. public int rotatedSearch(int[] values, int start, int end, int x){ if(values[start] == x){ return start; } else if(values[end] == x){ return end; } [...]


Related posts:<ol><li><a href='http://www.mytechinterviews.com/sub-array-with-the-largest-sum' rel='bookmark' title='Permanent Link: Sub-Array with the Largest Sum'>Sub-Array with the Largest Sum</a></li>
<li><a href='http://www.mytechinterviews.com/anagrams-in-array-of-strings' rel='bookmark' title='Permanent Link: Anagrams in an Array of Strings'>Anagrams in an Array of Strings</a></li>
<li><a href='http://www.mytechinterviews.com/permutations-of-a-string' rel='bookmark' title='Permanent Link: Permutations of a String'>Permutations of a String</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fsearch-in-a-sorted-circular-array"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fsearch-in-a-sorted-circular-array&amp;source=mytechinterview&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><strong>Question:</strong> Implement a search function for a sorted rotated array. Duplicates are allowed. Returning any one of the duplicates is acceptable.</p>
<p><strong>Answer:</strong> We can do a binary search with some modified checks.</p>
<pre>
public int rotatedSearch(int[] values, int start, int end,
                          int x){
    if(values[start] == x){
        return start;
    } else if(values[end] == x){
        return end;
    } else if(end - start == 1) {
        return -1;
    }
    int middle = (start + end) / 2;

    if(values[start] <= values[middle]){
        if(x <= values[middle] &#038;&#038; x >= values[start]){
            return rotatedSearch(values, start, middle, x);
        } else {
            return rotatedSearch(values, middle, end, x);
        }
    } else if(values[middle] <= values[end]){
        if(x >= values[middle] &#038;&#038; x <= values[end] ){
            return rotatedSearch(values, middle, end, x);
        } else {
            return rotatedSearch(values, start, middle, x);
        }
    } else {
        return -1;
    }
}
</pre>
<p>Like this question? Follow <a href='http://www.mytechinterviews.com/feed'>our feed</a> and tweet it.</p>


<p>Related posts:<ol><li><a href='http://www.mytechinterviews.com/sub-array-with-the-largest-sum' rel='bookmark' title='Permanent Link: Sub-Array with the Largest Sum'>Sub-Array with the Largest Sum</a></li>
<li><a href='http://www.mytechinterviews.com/anagrams-in-array-of-strings' rel='bookmark' title='Permanent Link: Anagrams in an Array of Strings'>Anagrams in an Array of Strings</a></li>
<li><a href='http://www.mytechinterviews.com/permutations-of-a-string' rel='bookmark' title='Permanent Link: Permutations of a String'>Permutations of a String</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=iPp_L1awoLM:tXtci45ou_o:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=iPp_L1awoLM:tXtci45ou_o:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=iPp_L1awoLM:tXtci45ou_o:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=iPp_L1awoLM:tXtci45ou_o:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=iPp_L1awoLM:tXtci45ou_o:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=iPp_L1awoLM:tXtci45ou_o:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=iPp_L1awoLM:tXtci45ou_o:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=iPp_L1awoLM:tXtci45ou_o:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/MyTechInterviews/~4/iPp_L1awoLM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.mytechinterviews.com/search-in-a-sorted-circular-array/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.mytechinterviews.com/search-in-a-sorted-circular-array</feedburner:origLink></item>
		<item>
		<title>Challenge – Camel and Bananas</title>
		<link>http://feedproxy.google.com/~r/MyTechInterviews/~3/Qbs4QnFPYBI/challenge-camel-and-bananas</link>
		<comments>http://www.mytechinterviews.com/challenge-camel-and-bananas#comments</comments>
		<pubDate>Wed, 06 Apr 2011 04:15:42 +0000</pubDate>
		<dc:creator>Doggie</dc:creator>
				<category><![CDATA[Challenge]]></category>
		<category><![CDATA[Puzzles]]></category>
		<category><![CDATA[banana]]></category>
		<category><![CDATA[camel]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[puzzle]]></category>

		<guid isPermaLink="false">http://www.mytechinterviews.com/?p=1114</guid>
		<description><![CDATA[Question: The owner of a banana plantation has a camel. He wants to transport his 3000 bananas to the market, which is located after the desert. The distance between his banana plantation and the market is about 1000 kilometer. So he decided to take his camel to carry the bananas. The camel can carry at [...]


Related posts:<ol><li><a href='http://www.mytechinterviews.com/challenge-50-trucks-with-payload' rel='bookmark' title='Permanent Link: Challenge &#8211; 50 trucks with payload'>Challenge &#8211; 50 trucks with payload</a></li>
<li><a href='http://www.mytechinterviews.com/how-many-trailing-zeros-in-100-factorial' rel='bookmark' title='Permanent Link: Trailing Zeros in 100 Factorial'>Trailing Zeros in 100 Factorial</a></li>
<li><a href='http://www.mytechinterviews.com/chess-squares' rel='bookmark' title='Permanent Link: Chess Squares'>Chess Squares</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fchallenge-camel-and-bananas"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fchallenge-camel-and-bananas&amp;source=mytechinterview&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><strong>Question:</strong> The owner of a banana plantation has a camel. He wants to transport his 3000 bananas to the market, which is located after the desert. The distance between his banana plantation and the market is about 1000 kilometer. So he decided to take his camel to carry the bananas. The camel can carry at the maximum of 1000 bananas at a time, and it eats one banana for every kilometer it travels.</p>
<p>What is the largest number of bananas that can be delivered to the market?</p>
<p><strong>Challenge:</strong> Do you know the answer to this question? Post in the comments. Answers will be posted<strong> April 8th</strong>.</p>
<p>Thanks for all the responses. Quite a few of you have the right answer.</p>
<p>At KM#0, we have 3000 bananas. The maximum bananas the camel can carry is 1000 so the camel must at least make 3 trips from the start point. <b>(Leave #0, Return to #0, Leave #0, Return to #0, Leave #0)</b>.<br />
If we move just 1km, we need 1 banana for each step mentioned above thus making a total of <b>5 bananas for each km</b>. </p>
<p>We continue making 3 trips until we reach a banana count of 2000.<br />
3000 &#8211; 5*d = 2000 => d = 200<br />
At #200km, we will have 2000 bananas</p>
<p>At this point, we only need to make 2 trips <b>(Leave #200, Return to #200, Leave #200)</b>. This will cost 1 banana for each step thus making a total of <b>3 bananas for each km</b>.</p>
<p>We continue making 2 trips until we reach a banana count of 1000.<br />
2000 &#8211; 3*d = 1000 => d = 333km<br />
At#(200+333) = #534km, we will have 998 bananas</p>
<p>At this point, we need to make one trip so the camel just carries everything and marches toward the market.<br />
Remaining km = 1000 &#8211; 534 = 466km. Bananas needed = 466.</p>
<p>Therefore, the bananas remaining once the camel reaches the market is 998 &#8211; 466 = <b>532 bananas</b>. <img src='http://www.mytechinterviews.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Check out the challenges from previous weeks <strong><a href="http://www.mytechinterviews.com/category/challenge">here</a></strong>.</p>


<p>Related posts:<ol><li><a href='http://www.mytechinterviews.com/challenge-50-trucks-with-payload' rel='bookmark' title='Permanent Link: Challenge &#8211; 50 trucks with payload'>Challenge &#8211; 50 trucks with payload</a></li>
<li><a href='http://www.mytechinterviews.com/how-many-trailing-zeros-in-100-factorial' rel='bookmark' title='Permanent Link: Trailing Zeros in 100 Factorial'>Trailing Zeros in 100 Factorial</a></li>
<li><a href='http://www.mytechinterviews.com/chess-squares' rel='bookmark' title='Permanent Link: Chess Squares'>Chess Squares</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=Qbs4QnFPYBI:rCaxaGaOeFY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=Qbs4QnFPYBI:rCaxaGaOeFY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=Qbs4QnFPYBI:rCaxaGaOeFY:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=Qbs4QnFPYBI:rCaxaGaOeFY:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=Qbs4QnFPYBI:rCaxaGaOeFY:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=Qbs4QnFPYBI:rCaxaGaOeFY:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=Qbs4QnFPYBI:rCaxaGaOeFY:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=Qbs4QnFPYBI:rCaxaGaOeFY:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/MyTechInterviews/~4/Qbs4QnFPYBI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.mytechinterviews.com/challenge-camel-and-bananas/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		<feedburner:origLink>http://www.mytechinterviews.com/challenge-camel-and-bananas</feedburner:origLink></item>
		<item>
		<title>Challenge – First Common Ancestor</title>
		<link>http://feedproxy.google.com/~r/MyTechInterviews/~3/6ul08VIzE_k/first-common-ancestor</link>
		<comments>http://www.mytechinterviews.com/first-common-ancestor#comments</comments>
		<pubDate>Wed, 02 Mar 2011 06:16:18 +0000</pubDate>
		<dc:creator>Smiley</dc:creator>
				<category><![CDATA[Challenge]]></category>
		<category><![CDATA[Trees]]></category>
		<category><![CDATA[ancestor]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[common]]></category>
		<category><![CDATA[first]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[tree]]></category>
		<category><![CDATA[tricky]]></category>

		<guid isPermaLink="false">http://www.mytechinterviews.com/?p=1045</guid>
		<description><![CDATA[Question: How would you find the first common ancestor of two nodes in a binary search tree? First as in the lowest in the tree. Another way to ask is to find the lowest common ancestor of two nodes. Challenge: Do you know the answer to this question? Post in the comments. Answers will be posted [...]


Related posts:<ol><li><a href='http://www.mytechinterviews.com/balanced-tree' rel='bookmark' title='Permanent Link: Balanced Tree'>Balanced Tree</a></li>
<li><a href='http://www.mytechinterviews.com/equal-probability-between-1-and-7' rel='bookmark' title='Permanent Link: Challenge &#8211; Equal Probability between 1 and 7'>Challenge &#8211; Equal Probability between 1 and 7</a></li>
<li><a href='http://www.mytechinterviews.com/reverse-a-linked-list' rel='bookmark' title='Permanent Link: Reverse a Linked-List'>Reverse a Linked-List</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mytechinterviews.com%2Ffirst-common-ancestor"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mytechinterviews.com%2Ffirst-common-ancestor&amp;source=mytechinterview&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><strong>Question:</strong> How would you find the first common ancestor of two nodes in a binary search tree? First as in the lowest in the tree. Another way to ask is to find the lowest common ancestor of two nodes.</p>
<p><strong>Challenge:</strong> Do you know the answer to this question? Post in the comments. Answers will be posted<strong> March 6th</strong>.</p>
<p>Meanwhile, check out the challenges from previous weeks <strong><a href="http://www.mytechinterviews.com/category/challenge">here</a></strong>.</p>
<p><strong>Answer:</strong></p>
<pre>TreeNode findFirstCommonAncestor(TreeNode root, TreeNode p,
	TreeNode q) {

    if (root == null) {
        return null;
    }

    if (root == p || root == q) {
	return root;
    }

    TreeNode left = findFirstCommonAncestor(root.left, p, q);
    TreeNode right = findFirstCommonAncestor(root.right, p, q);

    if ((left == p &amp;&amp; right == q) ||
        (left == q &amp;&amp; right == q)) {
	return root;
    }

    return (left != null) ? left : right;
}</pre>
<p><strong>Alternate:</strong></p>
<pre>TreeNode findFirstCommonAncestor(TreeNode root, int p, int q) {

    if (root == null) {
        return null;
    }

    if (root.value == p || root.value == q) {
	return root;
    }

    if (root.value > p &amp;&amp; root.value > q ) {
        return findFirstCommonAncestor(root.left, p, q);
    }
    else if (root.value < p &amp;&amp; root.value < q ) {
        return findFirstCommonAncestor(root.right, p, q);
    }
    else {
        return root;
    }
}</pre>
<p>Thanks Dave and Sunil for pointing out the alternate solution.<br />
&nbsp;</p>


<p>Related posts:<ol><li><a href='http://www.mytechinterviews.com/balanced-tree' rel='bookmark' title='Permanent Link: Balanced Tree'>Balanced Tree</a></li>
<li><a href='http://www.mytechinterviews.com/equal-probability-between-1-and-7' rel='bookmark' title='Permanent Link: Challenge &#8211; Equal Probability between 1 and 7'>Challenge &#8211; Equal Probability between 1 and 7</a></li>
<li><a href='http://www.mytechinterviews.com/reverse-a-linked-list' rel='bookmark' title='Permanent Link: Reverse a Linked-List'>Reverse a Linked-List</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=6ul08VIzE_k:TGqldt1w7JQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=6ul08VIzE_k:TGqldt1w7JQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=6ul08VIzE_k:TGqldt1w7JQ:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=6ul08VIzE_k:TGqldt1w7JQ:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=6ul08VIzE_k:TGqldt1w7JQ:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=6ul08VIzE_k:TGqldt1w7JQ:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=6ul08VIzE_k:TGqldt1w7JQ:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=6ul08VIzE_k:TGqldt1w7JQ:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/MyTechInterviews/~4/6ul08VIzE_k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.mytechinterviews.com/first-common-ancestor/feed</wfw:commentRss>
		<slash:comments>18</slash:comments>
		<feedburner:origLink>http://www.mytechinterviews.com/first-common-ancestor</feedburner:origLink></item>
		<item>
		<title>Challenge Series</title>
		<link>http://feedproxy.google.com/~r/MyTechInterviews/~3/tsZmEXmNfOM/challenge-series</link>
		<comments>http://www.mytechinterviews.com/challenge-series#comments</comments>
		<pubDate>Wed, 02 Mar 2011 06:04:01 +0000</pubDate>
		<dc:creator>Doggie</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.mytechinterviews.com/?p=1099</guid>
		<description><![CDATA[Hey everyone, We got great response from the interview challenge posted last week. We&#8217;ve decided to make a weekly challenge series (hopefully we have enough questions to keep up ). Do you have any interesting questions you would like to share? Or any questions that you&#8217;d like answered? Let us know and we can help [...]


Related posts:<ol><li><a href='http://www.mytechinterviews.com/equal-probability-between-1-and-7' rel='bookmark' title='Permanent Link: Challenge &#8211; Equal Probability between 1 and 7'>Challenge &#8211; Equal Probability between 1 and 7</a></li>
<li><a href='http://www.mytechinterviews.com/first-common-ancestor' rel='bookmark' title='Permanent Link: Challenge &#8211; First Common Ancestor'>Challenge &#8211; First Common Ancestor</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fchallenge-series"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fchallenge-series&amp;source=mytechinterview&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Hey everyone,</p>
<p>We got great response from the interview challenge posted last week. We&#8217;ve decided to make a weekly challenge series (hopefully we have enough questions to keep up <img src='http://www.mytechinterviews.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ).</p>
<p>Do you have any interesting questions you would like to share? Or any questions that you&#8217;d like answered? Let us know and we can help draw the audience to answer your questions!</p>
<p>Cheers!</p>


<p>Related posts:<ol><li><a href='http://www.mytechinterviews.com/equal-probability-between-1-and-7' rel='bookmark' title='Permanent Link: Challenge &#8211; Equal Probability between 1 and 7'>Challenge &#8211; Equal Probability between 1 and 7</a></li>
<li><a href='http://www.mytechinterviews.com/first-common-ancestor' rel='bookmark' title='Permanent Link: Challenge &#8211; First Common Ancestor'>Challenge &#8211; First Common Ancestor</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=tsZmEXmNfOM:V3vQlprVW4k:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=tsZmEXmNfOM:V3vQlprVW4k:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=tsZmEXmNfOM:V3vQlprVW4k:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=tsZmEXmNfOM:V3vQlprVW4k:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=tsZmEXmNfOM:V3vQlprVW4k:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=tsZmEXmNfOM:V3vQlprVW4k:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=tsZmEXmNfOM:V3vQlprVW4k:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=tsZmEXmNfOM:V3vQlprVW4k:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/MyTechInterviews/~4/tsZmEXmNfOM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.mytechinterviews.com/challenge-series/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.mytechinterviews.com/challenge-series</feedburner:origLink></item>
		<item>
		<title>Balanced Tree</title>
		<link>http://feedproxy.google.com/~r/MyTechInterviews/~3/2b8xR1ZzJ_Q/balanced-tree</link>
		<comments>http://www.mytechinterviews.com/balanced-tree#comments</comments>
		<pubDate>Sun, 27 Feb 2011 20:31:48 +0000</pubDate>
		<dc:creator>Smiley</dc:creator>
				<category><![CDATA[Trees]]></category>
		<category><![CDATA[balanced]]></category>
		<category><![CDATA[depth]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[recursive]]></category>
		<category><![CDATA[trees]]></category>

		<guid isPermaLink="false">http://www.mytechinterviews.com/?p=1019</guid>
		<description><![CDATA[Question: How would you check if a binary tree is balanced? Answer: A tree is considered balanced when the difference between the min depth and max depth does not exceed 1. Recursive algorithms always work well on trees, so here&#8217;s some code. int min_depth( Node * root ) { if( !root ) { return 0; [...]


Related posts:<ol><li><a href='http://www.mytechinterviews.com/first-common-ancestor' rel='bookmark' title='Permanent Link: Challenge &#8211; First Common Ancestor'>Challenge &#8211; First Common Ancestor</a></li>
<li><a href='http://www.mytechinterviews.com/singly-linked-list-delete-node' rel='bookmark' title='Permanent Link: Singly Linked List &#8211; Delete Node'>Singly Linked List &#8211; Delete Node</a></li>
<li><a href='http://www.mytechinterviews.com/loop-in-a-singly-linked-list' rel='bookmark' title='Permanent Link: Loop in a Singly-Linked List'>Loop in a Singly-Linked List</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fbalanced-tree"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fbalanced-tree&amp;source=mytechinterview&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><strong>Question:</strong> How would you check if a binary tree is balanced?<br />
<strong>Answer:</strong> A tree is considered balanced when the difference between the min depth and max depth does not exceed 1.</p>
<p>Recursive algorithms always work well on trees, so here&#8217;s some code.</p>
<pre>int min_depth( Node * root ) {
    if( !root ) {
        return 0;
    }
    return 1 + min( min_depth( root-&gt;left ),
                    min_depth( root-&gt;right ));
}

int max_depth( Node * root ) {
    if( !root ) {
        return 0;
    }
    return 1 + max( max_depth( root-&gt;left ),
                            max_depth( root-&gt;right ));
} 

bool is_balanced( Node * root ) {
    return ( max_depth( root ) - min_depth( root ) ) &lt;= 1
}</pre>
<p>Simple enough, right? Spread the word on Twitter and Digg!</p>
<p>&nbsp;</p>
<p>&nbsp;</p>


<p>Related posts:<ol><li><a href='http://www.mytechinterviews.com/first-common-ancestor' rel='bookmark' title='Permanent Link: Challenge &#8211; First Common Ancestor'>Challenge &#8211; First Common Ancestor</a></li>
<li><a href='http://www.mytechinterviews.com/singly-linked-list-delete-node' rel='bookmark' title='Permanent Link: Singly Linked List &#8211; Delete Node'>Singly Linked List &#8211; Delete Node</a></li>
<li><a href='http://www.mytechinterviews.com/loop-in-a-singly-linked-list' rel='bookmark' title='Permanent Link: Loop in a Singly-Linked List'>Loop in a Singly-Linked List</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=2b8xR1ZzJ_Q:0oZq16Xx8bg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=2b8xR1ZzJ_Q:0oZq16Xx8bg:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=2b8xR1ZzJ_Q:0oZq16Xx8bg:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=2b8xR1ZzJ_Q:0oZq16Xx8bg:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=2b8xR1ZzJ_Q:0oZq16Xx8bg:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=2b8xR1ZzJ_Q:0oZq16Xx8bg:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=2b8xR1ZzJ_Q:0oZq16Xx8bg:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=2b8xR1ZzJ_Q:0oZq16Xx8bg:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/MyTechInterviews/~4/2b8xR1ZzJ_Q" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.mytechinterviews.com/balanced-tree/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.mytechinterviews.com/balanced-tree</feedburner:origLink></item>
		<item>
		<title>Queue Using Stacks</title>
		<link>http://feedproxy.google.com/~r/MyTechInterviews/~3/QB0kg9BFGuA/queue-using-stacks</link>
		<comments>http://www.mytechinterviews.com/queue-using-stacks#comments</comments>
		<pubDate>Sun, 27 Feb 2011 18:26:36 +0000</pubDate>
		<dc:creator>Smiley</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[puzzle]]></category>
		<category><![CDATA[queue]]></category>
		<category><![CDATA[stack]]></category>

		<guid isPermaLink="false">http://www.mytechinterviews.com/?p=979</guid>
		<description><![CDATA[Question: How would you use stacks to implement a queue? Answer: So how could we go about doing this? What if we used two stacks and used one as the incoming stack and the other as the outgoing stack? A queue is a FIFO structure, so when we enqueue something, we need to make sure [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fqueue-using-stacks"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fqueue-using-stacks&amp;source=mytechinterview&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><strong>Question:</strong> How would you use stacks to implement a queue?</p>
<p><strong>Answer: </strong>So how could we go about doing this? What if we used two stacks and used one as the incoming stack and the other as the outgoing stack? A queue is a FIFO structure, so when we enqueue something, we need to make sure that it does not get popped off before something that was already there. Similarly, when we dequeue something, we have to make sure that elements that were inserted earlier get ejected first.</p>
<p>What&#8217;s better than some code!</p>
<pre>Stack in;
Stack out;
void enqueue( int value ) {
    while( !out.isEmpty() ) {
        in.push( out.pop() );
    }
    in.push( value );
}

int dequeue() {
    while( !in.isEmpty() ) {
        out.push( in.pop() );
    }
    return out.pop();
}</pre>
<p>Isn&#8217;t that cool? Help share this on Twitter!</p>


<p>No related posts.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=QB0kg9BFGuA:RzlIh_Sg19k:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=QB0kg9BFGuA:RzlIh_Sg19k:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=QB0kg9BFGuA:RzlIh_Sg19k:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=QB0kg9BFGuA:RzlIh_Sg19k:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=QB0kg9BFGuA:RzlIh_Sg19k:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=QB0kg9BFGuA:RzlIh_Sg19k:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=QB0kg9BFGuA:RzlIh_Sg19k:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=QB0kg9BFGuA:RzlIh_Sg19k:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/MyTechInterviews/~4/QB0kg9BFGuA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.mytechinterviews.com/queue-using-stacks/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.mytechinterviews.com/queue-using-stacks</feedburner:origLink></item>
		<item>
		<title>Bit Swaps</title>
		<link>http://feedproxy.google.com/~r/MyTechInterviews/~3/AXTzlI4-7wQ/bit-swaps</link>
		<comments>http://www.mytechinterviews.com/bit-swaps#comments</comments>
		<pubDate>Thu, 24 Feb 2011 04:35:36 +0000</pubDate>
		<dc:creator>Smiley</dc:creator>
				<category><![CDATA[Bit Manipulation]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[count]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[integer]]></category>
		<category><![CDATA[manipulation]]></category>
		<category><![CDATA[swap]]></category>

		<guid isPermaLink="false">http://www.mytechinterviews.com/?p=860</guid>
		<description><![CDATA[Question: How would you find the number of bit swaps required to convert integer A to integer B? Answer: Gut instinct might suggest that you go through every bit of A and every bit of B while simultaneously updating a count of the bits that are different. But how about a cooler way to solve [...]


Related posts:<ol><li><a href='http://www.mytechinterviews.com/power-of-2' rel='bookmark' title='Permanent Link: Power of 2'>Power of 2</a></li>
<li><a href='http://www.mytechinterviews.com/number-of-ones' rel='bookmark' title='Permanent Link: Number of Ones'>Number of Ones</a></li>
<li><a href='http://www.mytechinterviews.com/how-many-trailing-zeros-in-100-factorial' rel='bookmark' title='Permanent Link: Trailing Zeros in 100 Factorial'>Trailing Zeros in 100 Factorial</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fbit-swaps"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fbit-swaps&amp;source=mytechinterview&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://www.mytechinterviews.com/wp-content/uploads/2011/02/images.jpeg"></a><a href="http://www.mytechinterviews.com/wp-content/uploads/2011/02/images.jpeg"><img class="alignright size-medium wp-image-955" title="images" src="http://www.mytechinterviews.com/wp-content/uploads/2011/02/images-162x300.jpg" alt="" width="100" height="200" /></a><strong>Question:</strong> How would you find the number of bit swaps required to convert integer A to integer B?</p>
<p><strong>Answer: </strong>Gut instinct might suggest that you go through every bit of A and every bit of B while simultaneously updating a count of the bits that are different. But how about a cooler way to solve this?</p>
<p>A key to solving a lot of bit manipulation questions is the use of the XOR functionality.</p>
<pre>int bit_swaps_required( int a, int b ) {
    unsigned int count = 0;
    for( int c = a ^ b; c != 0; c = c &gt;&gt; 1 ) {
        count += c &amp; 1;
    }
    return count;
}</pre>
<p>Simple right?</p>
<p>Help share this post through Twitter!</p>


<p>Related posts:<ol><li><a href='http://www.mytechinterviews.com/power-of-2' rel='bookmark' title='Permanent Link: Power of 2'>Power of 2</a></li>
<li><a href='http://www.mytechinterviews.com/number-of-ones' rel='bookmark' title='Permanent Link: Number of Ones'>Number of Ones</a></li>
<li><a href='http://www.mytechinterviews.com/how-many-trailing-zeros-in-100-factorial' rel='bookmark' title='Permanent Link: Trailing Zeros in 100 Factorial'>Trailing Zeros in 100 Factorial</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=AXTzlI4-7wQ:uJ7Ooab4Cy8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=AXTzlI4-7wQ:uJ7Ooab4Cy8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=AXTzlI4-7wQ:uJ7Ooab4Cy8:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=AXTzlI4-7wQ:uJ7Ooab4Cy8:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=AXTzlI4-7wQ:uJ7Ooab4Cy8:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=AXTzlI4-7wQ:uJ7Ooab4Cy8:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=AXTzlI4-7wQ:uJ7Ooab4Cy8:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=AXTzlI4-7wQ:uJ7Ooab4Cy8:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/MyTechInterviews/~4/AXTzlI4-7wQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.mytechinterviews.com/bit-swaps/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.mytechinterviews.com/bit-swaps</feedburner:origLink></item>
		<item>
		<title>Challenge – Equal Probability between 1 and 7</title>
		<link>http://feedproxy.google.com/~r/MyTechInterviews/~3/RzNr2Hyraco/equal-probability-between-1-and-7</link>
		<comments>http://www.mytechinterviews.com/equal-probability-between-1-and-7#comments</comments>
		<pubDate>Thu, 24 Feb 2011 02:37:39 +0000</pubDate>
		<dc:creator>Doggie</dc:creator>
				<category><![CDATA[Challenge]]></category>

		<guid isPermaLink="false">http://www.mytechinterviews.com/?p=848</guid>
		<description><![CDATA[Question: Write a method to generate a random number between 1 and 7, given a method that generates a random number between 1 and 5. The distribution between each of the numbers must be uniform. Challenge: Do you know the answer to this question? Post in the comments. Let&#8217;s see who is up for the challenge. [...]


Related posts:<ol><li><a href='http://www.mytechinterviews.com/probability-of-a-car-passing-by' rel='bookmark' title='Permanent Link: Probability of a Car Passing By'>Probability of a Car Passing By</a></li>
<li><a href='http://www.mytechinterviews.com/red-and-blue-marbles' rel='bookmark' title='Permanent Link: Red and Blue Marbles'>Red and Blue Marbles</a></li>
<li><a href='http://www.mytechinterviews.com/10-google-interview-questions' rel='bookmark' title='Permanent Link: 10 Google Interview Puzzles'>10 Google Interview Puzzles</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fequal-probability-between-1-and-7"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mytechinterviews.com%2Fequal-probability-between-1-and-7&amp;source=mytechinterview&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><strong>Question: </strong>Write a method to generate a random number between 1 and 7, given a method that generates a random number between 1 and 5. The distribution between each of the numbers must be uniform.</p>
<p><strong>Challenge:</strong> Do you know the answer to this question? Post in the comments. Let&#8217;s see who is <strong>up for the challenge</strong>. Answers will be posted <strong>February 26th</strong>.</p>
<p>Let&#8217;s think of this like a decision tree. Each rand5() will be a decision. After 2 tries, we have 25 possible solutions. We try to get maximum bunches of 7 as we can (1 &#8211; 21, 3 sets of 7). If we get any of the other values, we just try again. Since the probability of getting each of 21 values are the same every time, trying again won&#8217;t affect their probabilities.</p>
<p><img src="http://www.mytechinterviews.com/wp-content/uploads/2011/02/1-7.png" alt="Equal Probability 1 to 7" /></p>
<pre>int rand7() {
    while (1) {
        int num = 5*(rand5()-1) + rand5();
        if (num &lt; 22) return ((num % 7) + 1);
    }
}</pre>
<p>That was fun, right? Anyone up for another challenge? Watch out for it next tuesday (March 1st).</p>


<p>Related posts:<ol><li><a href='http://www.mytechinterviews.com/probability-of-a-car-passing-by' rel='bookmark' title='Permanent Link: Probability of a Car Passing By'>Probability of a Car Passing By</a></li>
<li><a href='http://www.mytechinterviews.com/red-and-blue-marbles' rel='bookmark' title='Permanent Link: Red and Blue Marbles'>Red and Blue Marbles</a></li>
<li><a href='http://www.mytechinterviews.com/10-google-interview-questions' rel='bookmark' title='Permanent Link: 10 Google Interview Puzzles'>10 Google Interview Puzzles</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=RzNr2Hyraco:b--KmPUZKm0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=RzNr2Hyraco:b--KmPUZKm0:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=RzNr2Hyraco:b--KmPUZKm0:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=RzNr2Hyraco:b--KmPUZKm0:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?i=RzNr2Hyraco:b--KmPUZKm0:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=RzNr2Hyraco:b--KmPUZKm0:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=RzNr2Hyraco:b--KmPUZKm0:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/MyTechInterviews?a=RzNr2Hyraco:b--KmPUZKm0:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/MyTechInterviews?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/MyTechInterviews/~4/RzNr2Hyraco" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.mytechinterviews.com/equal-probability-between-1-and-7/feed</wfw:commentRss>
		<slash:comments>69</slash:comments>
		<feedburner:origLink>http://www.mytechinterviews.com/equal-probability-between-1-and-7</feedburner:origLink></item>
	</channel>
</rss>

