# Egg Drop Puzzle

by 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.

SPOILER BELOW

The algorithm given in the comments is: Try dropping the first egg from floors $F_i = 14, 27, 39, 50, 60, 69, 77, 84, 90, 95, 99, 100\ (1 \le i \le 12)$ in that order.1 If it never breaks, then the answer is 100. If it breaks on some floor $F_i$, then drop the second egg from the floor immediately above the last known good floor ($F_{i-1} + 1$, with $F_0 = 0$) 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 $(M,N) = (2,100)$, 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 $M > log_2(N)$ eggs, then this problem reduces to binary search, and the worst-case requires $\lfloor\log_2(N)\rfloor + 1$ trials. If we have $M=1$ eggs, then this problem reduces to linear search, and worst-case requires $N$ 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 $\ge N$.

The number of binary strings of length T containing exactly k zeros is ${T \choose k}$. So, the number of binary strings of length T containing at most M zeros is $\sum_{k=0}^{M} {T \choose k}$. 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:

1. 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.