<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Welcome to techInterview, a site for technical interview questions, brain teasers, puzzles, quizzles (whatever the heck those are) and other things that make you think!</description><title>techinterview</title><generator>Tumblr (3.0; @techinterview)</generator><link>http://www.techinterview.org/</link><item><title>Getting a fair result with an unfair coin</title><description>&lt;p&gt;How can you get a fair coin toss if someone hands you a coin that is weighted to come up heads more often than tails?&lt;/p&gt;</description><link>http://www.techinterview.org/post/3233458616</link><guid>http://www.techinterview.org/post/3233458616</guid><pubDate>Fri, 11 Feb 2011 08:41:31 -0500</pubDate></item><item><title>We are NY Tech asks:

&amp;#8220;How many unique areas of human knowledge have the right size of...</title><description>&lt;p&gt;&lt;a href="http://wearenytech.com/39-joel-spolsky-ceo-co-founder-of-stack-overflow"&gt;We are NY Tech asks&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;#8220;How many unique areas of human knowledge have the right size of passionate users to make it as a &lt;a href="http://www.stackexchange.com"&gt;Stack Exchange site&lt;/a&gt;?&amp;#8221;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://wearenytech.com/39-joel-spolsky-ceo-co-founder-of-stack-overflow"&gt;Answer: 30,000.&lt;/a&gt;&lt;/p&gt;</description><link>http://www.techinterview.org/post/2611623406</link><guid>http://www.techinterview.org/post/2611623406</guid><pubDate>Wed, 05 Jan 2011 14:12:00 -0500</pubDate></item><item><title>Storing 1 million phone numbers</title><description>&lt;p&gt;What is the most efficient way, memory-wise, to store 1 million phone numbers?  Apparently this is an interview question at Google, although this seems like its a bit too easy.&lt;/p&gt;</description><link>http://www.techinterview.org/post/1731330243</link><guid>http://www.techinterview.org/post/1731330243</guid><pubDate>Mon, 29 Nov 2010 18:50:00 -0500</pubDate><category>google</category><category>interview</category></item><item><title>Reverse a String</title><description>&lt;p&gt;A typical programming interview question is &amp;#8220;reverse a string, in place&amp;#8221;. if you understand pointers, the solution is simple. even if you don&amp;#8217;t, it can be accomplished using array indices. i usually ask candidates this question first, so they get the algorithm in their head. then i play dirty by asking them to reverse the string word by word, in place. for example if our string is &amp;#8220;the house is blue&amp;#8221;, the return value would be &amp;#8220;blue is house the&amp;#8221;. the words are reversed, but the letters are still in order (within the word).&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;Solving the initial problem of just reversing a string can either be a huge help or a frustrating hinderance. most likely the first attempt will be to solve it the same way, by swapping letters at the front of the string with letters at the back, and then adding some logic to keep the words in order. this attempt will lead to confusion pretty quickly.&lt;/p&gt;
&lt;p&gt;for example, if we start by figuring out that &amp;#8220;the&amp;#8221; is 3 letters long and then try to put the &amp;#8220;t&amp;#8221; from &amp;#8220;the&amp;#8221; where the &amp;#8220;l&amp;#8221; from &amp;#8220;blue&amp;#8221; is, we encounter a problem. where do we put the &amp;#8220;l&amp;#8221; from &amp;#8220;blue&amp;#8221;? hmm&amp;#8230; well we could have also figured out how long &amp;#8220;blue&amp;#8221; was and that would tell us where to put the &amp;#8220;l&amp;#8221; at&amp;#8230; but the &amp;#8220;e&amp;#8221; from &amp;#8220;blue&amp;#8221; needs to go into the space after &amp;#8220;the&amp;#8221;. argh. its getting quite confusing. in fact, i would be delighted to even see a solution to this problem using this attack method. i don&amp;#8217;t think its impossible, but i think it is so complex that it&amp;#8217;s not worth pursuing.&lt;/p&gt;
&lt;p&gt;here&amp;#8217;s a hint. remember before when we just reversed &amp;#8220;the house is blue&amp;#8221;? what happened?&lt;/p&gt;
&lt;pre&gt;initial: the house is blue&lt;br/&gt;reverse: eulb si esuoh eht&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;look at the result for a minute. notice anything? if you still don&amp;#8217;t see it, try this.&lt;/p&gt;
&lt;pre&gt;initial: the house is blue&lt;br/&gt;reverse: eulb si esuoh eht&lt;br/&gt;wanted : blue is house the&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;the solution can be attained by first reversing the string normally, and then just reversing each word.&lt;/p&gt;</description><link>http://www.techinterview.org/post/526374214</link><guid>http://www.techinterview.org/post/526374214</guid><pubDate>Fri, 16 Apr 2010 15:33:37 -0400</pubDate></item><item><title>100 Doors in a Row</title><description>&lt;p&gt;Problem: you have 100 doors in a row that are all initially closed. you make 100 passes by the doors starting with the first door every time. the first time through you visit every door and toggle the door (if the door is closed, you open it, if its open, you close it). the second time you only visit every 2nd door (door #2, #4, #6). the third time, every 3rd door (door #3, #6, #9), etc, until you only visit the 100th door.&lt;/p&gt;
&lt;p&gt;question: what state are the doors in after the last pass? which are open which are closed?&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;For example, after the first pass every door is open. on the second pass you only visit the even doors (2,4,6,8&amp;#8230;) so now the even doors are closed and the odd ones are opened. the third time through you will close door 3 (opened from the first pass), open door 6 (closed from the second pass), etc..&lt;/p&gt;
&lt;p&gt;question: what state are the doors in after the last pass? which are open which are closed?&lt;/p&gt;
&lt;p&gt;solution: you can figure out that for any given door, say door #42, you will visit it for every divisor it has. so 42 has 1 &amp;amp; 42, 2 &amp;amp; 21, 3 &amp;amp; 14, 6 &amp;amp; 7. so on pass 1 i will open the door, pass 2 i will close it, pass 3 open, pass 6 close, pass 7 open, pass 14 close, pass 21 open, pass 42 close. for every pair of divisors the door will just end up back in its initial state. so you might think that every door will end up closed? well what about door #9. 9 has the divisors 1 &amp;amp; 9, 3 &amp;amp; 3. but 3 is repeated because 9 is a perfect square, so you will only visit door #9, on pass 1, 3, and 9&amp;#8230; leaving it open at the end. only perfect square doors will be open at the end.&lt;/p&gt;</description><link>http://www.techinterview.org/post/526370758</link><guid>http://www.techinterview.org/post/526370758</guid><pubDate>Fri, 16 Apr 2010 15:31:42 -0400</pubDate></item><item><title>Red Marbles, Blue Marbles</title><description>&lt;p&gt;Problem: you have two jars, 50 red marbles, 50 blue marbles. you need to place all the marbles into the jars such that when you blindly pick one marble out of one jar, you maximize the chances that it will be red. (when picking, you&amp;#8217;ll first randomly pick a jar, and then randomly pick a marble out of that jar) you can arrange the marbles however you like, but each marble must be in a jar.&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;Chance! chance is easy if you know how to do the formula. we know that we have two choices to make. first we&amp;#8217;ll pick a jar, and each jar will have a 1/2 chance of being picked. then we&amp;#8217;ll pick a marble, and depending how we stack the marbles, we&amp;#8217;ll have a (# of red marbles in jar)/(# of total marbles in jar) chance of getting a red one.&lt;/p&gt;
&lt;p&gt;for example, say we put all the red marbles into &lt;strong&gt;jar A&lt;/strong&gt; and all the blue ones into &lt;strong&gt;jar B&lt;/strong&gt;. then our chances for picking a red one are:&lt;/p&gt;
&lt;p&gt;1/2 chance we pick &lt;strong&gt;jar A&lt;/strong&gt; * 50/50 chance we pick a red marble&lt;br/&gt;1/2 chance we pick &lt;strong&gt;jar B&lt;/strong&gt; * 0/50 chance we pick a red marble&lt;/p&gt;
&lt;p&gt;do the math and you get 1/2 chance for a red marble from &lt;strong&gt;jar A&lt;/strong&gt; and a 0/2 chance for a red marble from &lt;strong&gt;jar B&lt;/strong&gt;. add &amp;#8216;em up and you get the result = 1/2 chance for picking a red marble.&lt;/p&gt;
&lt;p&gt;think about it for awhile and see if you can figure out the right combination. we had a 50/50 (guaranteed) chance in picking a red marble from &lt;strong&gt;jar A&lt;/strong&gt;, but we didn&amp;#8217;t have to have 50 red marbles in there to guarantee those fantastic odds, did we? we could&amp;#8217;ve just left 1 red marble in there and the odds are still 1/1. then we can take all those other marbles and throw them in &lt;strong&gt;jar B&lt;/strong&gt; to help the odds out there.&lt;/p&gt;
&lt;p&gt;let&amp;#8217;s look at those chances:&lt;/p&gt;
&lt;p&gt;1/2 we pick &lt;strong&gt;jar A&lt;/strong&gt; * 1/1 we pick a red marble&lt;br/&gt;1/2 we pick &lt;strong&gt;jar B&lt;/strong&gt; * 49/99 we pick a red marble&lt;/p&gt;
&lt;p&gt;do the math and add them up to get 1/2 + 49/198 = 148/198, which is almost 3/4.&lt;/p&gt;
&lt;p&gt;we can prove these are the best odds in a somewhat non-formal way as follows. our goal is to maximize the odds of picking a red marble. therefore we can subdivide this goal into maximizing the odds of picking a red marble in &lt;strong&gt;jar A&lt;/strong&gt; and maximizing the odds of picking a red marble in &lt;strong&gt;jar B&lt;/strong&gt;. if we do that, then we will have achieved our goal. it is true that by placing more red marbles into a jar we will increase the chances of picking a red marble. it is also true that by reducing the number of blue marbles in a jar we will increase the odds also. we&amp;#8217;ve maximized the odds in &lt;strong&gt;jar A&lt;/strong&gt; since 1/1 is the maximum odds by reducing the number of blue marbles to 0 (the minimum). we&amp;#8217;ve also maximized the number of red marbles in &lt;strong&gt;jar B&lt;/strong&gt;. if we added any more red marbles to &lt;strong&gt;jar B&lt;/strong&gt; we would have to take them out of &lt;strong&gt;jar A&lt;/strong&gt; which reduce the odds there to 0 (very bad). if we took any more blue ones out of &lt;strong&gt;jar B&lt;/strong&gt; we would have to put them in &lt;strong&gt;jar A&lt;/strong&gt; which reduce the odds there by 50% (very bad).&lt;/p&gt;
&lt;p&gt;it wasn&amp;#8217;t really a good proof, but QED anyway :-P&lt;/p&gt;</description><link>http://www.techinterview.org/post/526363745</link><guid>http://www.techinterview.org/post/526363745</guid><pubDate>Fri, 16 Apr 2010 15:26:59 -0400</pubDate></item><item><title>Bumblebee</title><description>&lt;p&gt;problem: two trains enter a tunnel 200 miles long (yeah, its a big tunnel) travelling at 100 mph at the same time from opposite directions. as soon as they enter the tunnel a supersonic bee flying at 1000 mph starts from one train and heads toward the other one. as soon as it reaches the other one it turns around and heads back toward the first, going back and forth between the trains until the trains collide in a fiery explosion in the middle of the tunnel (the bee survives). how far did the bee travel?&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;solution: this puzzle falls pretty high on my &lt;a href="http://techinterview.org/aha.html"&gt;aha&lt;/a&gt; scale. my first inclination when i heard it was to think &amp;#8220;ok, so i just need to sum up the distances that the bee travels&amp;#8230;&amp;#8221; but then you quickly realize that its a difficult (not impossible) summation which the interviewer could hardly expect you to answer (unless i guess if you are looking for a job as a quant). &amp;#8220;there must be a trick&amp;#8221; you say. eh, sort of i guess, enough to say that this question is a stupid interview question.&lt;/p&gt;
&lt;p&gt;the tunnel is 200 miles long. the trains meet in the middle travelling at 100 mph, so it takes them an hour to reach the middle. the bee is travelling 1000 mph for an hour (since its flying the whole time the trains are racing toward one another) - so basically the bee goes 1000 miles.&lt;/p&gt;
&lt;p&gt;there is no process to explain, so this question can&amp;#8217;t possibly teach you anything about the person. they either know it or they don&amp;#8217;t and if they already knew it before you asked, you&amp;#8217;re not going to be able to tell when they give you the answer. so don&amp;#8217;t ask this question. and if someone asks you this question, just tell them you&amp;#8217;ve already heard it before.&lt;/p&gt;</description><link>http://www.techinterview.org/post/526342692</link><guid>http://www.techinterview.org/post/526342692</guid><pubDate>Fri, 16 Apr 2010 15:13:52 -0400</pubDate></item><item><title>int atoi( char* pStr )</title><description>&lt;p&gt;Problem: write the definition for this function &lt;em&gt;without using any built-in functions&lt;/em&gt;. if pStr is null, return 0. if pStr contains non-numeric characters, either return 0 (ok) or return the number derived so far (better) (e.g. if its &amp;#8220;123A&amp;#8221;, then return 123). assume all numbers are positive. plus or minus signs can be considered non-numeric characters. in order to solve this program, the programmer must understand the difference between the integer 0 and the character &amp;#8216;0&amp;#8217;, and how converting &amp;#8216;0&amp;#8217; to an int, will not result in 0. in other words, they have to understand what ascii is all about.&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;string manipulation functions are great programming questions. they test whether the user can understand and translate into code simple algorithms. string functions test pointer arithmetic which usually shows &lt;a href="http://www.joelonsoftware.com/articles/fog0000000073.html"&gt;a knowledgeable programmer&lt;/a&gt;. also there are usually multiple solutions, some more efficient than others. plus people use them all the time so they should understand how they work. my favorite is atoi and i start the problem like this:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;int atoi( char* pStr )&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;write the definition for this function &lt;em&gt;without using any built-in functions&lt;/em&gt;. if pStr is null, return 0. if pStr contains non-numeric characters, either return 0 (ok) or return the number derived so far (better) (e.g. if its &amp;#8220;123A&amp;#8221;, then return 123). assume all numbers are positive. plus or minus signs can be considered non-numeric characters. in order to solve this program, the programmer must understand the difference between the integer 0 and the character &amp;#8216;0&amp;#8217;, and how converting &amp;#8216;0&amp;#8217; to an int, will not result in 0. in other words, they have to understand what ascii is all about. if they are stuck solving this problem, just ask them first to write:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;charToInt(char c)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;if they can&amp;#8217;t do that then they basically missed half the problem. any moderately talented programmer who has a CS degree knows how to convert a char to an int. (note i said convert, not cast. &lt;code&gt;charToInt('9')&lt;/code&gt; should return 9.)&lt;/p&gt;
&lt;p&gt;when they start to solve the problem you will notice that they must make a choice in how they will process the string - from left to right or right to left. i will discuss both methods and the difficulties encountered in each.&lt;/p&gt;
&lt;p&gt;&amp;#8220;right to left&amp;#8221; - this method starts at the right hand letter of the string and converts that character to an int. it then stores this value after promoting it to its correct &amp;#8220;tens&amp;#8221; place.&lt;/p&gt;
&lt;pre&gt;int atoi( char* pStr ) &lt;br/&gt;{ &lt;br/&gt;  int iRetVal = 0; &lt;br/&gt;  int iTens = 1;&lt;br/&gt; &lt;br/&gt;  if ( pStr )&lt;br/&gt;  {&lt;br/&gt;    char* pCur = pStr; &lt;br/&gt;    while (*pCur) &lt;br/&gt;      pCur++;&lt;br/&gt; &lt;br/&gt;    pCur--;&lt;br/&gt; &lt;br/&gt;    while ( pCur &amp;gt;= pStr &amp;amp;&amp;amp; *pCur &amp;lt;= '9' &amp;amp;&amp;amp; *pCur &amp;gt;= '0' ) &lt;br/&gt;    { &lt;br/&gt;      iRetVal += ((*pCur - '0') * iTens);&lt;br/&gt;      pCur--; &lt;br/&gt;      iTens *= 10; &lt;br/&gt;    }&lt;br/&gt;  } &lt;br/&gt;  return iRetVal; &lt;br/&gt;} &lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;&amp;#8220;left to right&amp;#8221; - this method keeps adding the number and multiplying the result by ten before continuing to the next number. e.g. if you had &amp;#8220;6234&amp;#8221; and you processed from left to right you&amp;#8217;d have 6, then if you kept reading you&amp;#8217;d multiply your result by 10 (6*10) to add a zero for where the next number would go. 60, and then you&amp;#8217;d slide the 2 into the zero place you just made. 62. do it again, 620, slide the next number in, 623.&lt;/p&gt;
&lt;pre&gt;int atoi( char* pStr ) &lt;br/&gt;{&lt;br/&gt;  int iRetVal = 0; &lt;br/&gt; &lt;br/&gt;  if ( pStr )&lt;br/&gt;  {&lt;br/&gt;    while ( *pStr &amp;amp;&amp;amp; *pStr &amp;lt;= '9' &amp;amp;&amp;amp; *pStr &amp;gt;= '0' ) &lt;br/&gt;    {&lt;br/&gt;      iRetVal = (iRetVal * 10) + (*pStr - '0');&lt;br/&gt;      pStr++;&lt;br/&gt;    }&lt;br/&gt;  } &lt;br/&gt;  return iRetVal; &lt;br/&gt;} &lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;i think the &amp;#8220;left to right&amp;#8221; method is a little bit cleaner, or maybe its just cooler. but both are &amp;#8220;correct&amp;#8221;.&lt;/p&gt;
&lt;p&gt;remember that debugging code on paper is somewhat hard. most programmers aren&amp;#8217;t used to studying code that much when you can just hit F-7, compile and see if the compiler barfs or not. if you notice an error, just ask them to step through a sample string drawing out what is happening with all the variables and the pointers in every step. they should find their mistake then and fix it (no points deducted).&lt;/p&gt;</description><link>http://www.techinterview.org/post/526339864</link><guid>http://www.techinterview.org/post/526339864</guid><pubDate>Fri, 16 Apr 2010 15:12:03 -0400</pubDate></item><item><title>Daughters' Ages</title><description>&lt;p&gt;Two MIT math grads bump into each other at Fairway on the upper west side. They haven&amp;#8217;t seen each other in over 20 years.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;the first grad says to the second&lt;/strong&gt;: &amp;#8220;how have you been?&amp;#8221;&lt;br/&gt;&lt;strong&gt;second&lt;/strong&gt;: &amp;#8220;great! i got married and i have three daughters now&amp;#8221;&lt;br/&gt;&lt;strong&gt;first&lt;/strong&gt;: &amp;#8220;really? how old are they?&amp;#8221;&lt;br/&gt;&lt;strong&gt;second&lt;/strong&gt;: &amp;#8220;well, the product of their ages is 72, and the sum of their ages is the same as the number on that building over there..&amp;#8221;&lt;br/&gt;&lt;strong&gt;first&lt;/strong&gt;: &amp;#8220;right, ok.. oh wait.. hmm, i still don&amp;#8217;t know&amp;#8221;&lt;br/&gt;&lt;strong&gt;second&lt;/strong&gt;: &amp;#8220;oh sorry, the oldest one just started to play the piano&amp;#8221;&lt;br/&gt;&lt;strong&gt;first&lt;/strong&gt;: &amp;#8220;wonderful! my oldest is the same age!&amp;#8221;&lt;/p&gt;
&lt;p&gt;problem: how old are the daughters?&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;solution: start with what you know. you know there are 3 daughters whose ages multiply to 72. let&amp;#8217;s look at the possibilities&amp;#8230;&lt;/p&gt;
&lt;pre&gt;&lt;strong&gt;Ages&lt;/strong&gt;:            &lt;strong&gt;Sum of ages&lt;/strong&gt;:&lt;br/&gt;1 1 72            74&lt;br/&gt;1 2 36            39&lt;br/&gt;1 3 24            28&lt;br/&gt;1 4 18            23&lt;br/&gt;1 6 12            19&lt;br/&gt;1 8 9             18&lt;br/&gt;2 2 18            22&lt;br/&gt;2 3 12            17&lt;br/&gt;2 4 9             15&lt;br/&gt;2 6 6             14&lt;br/&gt;3 3 8             14&lt;br/&gt;3 4 6             13&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;after looking at the building number the man still can&amp;#8217;t figure out what their ages are (we&amp;#8217;re assuming since he&amp;#8217;s an MIT math grad, he can factor 72 and add up the sums), so the building number must be 14, since that is the only sum that has more than one possibility.&lt;/p&gt;
&lt;p&gt;finally the man discovers that there is an oldest daughter. that rules out the &amp;#8220;2&amp;#160;6 6&amp;#8221; possibility since the two oldest would be twins. therefore, the daughters ages must be &amp;#8220;3&amp;#160;3 8&amp;#8221;.&lt;/p&gt;
&lt;p&gt;&lt;small&gt;(caveat: an astute reader pointed out that it &lt;strong&gt;is&lt;/strong&gt; possible for two siblings to have the same age but not be twins, for instance one is born in january, and the next is conceived right away and delivered in october. next october both siblings will be one year old. if a candidate points this out, extra credit points to him/her.)&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;this question is pretty neat, although there is certainly a bit of an &lt;a href="http://techinterview.org/aha.html"&gt;aha&lt;/a&gt; factor to it. the clues are given in such a way that you think you are missing information (the building number), but whats important isn&amp;#8217;t the building number, but the fact that the first man thought that it was enough information, but actually wasn&amp;#8217;t.&lt;/p&gt;
&lt;p&gt;even if the candidate doesn&amp;#8217;t know the solution, they could come up with some interesting thoughts. if they just stare at you and shrug &amp;#8220;i dunno&amp;#8221; then thank them for their time and don&amp;#8217;t give them a &lt;a href="http://www.fogcreek.com/"&gt;fogcreek&lt;/a&gt; pen.&lt;/p&gt;
&lt;p&gt;&lt;small&gt;credit to david for reminding me of this one&lt;/small&gt;&lt;/p&gt;</description><link>http://www.techinterview.org/post/526335645</link><guid>http://www.techinterview.org/post/526335645</guid><pubDate>Fri, 16 Apr 2010 15:09:33 -0400</pubDate></item><item><title>Palindromes</title><description>&lt;p&gt;Problem: this year on October 2, 2001, the date in MMDDYYYY format will be a palindrome (same forwards as backwards).&lt;br/&gt;10/02/2001&lt;br/&gt;when was the last date that this occurred on? (see if you can do it in your head!)&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;olution: we know the year has to be less than 2001 since we already have the palindrome for 10/02. it can&amp;#8217;t be any year in 1900 because that would result in a day of 91. same for 1800 down to 1400. it could be a year in 1300 because that would be the 31st day. so whats the latest year in 1300 that would make a month? at first i thought it would be 1321, since that would give us the 12th month, but we have to remember that we want the maximum &lt;strong&gt;year&lt;/strong&gt; in the 1300 century with a valid month, which would actually be 1390, since 09/31 is a valid date.&lt;/p&gt;
&lt;p&gt;but of course, a question like this wouldn&amp;#8217;t be complete without an &lt;em&gt;aha&lt;/em&gt; factor. and of course, there are not 31 days in september, only 30. so we have to go back to august 08 which means the correct date would be &lt;strong&gt;08/31/1380&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;palindromes also offer another great string question.&lt;br/&gt;write a function that tests for palindromes&lt;br/&gt;&lt;code&gt;bool isPalindrome( char* pStr )&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;if you start a pointer at the beginning and the end of the string and keep comparing characters while moving the pointers closer together, you can test if the string is the same forwards and backwards. notice that the pointers only have to travel to the middle, not all the way to the other end (to reduce redundancy).&lt;/p&gt;
&lt;pre&gt;bool isPalindrome( char* pStr )&lt;br/&gt;{&lt;br/&gt;  if ( pStr == NULL )&lt;br/&gt;   return false;&lt;br/&gt; &lt;br/&gt;  char* pEnd = pStr;&lt;br/&gt;  while ( *pEnd != '\0' )&lt;br/&gt;    pEnd++;&lt;br/&gt; &lt;br/&gt;  pEnd--;&lt;br/&gt; &lt;br/&gt;  while(pEnd &amp;gt; pStr)&lt;br/&gt;  {&lt;br/&gt;    if ( *pEnd != *pStr )&lt;br/&gt;      return false;&lt;br/&gt; &lt;br/&gt;    pEnd--;&lt;br/&gt;    pStr++;&lt;br/&gt;  }&lt;br/&gt; &lt;br/&gt;  return true;&lt;br/&gt;}&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;&lt;small&gt;thanks to tom for sending me this one! congrats on the wedding&amp;#8230;&lt;/small&gt;&lt;/p&gt;</description><link>http://www.techinterview.org/post/526332105</link><guid>http://www.techinterview.org/post/526332105</guid><pubDate>Fri, 16 Apr 2010 15:07:21 -0400</pubDate></item><item><title>Sum it Up</title><description>&lt;p&gt;Problem: you are given a sequence of numbers from 1 to n-1 with one of the numbers repeating only once. (example: 1&amp;#160;2 3&amp;#160;3 4&amp;#160;5). how can you find the repeating number? what if i give you the constraint that you can&amp;#8217;t use a dynamic amount of memory (i.e. the amount of memory you use can&amp;#8217;t be related to n)? &lt;br/&gt;what if there are two repeating numbers (and the same memory constraint?)&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;as a programmer, my first answer to this problem would be make a bit vector of size n, and every time you see the number, set its correspond index bit to 1. if the bit is already set, then that&amp;#8217;s the repeater. since there were no constraints in the question, this is an ok answer. its good because it makes sense if you draw it for someone, whether they are a programmer, mathemetician, or just your grandpa. its not the most efficient answer though.&lt;/p&gt;
&lt;p&gt;now, if i add the constraint that you can only use a fixed amount of memory (i.e. not determined by n) and it must run in O(n) time&amp;#8230; how do we solve it. adding all the numbers up from 1 to n-1 would give us a distinct sum. subtracting the total sum of all the numbers from the sum of n to n-1 ( which is (n)(n-1)/2 ) would give us the secret extra number.&lt;/p&gt;
&lt;p&gt;what if you can only use a fixed amount of memory, and &lt;strong&gt;two&lt;/strong&gt; of the numbers are repeated? we know that the numbers have a distinct sum, and the difference would be equal to the sum of our unknowns&lt;br/&gt;&lt;code&gt;c = a + b&lt;/code&gt;&lt;br/&gt;where c is the sum and &lt;em&gt;a&lt;/em&gt; and &lt;em&gt;b&lt;/em&gt; are the unknowns - c is a constant&lt;br/&gt;if we had another similar formula we could solve the two unknown equations. my first thought was that the numbers would have a distinct product - (n-1)!&lt;br/&gt;if we divide the total product by the (n-1)! product, we would get another equation&lt;br/&gt;&lt;code&gt;c2 = ab&lt;/code&gt;&lt;br/&gt;we could then solve the two equations to get them into quadratic formula notation &lt;br/&gt;&lt;code&gt;0 = ax^2 + bx + c&lt;/code&gt;&lt;br/&gt;and solve for the two values of x. this answer is correct but factorial grows really fast.&lt;/p&gt;
&lt;p&gt;some sort of sum would be better. the sum of the squares from n-1 to 1 would work. that would yield a function of the form&lt;br/&gt;&lt;code&gt;c2 = a^2 + b^2&lt;/code&gt;&lt;br/&gt;which could also be solved by using the quadratic equation.&lt;/p&gt;
&lt;p&gt;i think its fine to remind someone of the quadratic equation&amp;#8230; (maybe only because i myself had to look it up to solve the problem) i mean really though, the last time i used it was probably in 10th grade. as long as they get the idea that given two unknowns and two equations you can solve for the unknowns - thats the point.&lt;/p&gt;

&lt;p&gt;&lt;img alt="quadractic formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a" src="http://techinterview.org/Pictures/quadratic_1_.gif" border="0" height="58" hspace="4" width="159"/&gt;&lt;/p&gt;</description><link>http://www.techinterview.org/post/526329049</link><guid>http://www.techinterview.org/post/526329049</guid><pubDate>Fri, 16 Apr 2010 15:05:32 -0400</pubDate></item><item><title>Pirates</title><description>&lt;p&gt;Five pirates have 100 gold coins. they have to divide up the loot. in order of seniority (suppose pirate 5 is most senior, pirate 1 is least senior), the most senior pirate proposes a distribution of the loot. they vote and if at least 50% accept the proposal, the loot is divided as proposed. otherwise the most senior pirate is executed, and they start over again with the next senior pirate. what solution does the most senior pirate propose? assume they are very intelligent and extremely greedy (and that they would prefer not to die).&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;(to be clear on what 50% means, 3 pirates must vote for the proposal when there are 5 for it to pass. 2 if there are 4. 2 if there are 3. etc&amp;#8230; )&lt;/p&gt;
&lt;p&gt;solution: most of the time i get people who give answers like &amp;#8220;the most senior pirate takes half and divides the rest up among the least senior pirates.&amp;#8221; um, you missed the whole point to begin with. sorry.&lt;/p&gt;
&lt;p&gt;any answer without a specific logic behind it is invalid. if i ask you why pirate 5 gave x coins to pirate 1, please don&amp;#8217;t say &amp;#8220;because he&amp;#8217;s nice&amp;#8221;.&lt;/p&gt;
&lt;p&gt;now for the real solution. pirate 5 being the most senior knows that he needs to get 2 other people to vote for his solution in order for him not to be executed. so who can he get to vote for him, and why would they choose to vote for him? if you start thinking that pirate 4 will never vote for him, because he would rather have 5 die and then be in charge and take it all for himself, you are on the right track. but it gets more complicated.&lt;/p&gt;
&lt;p&gt;lets consider if there were only 1 pirate. obviously he would take it all for himself and no one would complain.&lt;/p&gt;
&lt;p&gt;if there were 2 pirates, pirate 2 being the most senior, he would just vote for himself and that would be 50% of the vote, so he&amp;#8217;s obviously going to keep all the money for himself.&lt;/p&gt;
&lt;p&gt;if there were 3 pirates, pirate 3 has to convince at least one other person to join in his plan. so who can he convince and how? here is the leap that needs to be made to solve this problem. &lt;strong&gt;pirate 3 realizes that if his plan is not adopted he will be executed and they will be left with 2 pirates&lt;/strong&gt;. he already knows what happens when there are 2 pirates as we just figured out. pirate 2 takes all the money himself and gives nothing to pirate 1. so pirate 3 proposes that he will take 99 gold coins and give 1 coin to pirate 1. pirate 1 says, well, 1 is better than none, and since i know if i don&amp;#8217;t vote for pirate 3, i get nothing, i should vote for this plan.&lt;/p&gt;
&lt;p&gt;now we know what happens when there are 3 pirates. so what happens with 4? well pirate 4 has to convince 1 other person to join in his plan. he knows if he walks the plank then pirate 3 will get 99 coins and pirate 1 will get 1 coin. pirate 4 could propose giving pirate 1 two coins, and surely pirate 1 would vote for him, since 2 is better than 1. but as greedy as he is, pirate 4 would rather not part with 2 whole coins. he realizes that if he gets executed, then pirate 3&amp;#8217;s scenario happens and pirate 2 gets the shaft in that scenario (he gets zero coins). so pirate 4 proposes that he will give 1 coin to pirate 2, and pirate 2 seeing that 1 is better than 0 will obviously vote for this plan.&lt;/p&gt;
&lt;p&gt;a common objection is that pirate 2 is not guaranteed to vote for this plan since he might hope for the case when there are only 2 pirates and then he gets all the booty. but that is why i said that the pirates are extremely intelligent. pirate 2 realizes that pirate 3 is smart enough to make the optimal proposal, so he realizes that there will never be 2 pirates left, because 3 doesn&amp;#8217;t want to die and we just showed that 3 has a winning proposal.&lt;/p&gt;
&lt;p&gt;so lets sum up at this point&lt;/p&gt;
&lt;pre&gt;Pirate 1  2  3  4  5&lt;br/&gt;    5. ?  ?  ?  ?  ?&lt;br/&gt;    4. 0  1  0 99  -&lt;br/&gt;    3. 1  0 99  -  -&lt;br/&gt;    2. 0 100 -  -  -&lt;br/&gt;    1.100&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;once you see the pattern it becomes very clear. you have to realize that when a pirate&amp;#8217;s plan does not succeed then that means you are in the same situation with one less pirate.&lt;br/&gt;1. pirate 1 needs 0 other people to vote for him. so he votes for himself and takes all the money. 2. pirate 2 needs 0 other people to vote for him. so he votes for himself and takes all the money. pirate 1 gets 0. 3. pirate 3 needs 1 other person to vote for him. he gives 1 coin to pirate 1 for his vote - if we are reduced to 2 pirates, pirate 1 gets 0 so pirate 1 knows 1 is better than none. pirate 3 takes 99. pirate 2 gets 0. 4. pirate 4 needs 1 other person to vote for him. he gives 1 coin to pirate 2 - if we reduce to 3 pirates, pirate 2 gets 0 so pirate 2 knows 1 is better than none. pirate 4 takes 99. pirate 3 gets 0. pirate 1 gets 0. 5. pirate 5 needs 2 other people to vote for him. its clear now that the 2 people he needs to convince are the 2 who get shafted in the 4 pirate scenario - pirate 3 and pirate 1. so he can give them each 1 coin (which is better than 0 - what they would get otherwise) and keep 98 for himself.&lt;/p&gt;
&lt;pre&gt;Pirate 1  2  3  4  5&lt;br/&gt;    5. 1  0  1  0 98&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;what happens if there are 15 pirates? pirate 15 needs 7 other people to vote for him, so he recruits pirates 13,11,9,7,5,3, and 1 with 1 coin each and keeps 93 coins himself. those pirates will all vote for him because they know that they get 0 coins if he dies and pirate 14 is in charge.&lt;/p&gt;
&lt;p&gt;hope you enjoyed this one. its my favorite interview question of all. it really allows the candidate to ask a lot of interesting questions and its really amazing when they reach the solution all by themselves (as all &lt;a href="http://www.fogcreek.com/"&gt;fogcreek&lt;/a&gt; employees have done so far).&lt;/p&gt;
&lt;p&gt;&lt;br/&gt;               			 		&lt;br/&gt;&lt;/p&gt;</description><link>http://www.techinterview.org/post/526325766</link><guid>http://www.techinterview.org/post/526325766</guid><pubDate>Fri, 16 Apr 2010 15:03:23 -0400</pubDate></item><item><title>Fog Creek Programmers</title><description>&lt;p&gt;100&amp;#160;&lt;a&gt;fogcreek&lt;/a&gt; programmers are lined up in a row by an assassin. the assassin puts red and blue hats on them. they can&amp;#8217;t see their own hats, but they can see the hats of the people in front of them. the assassin starts in the back and says &amp;#8220;what color is your hat?&amp;#8221; the fogcreek programmer can only answer &amp;#8220;red&amp;#8221; or &amp;#8220;blue.&amp;#8221; the programmer is killed if he gives the wrong answer; then the assassin moves on to the next programmer. the programmers in front get to hear the answers of the programmers behind them, but not whether they live or die. they can consult and agree on a strategy before being lined up, but after being lined up and having the hats put on, they can&amp;#8217;t communicate in any way other than those already specified. what strategy should they choose to maximize the number of programmers who are guaranteed to be saved?&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;this is a very difficult problem to solve during an interview (especially if you&amp;#8217;ve already taxed the candidate&amp;#8217;s brain). look for obvious solutions first, and the reasoning behind them and then try to lead them to the ultimate solution.&lt;/p&gt;
&lt;p&gt;a logical answer could be all the programmers would just say &amp;#8220;red&amp;#8221; and that way about half of them would survive on average, assuming the hats were distributed randomly.&lt;/p&gt;
&lt;p&gt;this is a good start and should naturally lead to having every other programmer say the color of the hat in front of them. the first programmer would say the color of the hat in front of him, then the next programmer would just say that color that was just said. so we can guarantee that half survive - the even numbered programmers (since the person behind them told them the answer). and potentially if the hats were distributed randomly some of the programmers would get lucky and the hat in front of them would be the same color as their own. so this strategy should save more than half, and on average 75% of them would live.&lt;/p&gt;
&lt;p&gt;at this point, if the solution is not clear, the candidate may give answers like, &amp;#8220;they could agree that if they said their hat color in a soft voice, it means the hat in front of them is the same color, and if they say it in a loud voice, it means the hat in front is a different color&amp;#8221;. this is definitely good and on the correct track. another option is they could say &amp;#8220;reeeeeeeeeeed&amp;#8221; for x number of seconds, where x represented the distribution of hats where a hat was a bit in a binary number, (red = 1, blue = 0). another interesting answer. there are many others like these that &amp;#8220;bend&amp;#8221; the rules and come to a solution.&lt;/p&gt;
&lt;p&gt;but the real solution acknowledges that the programmers can only say &amp;#8220;red&amp;#8221; or &amp;#8220;blue&amp;#8221; and cannot alter their voice in such a convincing way as to signal any information other than the word they said. a good way to get this point across, is simply to change the problem slightly by saying &amp;#8220;the assassin gets to hear their plan before she puts the hats on, and so will try to thwart the plan however she can.&amp;#8221;&lt;/p&gt;
&lt;p&gt;so if they decide to all say &amp;#8220;red&amp;#8221;, she&amp;#8217;ll put blue hats on all of them. if they decide to all say the color of the hat in front of them, she&amp;#8217;ll alternate the hats on every head, guaranteeing half will die. even with the assassin hearing their plan, there is still a way to save almost everyone.&lt;/p&gt;
&lt;p&gt;we know that the first person is never going to have any information about the color of their hat, so they cannot be guaranteed to survive. but, i&amp;#8217;ll give you a hint to the solution: i can save every other person for sure.&lt;/p&gt;
&lt;p&gt;solution: they agree that if the number of red hats that the back person can see is even, that programmer will say &amp;#8220;red&amp;#8221;. if they add up to an odd number, they will say &amp;#8220;blue&amp;#8221;. this way number 99 can look ahead and count the red hats. if they add up to an even number and number 100 said &amp;#8220;red&amp;#8221;, then 99 must be wearing a blue hat. if they add up to an even number and number 100 said &amp;#8220;blue&amp;#8221;, signalling an odd number of red hats, number 99 must also be wearing a red hat. number 98 knows that 99 said the correct hat, and so uses that information along with the 97 hats in front to figure out what color hat is on 98&amp;#8217;s head.&lt;/p&gt;
&lt;p&gt;sample:&lt;/p&gt;
&lt;pre&gt;100  99  98  97  96  95  94 ... facing -&amp;gt;&lt;br/&gt; R    B   B   R   B   R   B ... -&amp;gt; 45 R and 48 B&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;this shows #100 wearing a red hat, 99 a blue, 98 a blue, 97 a red, 96 a blue, 95 a red, 94 a blue and 45 red hats - 48 blue hats on the people in front of them.&lt;/p&gt;
&lt;p&gt;100 counts up the red hats: 47 total. so 100 says &amp;#8220;blue&amp;#8221;. the assassin kills 100. 99 counts up the red hats in front: 47. 100 said blue, so 100 saw an odd number. 99 sees an odd number, so 99 says &amp;#8220;blue&amp;#8221; and lives. 98 had counted 47 red hats, and 99 didn&amp;#8217;t say &amp;#8220;red&amp;#8221; so thats still the total. 98 says &amp;#8220;blue&amp;#8221;. 97 counts up and finds 46 red hats. 99 and 98 didn&amp;#8217;t say &amp;#8220;red&amp;#8221;, so his count is missing a red hat (its on his head, he realizes). he says &amp;#8220;red&amp;#8221;. 96 heard the &amp;#8220;red&amp;#8221; and now knows that there are an even number of &amp;#8220;red&amp;#8221; hats in front of 95. 96 sees 46, so he knows he has a &amp;#8220;blue&amp;#8221; hat. etc&amp;#8230;&lt;/p&gt;
&lt;p&gt;even if the assassin knows the plan, she can&amp;#8217;t thwart it. she hears the plan, but she still has to put the hats on their heads. the plan doesn&amp;#8217;t rely on any ordering of the hats, so the worst the assassin can do is to make sure #100 gets killed and thats the worst damage she can do.&lt;/p&gt;</description><link>http://www.techinterview.org/post/526321110</link><guid>http://www.techinterview.org/post/526321110</guid><pubDate>Fri, 16 Apr 2010 15:00:40 -0400</pubDate></item><item><title>Bad King</title><description>&lt;p&gt;A bad king has a cellar of 1000 bottles of delightful and very expensive wine. A neighbouring queen plots to kill the bad king and sends a servant to poison the wine. (un)fortunately the bad king&amp;#8217;s guards catch the servant after he has only poisoned one bottle. Alas, the guards don&amp;#8217;t know which bottle but know that the poison is so strong that even if diluted 1,000,000 times it would still kill the king. furthermore, it takes one month to have an effect. The bad king decides he will get some of the prisoners in his vast dungeons to drink the wine. Being a clever bad king he knows he needs to murder no more than 10 prisoners - believing he can fob off such a low death rate - and will still be able to drink the rest of the wine at his anniversary party in 5 weeks time.&lt;/p&gt;
&lt;p&gt;explain how&amp;#8230;.&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;solution: i&amp;#8217;ll give you a hint. 1000 is less than 1024. if there were 1024 or more bottles of wine it would take more than 10 prisoners.&lt;/p&gt;
&lt;p&gt;number the bottles 1 to 1000, and write the number in binary format.&lt;/p&gt;
&lt;pre&gt;bottle 1    = 0000000001&lt;br/&gt;bottle 250  = 0011111010&lt;br/&gt;bottle 1000 = 1111101000&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;now take your prisoner&amp;#8217;s 1 through 10 and let prisoner 1 take a sip from every bottle that has a 1 in its least significant bit. let prisoner 10 take a sip from every bottle with a 1 in its most significant bit. etc.&lt;/p&gt;
&lt;pre&gt;prisoner    10 9 8 7 6 5 4 3 2 1&lt;br/&gt;bottle 924   1 1 1 0 0 1 1 1 0 0&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;for instance, bottle #924 would be sipped by 10,9,8,5,4 and 3. that way if bottle #924 was the poisoned one, only those prisoners would die.&lt;/p&gt;
&lt;p&gt;after four weeks, line the prisoners up in their bit order and read each living prisoner as a 0 bit and each dead prisoner as a 1 bit. the number that you get is the bottle of wine that was poisoned.&lt;/p&gt;
&lt;p&gt;additional question: to increase your chance of living, which prisoner would you want to be?&lt;/p&gt;
&lt;p&gt;if there were 1023 bottles, it wouldn&amp;#8217;t matter since everyone would have to take 512 sips. but there are 23 bottles less, so the people whose bits would have been on from 1001 to 1023 won&amp;#8217;t have to take a sip. 1001 is [11111&amp;#160;01001] in binary and 1023 is [11111&amp;#160;11111]. the most five significant bits are the most interesting because they would always be on from 1001 to 1023, so all those people are missing out on 23 bottles of wine that they otherwise would have had to drink. so in order to increase your chance of living, you&amp;#8217;d probably want to be prisoner 6 to 10. (but depending on how the king determines who is least significant and who is most significant you could get shafted.)&lt;/p&gt;
&lt;p&gt;note that if the king was really trying to kill the least number of prisoners, he should have let 999 prisoners each take a sip from their respective bottle numerically (if he had that many prisoners at his disposal). that way only one prisoner would die, and there&amp;#8217;s a chance of 1/1000 that no one would die, but then the puzzle isn&amp;#8217;t very fun&lt;/p&gt;</description><link>http://www.techinterview.org/post/526313890</link><guid>http://www.techinterview.org/post/526313890</guid><pubDate>Fri, 16 Apr 2010 14:56:21 -0400</pubDate></item><item><title>Jelly Beans</title><description>&lt;p&gt;You have three jars that are all mislabeled. one contains peanut butter jelly beans, another grape jelly jelly beans, and the third has a mix of both (not necessarily a 50/50 mix, could be a 1/99 mix or a 399/22 mix). how many jelly beans would you have to pull out, and out of which jars, to find out how to fix the labels on the jars?&lt;/p&gt;
&lt;pre&gt;|     |        |     |          |     |&lt;br/&gt;|jar 1|        |jar 2|          |jar 3|&lt;br/&gt;|     |        |     |          |     |&lt;br/&gt;=======        =======          =======&lt;br/&gt;  p.b.          grape          p.b./grape&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;thanks to joel wollman&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;You have three jars that are all mislabeled. one contains peanut butter jelly beans, another grape jelly jelly beans, and the third has a mix of both (not necessarily a 50/50 mix, could be a 1/99 mix or a 399/22 mix). how many jelly beans would you have to pull out, and out of which jars, to find out how to fix the labels on the jars?&lt;/p&gt;
&lt;pre&gt;   |     |                  |     |                 |     |&lt;br/&gt;  |jar 1|                |jar 2|               |jar 3|&lt;br/&gt;   |     |                  |     |                 |     |&lt;br/&gt;=======        =======          =======&lt;br/&gt;     p.b.                   grape             p.b./grape&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;solution: 1 jelly bean from the p.b./grape jar will do the trick.&lt;/p&gt;
&lt;p&gt;the trick here is to realize that every jar is mislabeled. therefore you know that the peanut butter jelly bean jar is not the penut butter jelly bean jar, and the same goes for the rest.&lt;/p&gt;
&lt;p&gt;you also need to realize that it is the jar labeled p.b./grape, labelled as the mix jar, that is your best hope. if you choose a jelly bean out of there, then you will know whether that jar is peanut butter or grape jelly jelly beans. it can&amp;#8217;t be the mix jar because i already said that every jar is mislabeled.&lt;/p&gt;
&lt;p&gt;once you know that jar 3 is either peanut butter, or grape jelly, then you know the other jars also. if it is peanut butter, then jar 2 must be mixed because it can&amp;#8217;t be grape (as its labelled) and it can&amp;#8217;t be peanut butter (that&amp;#8217;s jar 3). hence jar 1 is grape.&lt;/p&gt;
&lt;p&gt;if jar 3 is grape, then you know jar 1 must be the mix because it can&amp;#8217;t be p.b. (as its labelled) and it can&amp;#8217;t be grape (that&amp;#8217;s jar 3). hence jar 2 is peanut butter.&lt;/p&gt;
&lt;p&gt;if you pick jelly beans from jar 1 or jar 2, then you would have to pick out &lt;strong&gt;all&lt;/strong&gt; of the jelly beans before you knew what that jar was. this is because jar 1 and 2 could be the mix, so in order to disprove that they were the mix, you would have to pull out &lt;strong&gt;every&lt;/strong&gt; jelly bean just to make sure (since there could just be one bean of the opposite flavor in there)&lt;/p&gt;</description><link>http://www.techinterview.org/post/526310513</link><guid>http://www.techinterview.org/post/526310513</guid><pubDate>Fri, 16 Apr 2010 14:54:15 -0400</pubDate></item><item><title>Bridge</title><description>&lt;p&gt;Problem: this one is a classic that many of you have probably already heard, but all the more reason why it should definitely be included here. four people are on this side of the bridge. the bridge will be destroyed by a bomb in 17 minutes. everyone has to get across before that. problem is that it&amp;#8217;s dark and so you can&amp;#8217;t cross the bridge without a flashlight, and they only have one flashlight. plus the bridge is only big enough for two people to cross at once. the four people walk at different speeds: one fella is so fast it only takes him 1 minute to cross the bridge, another 2 minutes, a third 5 minutes, the last it takes 10 minutes to cross the bridge. when two people cross the bridge together (sharing the flashlight), they both walk at the slower person&amp;#8217;s pace. can they all get across before the bridge blows up?&lt;/p&gt;
&lt;pre&gt;person A: 1 minute&lt;br/&gt;person B: 2 minutes&lt;br/&gt;person C: 5 minutes&lt;br/&gt;person D:10 minutes&lt;br/&gt;&lt;br/&gt;&lt;!-- more --&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;/pre&gt;
&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;Of course its possible, otherwise it wouldn&amp;#8217;t be a very interesting question. the only trick is in realizing that you want to get the two slowest people across together, because otherwise you are wasting too much time. but then once you get them across, how do you not make one of them walk back with the flashlight? well, you just have one of the fast people already there waiting to sprint the flashlight back across.&lt;/p&gt;
&lt;p&gt;1. A &amp;amp; B cross. total time: 2 minutes.&lt;/p&gt;
&lt;p&gt;C |==========================| A  D |                          | B    |==========================| flashlight   2. B comes back. total time:  4 minutes.    C |==========================| A  D |                          |   B |==========================|  flashlight   3. C &amp;amp; D cross.  total time: 14 minutes.    B |==========================| A    |                          | C    |==========================| D                                flashlight   4. A comes back. total time: 15 minutes.    A |==========================| C  B |                          | D    |==========================|  flashlight   5. A &amp;amp; B cross.  total time: 17 minutes.      |=========    =============| A    |        KABOOM!           | B    |=========    =============| C D                               flashlight  &lt;/p&gt;
&lt;p&gt;another solution which is valid is to have A bring the flashlight back in step 2. it only changes the solution slightly. this is supposed to be a &amp;#8220;classic&amp;#8221; microsoft interview question but it seems absurdly easy to be a good interview question (especially coupled with the fact that everyone has probably heard it already).&lt;/p&gt;</description><link>http://www.techinterview.org/post/526304828</link><guid>http://www.techinterview.org/post/526304828</guid><pubDate>Fri, 16 Apr 2010 14:51:03 -0400</pubDate></item><item><title>Card Trick Without the Trick</title><description>&lt;p&gt;This is a card trick without the trick. there is no sleight of hand, no tricks up my sleeve, no magic whatsoever. it&amp;#8217;s all done with logic, yet it will amaze most people.&lt;/p&gt;
&lt;p&gt;&lt;a&gt;Joel&lt;/a&gt; and i are working together as a team to do the trick. &lt;a&gt;Babak&lt;/a&gt; will be the culprit.&lt;/p&gt;
&lt;p&gt;i ask babak to pick 5 cards out of a deck. he can pick any five cards, he can shuffle the deck 7 times, it really doesn&amp;#8217;t matter. he honestly picks out 5 cards that i cannot see. he hands the five cards to me (joel can&amp;#8217;t see any of this). i look at the cards and i pick 1 card out and give it back to babak. i then arrange the other four cards in a special way, and give those 4 cards all face down, and in a neat pile, to joel. joel looks at the 4 cards i gave him, and says out loud which card babak is holding (suit and number).&lt;/p&gt;
&lt;p&gt;i did not convey any information to joel other than the way i ordered the 4 cards, (all face down, aligned in line with one another) so how did i encode babak&amp;#8217;s card using that method?&lt;/p&gt;
&lt;p&gt;&lt;a&gt;hint 1: card trick without the trick&lt;/a&gt;&lt;br/&gt;&lt;a&gt;hint 2: card trick without the trick&lt;/a&gt;&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;hint 1 talks about how there are guaranteed to be at least 2 cards with the same suit in a series of five cards. i use this to signal to joel the suit of the card. i have to make a decision though between which of the two cards to give back to babak. which one do i pick?&lt;/p&gt;
&lt;p&gt;hint 2 talks about how if i use one of the cards to signal the suit, that only leaves me with 3 cards. i can only arrange 3 cards in six permutations. how do i signal a number between 2 and 13 using only six permutations?&lt;/p&gt;
&lt;p&gt;simple really. consider that the series of cards is a cycle.&lt;/p&gt;
&lt;pre&gt;2 3 4 5 6 7 8 9 10 J Q K A 2 3 4 5 6 7 8 9 10 J Q K A 2 ... etc&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;i take the two cards of the same suit (if there are more than two of the same suit, then i choose any two, it won&amp;#8217;t affect the solution) and i pass babak the one that is further to the right in the cycle. if i do it correctly, the one further to the right will be no more than 6 places away than the one to the left. (it just depends on how you think about the cycle).&lt;/p&gt;
&lt;p&gt;for example. say we had the 2 and the 9 of hearts. if you look at it as the 2 to the left and the 9 to the right, it is seven cards away. but if you look as the 9 to the left and the 2 to the right in the cycle, the 2 is only 6 cards away.&lt;/p&gt;
&lt;p&gt;then i just come up with a system of the permutations that signals how many places to the right to count to find the correct card&lt;/p&gt;
&lt;p&gt;one such system could be:&lt;br/&gt;sort the 3 non-same suit cards first by number then by suit (diamonds, spades, hearts, clubs) to give a unique sort for every card.&lt;br/&gt;label the cards as A, B, and C.&lt;br/&gt;place the cards in the following order to signal how many places away babak&amp;#8217;s card is:&lt;/p&gt;
&lt;pre&gt;A B C - 1&lt;br/&gt;A C B - 2&lt;br/&gt;B A C - 3&lt;br/&gt;B C A - 4&lt;br/&gt;C A B - 5&lt;br/&gt;C B A - 6&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;then put the suit card on top and give the pile to joel.&lt;/p&gt;
&lt;p&gt;example:&lt;br/&gt;babak picks out the following five cards:&lt;/p&gt;
&lt;pre&gt;3C 7D 10S QH AD&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;the 7 of diamonds (7D) and the ace of diamonds (AD) are the two suit cards. i recognize that AD is going to be the left card and 7D the right (because the other way they are 7 spaces apart). i give babak back the 7D. i sort the other cards, 3C 10S QH. i have to signal 6 places to the right (7D - AD = 6). so i place them in C B A format: QH 10S 3C. i then place the AD on top and pass to joel.&lt;/p&gt;
&lt;p&gt;joel pulls off the first card and sees that it is the AD. (he knows babak&amp;#8217;s card is a diamond). he looks at the next 3 cards and realizes their ordering is C B A. signalling 6, he counts 6 spots from the ace. (2&amp;#160;3 4&amp;#160;5 6&amp;#160;7). then he says &amp;#8220;7 of diamonds&amp;#8221; and the crowd is amazed.&lt;/p&gt;
&lt;p&gt;another &lt;strong&gt;sneakier card trick&lt;/strong&gt; (with the trick) works as follows.&lt;/p&gt;
&lt;p&gt;babak picks one card out of the deck and shows it to me. he doesn&amp;#8217;t tell joel and i don&amp;#8217;t speak. after seeing the card, i sneakily use my one hand to signal both the number and suit of the card. i use my four fingers (not the thumb) as bits signalling a binary number. if they are extended it means 1, if i don&amp;#8217;t extend them it means 0. the pinky is the least significant. so to signal 7 i would extend all fingers except my index finger. the suit is signalled by four positions of the thumb (hidden, adjacent to the hand, slightly ajar, fully extended). i can use this method to tell joel the card without saying anything (and making any noticeable motions). with some practice it can really amaze people (at least it worked with my family).&lt;/p&gt;</description><link>http://www.techinterview.org/post/526296398</link><guid>http://www.techinterview.org/post/526296398</guid><pubDate>Fri, 16 Apr 2010 14:46:12 -0400</pubDate></item><item><title>Pirates Revisited</title><description>&lt;p&gt;A slightly different version of the original &lt;a&gt;pirates&lt;/a&gt; problem (read that one first to get all the rules). 6 pirates, only one gold coin. as before, the pirates are super-smart, and they value, in this order: (i) their lives, (ii) getting money, (iii) seeing other pirates die. so if given the choice between two outcomes, in which they get the same amount of money, they&amp;#8217;d choose the outcome where they get to see more of the other pirates die. how can pirate 6 save his skin?&lt;/p&gt;
&lt;p&gt;thanks to my super smart &lt;a&gt;professor brother&lt;/a&gt;&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;1 pirate case is obvious. 2 pirate case is obvious, pirate 2 keeps the coin.&lt;/p&gt;
&lt;p&gt;3 pirate case: pirate 3 has to give the coin to pirate 1, because if he gives it pirate 2 pirate 2 will say &amp;#8220;screw that i wanna see you die and i&amp;#8217;m going to get the coin anyway.&amp;#8221; so in 3 pirate case, we have pirate 1 gets 1 coin, pirates 2 and 3 get 0.&lt;/p&gt;
&lt;p&gt;4 pirate case: pirate 4 can&amp;#8217;t give the coin to pirate 1, because pirate 1 would rather see him die since he&amp;#8217;s going to get 1 coin anyway. But pirate 4 could give the coin to either pirate 2 or pirate 3.&lt;/p&gt;
&lt;p&gt;5 pirate case: pirate 5 just dies, and it goes to the 4 pirate case. there is no way for him to convince two people to vote for him.&lt;/p&gt;
&lt;p&gt;6 pirate case: pirate 6 can count on his own vote, plus pirate 5&amp;#8217;s vote, because 5 won&amp;#8217;t want to die. now who should he give the coin to? he could give it to pirate 1 or to pirate 4, since in the 4 pirate case they are guaranteed to get nothing. it&amp;#8217;s unclear whether he could give it to pirate 2 or 3. neither pirate 2 nor pirate 3 is guaranteed to get a coin in the 4 pirate case. so the question is, how do they value (i) definitely getting a coin from pirate 6 vs. (ii) definitely seeing pirates 6 and 5 die, with the chance of getting a coin from pirate 4. since we don&amp;#8217;t have enough information to answer that, to be safe, i would just say pirate 6 should offer the coin to pirate 1 or pirate 4.&lt;/p&gt;</description><link>http://www.techinterview.org/post/526292796</link><guid>http://www.techinterview.org/post/526292796</guid><pubDate>Fri, 16 Apr 2010 14:44:07 -0400</pubDate></item><item><title>Fuse on Fire</title><description>&lt;p&gt;A mad bomber is out on the job, making bombs. he has two fuses (pieces of string) of varying thickness which each burn for 30 seconds. unfortunately he wants this bomb to go off in 45 seconds. he can&amp;#8217;t cut the one fuse in half because the fuses are different thicknesses and he can&amp;#8217;t be sure how long it will burn. how can he arrange the fuses to make his bomb go off at the right time?&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;light both ends of one of the fuses. when that fuse goes out, 15 seconds has elapsed. then light the other fuse.&lt;/p&gt;</description><link>http://www.techinterview.org/post/526289631</link><guid>http://www.techinterview.org/post/526289631</guid><pubDate>Fri, 16 Apr 2010 14:41:57 -0400</pubDate></item><item><title>Dave's on Fire</title><description>&lt;p&gt;&lt;a&gt;dave winer&lt;/a&gt; is stuck on a deserted island, with lots of trees, which is very thin and ten miles long (east to west). large cliffs surround the entire island and if he jumped off, he wouldn&amp;#8217;t survive the fall. a fire starts burning at the west side of the island. unfortunately this island always has a west to east blowing wind blowing at 2mph and this moves the fire slowly toward dave at 1mph. (so he only has ten hours left). save dave (or maybe, let him burn :-)&amp;#160;! what to do?&lt;/p&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_l0zfsmMcQm1qz9fkg.png"/&gt;&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;someone suggested he could dig a pit across the island to act as a firebreak. good suggestion, if he had a shovel and the ground wasn&amp;#8217;t too hard.&lt;/p&gt;
&lt;p&gt;but even if he didn&amp;#8217;t have a shovel, he could pick up a branch and run up to the fire and light the branch. then run all the way to the eastern edge of the island, but stop about a mile short. there he could light all those trees on fire and they would start burning and the fire would move east. it would consume all that vegetation in an hour, and then dave could wait for awhile for that part to cool down some. when the initial fire reached him, he could just run into the already burnt part, and the fire couldn&amp;#8217;t get him.&lt;/p&gt;
&lt;pre&gt;T = tree&lt;br/&gt;D = dave&lt;br/&gt;B = burnt&lt;br/&gt; &lt;br/&gt;top view&lt;br/&gt;==========================&lt;br/&gt;| fire-&amp;gt; T T T T T T T D |&lt;br/&gt;==========================&lt;br/&gt; &lt;br/&gt;dave gets branch and lights it from fire&lt;br/&gt;==========================&lt;br/&gt;| fire-&amp;gt; D T T T T T T T |&lt;br/&gt;==========================&lt;br/&gt; &lt;br/&gt;dave lights trees farther east on the island&lt;br/&gt;==========================&lt;br/&gt;| fire-&amp;gt; T T T T D fire-&amp;gt;|&lt;br/&gt;==========================&lt;br/&gt; &lt;br/&gt;dave waits until second fire cools, and then hides out there&lt;br/&gt;==========================&lt;br/&gt;| B B B fire-&amp;gt; T T B B D |&lt;br/&gt;==========================&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;dave is saved!&lt;/p&gt;</description><link>http://www.techinterview.org/post/526285638</link><guid>http://www.techinterview.org/post/526285638</guid><pubDate>Fri, 16 Apr 2010 14:39:21 -0400</pubDate></item></channel></rss>
