Egg Drop Puzzle
by Michael Brundage Created: 26 Apr 2014 Updated: 26 Apr 2014
Given an N-story building and M eggs, how would you find out the highest floor of the building from which you can drop the eggs before they break? Eggs may be dropped again until broken.
The original problem was stated like this:
Given a 100-story building and only two eggs, how would you find out the highest floor of the building from which you can drop the eggs before they break?
which corresponds to the case that N=100 and M=2.
The algorithm given in the comments is: Try dropping the first egg from floors in that order.1 If it never breaks, then the answer is 100. If it breaks on some floor , then drop the second egg from the floor immediately above the last known good floor (, with ) and continue incrementing the floor by 1 until the egg breaks. The last floor before the second egg breaks is the answer. The worst-case number of trials required is then 14, corresponding to the case that the first egg broke on floor 14 and the second egg broke on floor 13, for a total of 14 trials.
I will show that this algorithm is correct for , and generalize it to any number of floors and eggs.
We need one node in the search tree per floor. If we have at least eggs, then this problem reduces to binary search, and the worst-case requires trials. If we have eggs, then this problem reduces to linear search, and worst-case requires trials.
For other values of M, the search space is in-between linear and binary, a kind of truncated binary tree in which we are allowed to follow one direction (breaks) at most M-1 times, because after M-1 breaks, we must always take only the other direction (doesn’t break) until it finally breaks that one last time. This is equivalent to counting the number of binary strings of length T containing at most M zeroes. Then the answer to our question is the minimum T such that this count is .
The number of binary strings of length T containing exactly k zeros is . So, the number of binary strings of length T containing at most M zeros is . This is OEIS sequence A08949 and has no closed-form solution for general M.
For M=2, we can solve it exactly:
For N=100, this yields the expected answer, T=14.
For other M, because we’re using small values of M and N, we could simply brute-force it:
Here’s a chart of how the number of trials varies with M:
Notice that the differences between floors for the first egg decrease by 1 until reaching 1 (27-14=13, 39-27=12, … 100-99=1). This is due to the Pascal’s triangle/binomial coefficients inherent in the search tree. ↩