Tuesday, 12 May 2015

Puzzle - 2 Eggs and a 100 storey building - Answer

You would be starting the thought process by thinking of a binary search, i.e, drop the first egg from 50th floor, if it does not break, then go to 25th floor etc. But if the first egg breaks on 50th floor, then you have to drop the second egg from 1 to 49th floor to figure out the answer.

Then you may be thinking of dropping the first egg at 25th floor, then at 50th, 75th etc, this will take 28 tries if the answer is 99th floor.

There is definitely a better answer.

The optimal number of drops would be such that if you do the first drop from nth floor, if the attempt fails, then do the drop by climbing (n-1) more floors, if it fails again, then do the drop after climbing (n-2) floors etc till (n-x) = 1. At this point you should have covered all the floors.
If the drop at any of these point breaks the egg, then start dropping from the last floor at which the breakage did not happen with the second egg and move up. In this way, n drops would be what you require the maximum to figure out the breaking point in any scenario.

So, in our case, to cover up 100 floors:

n + (n-1) + (n-2) + (n-3) + ......+ 2 + 1 >= 100

i.e, n*(n+1)/2 >= 100

=> n = 14

So, 14 is the answer.

one example sequence - 14, 27, 39, 50, 60, 69, 77, 84, 90, 95, 99 (let's say it broke at 99), 96, 97, 98 => 14 iterations.

No comments:

Post a Comment