<p>One of those classic programming interview questions...</p>

<p>You are given two marbles, and told that they will break when dropped from some certain height (and presumably suffer no damage if dropped from below that height). You’re then taken to a 100 story building (presumably higher than the certain height), and asked to find the highest floor your can drop a marble from without breaking it as efficiently as possible.</p>

<p>Extra info</p>

<ul>
<li>You must find the correct floor (not a possible range)</li>
<li>The marbles are both guaranteed to break at the same floor</li>
<li>Assume it takes zero time for you to change floors - only the number of marble drops counts</li>
<li>Assume the correct floor is randomly distributed in the building</li>
</ul>

[{'answer_id': 6553, 'author': 'Justin Bennett', 'author_id': 271, 'author_profile': 'https://Stackoverflow.com/users/271', 'pm_score': 2, 'selected': False, 'text': "<p>They each break when dropped from the same height, or are they different?</p>\n\n<p>If they're the same, I go to the 50th floor and drop the first marble.  If it doesn't break, I go to the 75th floor and do the same, as long as it keeps not breaking I keep going up by 50% of what's left.  When it does break, I go back to one higher than where I was previously (so if it broke at the 75th floor I go back to the 51st floor) and drop the second marble and move up a floor at a time until it breaks, at which point I know the highest floor I can drop from with no marble breakage.</p>\n\n<p>Probably not the best answer, I'm curious to see how others answer.</p>\n"}, {'answer_id': 6558, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 1, 'selected': False, 'text': "<p>I'm personally not very big a fan of such puzzle questions, I prefer actual programming exercises in interviews.</p>\n\n<p>That said, first it would depend on if I can tell if they are broken or not from the floor I am dropping them at.  I will presume I can.</p>\n\n<p>I would go up to the second floor, drop the first marble.  If it broke I would try the first floor.  If that broke I would know it was no floor.</p>\n\n<p>If the first didn't break, I would go to the 4th floor and drop from there.  If that broke, I would go back down and get the other marble, then drop at the 3rd floor, breaking or not I would know which is the limit.</p>\n\n<p>If neither broke, I would go get both, and do the same process, this time starting at the 6th floor.</p>\n\n<p>This way, I can skip every other floor until I get a marble that breaks.</p>\n\n<p>This would be optimized for if the marble breaks early... I suppose there is probably an optimal amount of floors I could skip to get the most for each skip... but then if one breaks, I would have to check each floor individually from the first floor above the last known floor... which of course would be a pain if I skipped too many floors (sorry, not going to figure out the optimal solution right now).</p>\n\n<p>Ideally, I would want a whole bag of marbles, then I could use a binary search algorithm and divide the number of floors in half with each drop... but then, that wasn't the question, was it?</p>\n"}, {'answer_id': 6560, 'author': 'Joseph Pecoraro', 'author_id': 792, 'author_profile': 'https://Stackoverflow.com/users/792', 'pm_score': 2, 'selected': False, 'text': '<p>I think the <em>real</em> question is how accurate do you want the answer.  Because your efficiency is going to really depend on that.</p>\n\n<p>I\'m going to agree with Justin if you want 100% accuracy on the marbles then once the first marble breaks your going to have to go up 1 floor at a time from the last known "good" floor until you find out which floor is the "winner."  Maybe even throw in some statistics and start at the 25th floor instead of the 50th floor so that you\'re worst case scenario would be 24 instead of 49.</p>\n\n<p>If you\'re answer can be plus or minus a floor or two then there could be some optimizations.</p>\n\n<p>Secondly, does walking up/down the stairs count against your efficiency?  In that case always drop both marbles and pick up both marbles on every up/down trip.</p>\n'}, {'answer_id': 6565, 'author': 'mreggen', 'author_id': 613, 'author_profile': 'https://Stackoverflow.com/users/613', 'pm_score': 7, 'selected': True, 'text': "<p>The interesting thing here is how you can do it in the least amount of drops possible. Going to the 50th floor and dropping the first would be disastrous if the breaking floor is the 49th, resulting in us having to do 50 drops. We should drop the first marble at floor n, where n is the max amount of drops required. If the marble breaks at floor n, we may have to make n-1 drops after that. If the marble doesn't break we go up to floor 2n-1 and if it breaks here we have to drop the second marble n-2 times in the worst case. We continue like this up to the 100th floor and try to break it at 3n-2, 4n-3....<br>\nand n+(n-1)+(n-2)+...1 &lt;=100<br>\n<strong>n=14 Is the maximum drops required</strong></p>\n"}, {'answer_id': 6665, 'author': 'Lance Fisher', 'author_id': 571, 'author_profile': 'https://Stackoverflow.com/users/571', 'pm_score': 1, 'selected': False, 'text': '<p>Drop the first marble from the 3rd floor.  If it breaks, you know it\'s floor 1 or 2, so drop the other marble from floor 2.  If it doesn\'t break you\'ve found that floor 2 is the highest.  If it does break, you\'ve found that floor 1 is the highest.  2 drops.</p>\n\n<p>If dropping from the 3rd floor does not break the marble, drop from floor 6.  If it breaks, you know floor 4 or 5 is the highest.  Drop the second marble from floor 5.  If it doesn\'t break you\'ve found that 5 is the highest.  If it does, floor 4 is the highest.  4 drops.</p>\n\n<p>Continue.</p>\n\n<p>3 floors - maximum of 2 drops</p>\n\n<p>6 floors - maximum of 4 drops</p>\n\n<p>9 floors - maximum of 6 drops</p>\n\n<p>12 floors - maximum of 8 drops</p>\n\n<p>etc.</p>\n\n<p>3x floors - maximum of 2x drops</p>\n\n<p>So for a 99 floor building you\'d have a maximum of 66 drops.  And that is the maximum.  You\'d likely have less drops than that.  Oh, and 66 is the maximum for a 100 story building too.  You\'d only need 66 drops if the break floor was floor 98 or 97.  If the break floor was 100 you\'d use 34 drops.</p>\n\n<p>Even though you said it didn\'t matter, this would probably require the least amount of walking and you don\'t have to know how high the building is.</p>\n\n<p>Part of the problem is how you define efficiency.  Is it more "efficient" to always have a solution in less than x drops, or is it it more efficient to have a good chance at having a solution in y drops where y &lt; x with the caveat that you could have more than x drops?</p>\n'}, {'answer_id': 6795, 'author': 'Mike Minutillo', 'author_id': 358, 'author_profile': 'https://Stackoverflow.com/users/358', 'pm_score': 2, 'selected': False, 'text': '<p>Drop the first marble at floor 10, 20, 30, etc. until it breaks then jump back to the last known <em>good</em> floor and start dropping marbles from there one floor at a time. Worst case is 99 being the Magic Floor and you can always find it in 19 drops or less.</p>\n'}, {'answer_id': 134089, 'author': 'Kevin Gale', 'author_id': 7176, 'author_profile': 'https://Stackoverflow.com/users/7176', 'pm_score': -1, 'selected': False, 'text': "<p>First thing I would do is use the dead simple algorithm that starts at floor 1 drops the marble one floor at a time until it reaches 100 or the marble breaks.</p>\n\n<p>Then I'd ask why should I spend time optimizing it until somone can show that it will be a problem. Too many times people get all hung up on finding the perfect complicated algorithm when a much simpler one will solve the problem. In other words don't optimize things until it's needed. </p>\n\n<p>This might be trick question to see if you are one of those people who can make a mountain out of a mole hill.</p>\n"}, {'answer_id': 21133595, 'author': 'herohuyongtao', 'author_id': 2589776, 'author_profile': 'https://Stackoverflow.com/users/2589776', 'pm_score': 4, 'selected': False, 'text': '<p>This problem is covered in Problem 6.5 from Book "<a href="https://rads.stackoverflow.com/amzn/click/com/098478280X" rel="noreferrer" rel="nofollow noreferrer">Cracking the Coding Interview (5th)</a>", with solutions summarized as follows:</p>\n\n<h2>Observation:</h2>\n\n<p>Regardless of how we drop Marble1, Marble2 must do a linear search. Eg, if the Marble1 \nbreaks between floor 10 and 15, we have to check every floor in between with the Marble2</p>\n\n<hr>\n\n<h2>The Approach:</h2>\n\n<p><strong>A First Try:</strong> Suppose we drop an Marble from the 10th floor, then the 20th, …</p>\n\n<ul>\n<li>In the first Marble breaks on the first drop (Floor 10), then we have at most 10 drops total.</li>\n<li>If the first Marble breaks on the last drop (Floor 100), then we have at most 19 drops total \n(floors 1 through 100, then 91 through 99).</li>\n<li>That’s pretty good, but all we’re considered about is the absolute worst case. We should \ndo some “load balancing” to make those two cases more even.</li>\n</ul>\n\n<p><strong>Goal:</strong>  Create  a  system  for  dropping  Marble1  so  that  the  most  drops  required  is  consistent, whether Marble1 breaks on the first drop or the last drop.</p>\n\n<ol>\n<li>A perfectly load balanced system would be one in which Drops of Marble1 + Drops of \nMarble2 is always the same, regardless of where Marble1 broke.</li>\n<li>For that to be the case, since each drop of Marble1 takes one more step, Marble2 is allowed \none fewer step.</li>\n<li>We must, therefore, reduce the number of steps potentially required by Marble2 by one \ndrop each time. For example, if Marble1 is dropped on Floor 20 and then Floor 30, Marble2 is \npotentially required to take 9 steps. When we drop Marble1 again, we must reduce potential Marble2 steps to only 8. eg, we must drop Marble1 at floor 39.</li>\n<li>We know, therefore, Marble1 must start at Floor X, then go up by X-1 floors, then X-2, …, \nuntil it gets to 100.</li>\n<li>Solve for X+(X-1)+(X-2)+…+1 = 100. X(X+1)/2 = 100 -> X = 14</li>\n</ol>\n\n<p><strong>We go to Floor 14, then 27, then 39, … This takes 14 steps maximum.</strong></p>\n\n<hr>\n\n<h2>Code &amp; Extension:</h2>\n\n<ul>\n<li><p>For code implementation, you can check out <a href="https://stackoverflow.com/a/14919702/2589776">here</a>.</p></li>\n<li><p>For the extension to <code>N</code> marbles, <code>M</code> floors, check out <a href="http://www.usvishakh.net/umesh/p2.pdf" rel="noreferrer">Chapter 12: The puzzle of eggs and floors</a> .</p></li>\n</ul>\n'}, {'answer_id': 24851500, 'author': 'Shuhong Wong', 'author_id': 3858039, 'author_profile': 'https://Stackoverflow.com/users/3858039', 'pm_score': -1, 'selected': False, 'text': "<p>This can be done better with just 7 marbles.</p>\n\n<p>So following the voted answer, say marble breaks at at least 49th floor.</p>\n\n<ol>\n<li>50th floor -> breaks (answer is between 1 to 50 inclusive)</li>\n<li>25th floor -> doesn't break (26 to 50)</li>\n<li>37th floor -> doesn't break (38 to 50)</li>\n<li>43rd floor -> doesn't break (44 to 50)</li>\n<li>46th floor -> doesn't break (47 to 50)</li>\n<li>48th floor -> doesn't break (49 or 50)</li>\n<li>49th floor -> breaks (49th, this step is actually needed because it might have been the min floor for marble to break is at 50th)</li>\n</ol>\n\n<p>This can be imagined as doing a binary search in a sorted set for some k, where we half the solution space with each try. For 100 floors, we need log2 100 = 6.644 (7 tries). With 7 marbles we can be sure which is the minimum floor that marble will break up to 128 storeys.</p>\n"}, {'answer_id': 55375368, 'author': 'matt525252', 'author_id': 3249293, 'author_profile': 'https://Stackoverflow.com/users/3249293', 'pm_score': 1, 'selected': False, 'text': '<p>If you want a general solution which will give you the result for N floors (in your case N=100) then you can just solve quadratic equation <a href="https://chart.googleapis.com/chart?cht=tx&amp;chl=x%5E2%2Bx-2%5Ccdot(N-1)%3D0" rel="nofollow noreferrer">$x^2+x-2\\cdot(N-1)=0$</a> and the result is a <a href="https://en.wikipedia.org/wiki/Floor_and_ceiling_functions" rel="nofollow noreferrer">ceiling</a> of a positive root.</p>\n\n<p>Which is:</p>\n\n<p><a href="https://chart.googleapis.com/chart?cht=tx&amp;chl=f(N)%3Dceiling%5Cleft(%5Cfrac%7B-1%2B%5Csqrt%7B1%2B4%5Ccdot2%5Ccdot(N-1))%7D%7D%7B2%7D%5Cright)" rel="nofollow noreferrer">$$f(N)=ceiling\\bigg(\\frac{-1+\\sqrt{1+4\\cdot2\\cdot(N-1))}}{2}\\bigg)$$</a></p>\n'}]
['https://Stackoverflow.com/questions/6547', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/797/']