Egg Drop Puzzle
by Michael Brundage Created: 26 Apr 2014 Updated: 26 Apr 2014
Given an Nstory 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 100story 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.
SPOILER BELOW
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 worstcase 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 worstcase requires trials. If we have eggs, then this problem reduces to linear search, and worstcase requires trials.
For other values of M, the search space is inbetween linear and binary, a kind of truncated binary tree in which we are allowed to follow one direction (breaks) at most M1 times, because after M1 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 closedform 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 bruteforce 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 (2714=13, 3927=12, … 10099=1). This is due to the Pascal’s triangle/binomial coefficients inherent in the search tree. ↩