Egg Drop

May 7, 2021


I did mention the classic egg drop problem back in my early days, when I started the interview process with many companies. There is a leetcode problem as well, in a more generic version:

You are given k identical eggs and you have access to a building with n floors labeled from 1 to n.

You know that there exists a floor f where 0 <= f <= n such that any egg dropped at a floor higher than f will break, and any egg dropped at or below floor f will not break.

Each move, you may take an unbroken egg and drop it from any floor x (where 1 <= x <= n). If the egg breaks, you can no longer use it. However, if the egg does not break, you may reuse it in future moves.

Return the minimum number of moves that you need to determine with certainty what the value of f is.

The original question asked during my interview was only using 2 eggs, and you can probably solve it by just enumeration. However, if the number of eggs increases, it is almost impossible to list all possibilities. There should be some programmatic way to solve this kind of problem.

Without loss of generality, we still assume we have 2 eggs. But later you can see my program can be easily generated to more than 2 eggs.

Step 1: Intuition

One can easily think about some sort of binary search algorithms. The binary search algorithm can quickly identify target in a sorted list of candidates. Is this all? No. Because the problem adds an additional constraint - the number of eggs is 2, and some trials may have some consequences to later trials. If an egg is broken, we need to be more conservative in the next move; otherwise we may not get to the final answer. For example, suppose we have 2 eggs and 100 floors, and our first trial at floor 50 breaks the egg; then we have only 1 egg left, and we will have to try each floor between 1 and 49 one by one using the only remaining egg.

But you will notice the pattern quickly:

  1. If an egg is broken at floor f, we will have to be conservative - basically try all the lower floors one by one.

  2. If an egg is not broken at floor f, we can try all the upper (n-f) floors, and the number of eggs will remain the same.

We will definitely consider the worst case in the above 2 cases, basically take the max moves between the 2. But we also want to minimize the final answer.

We also notice that, the optimal number of moves required in step 1 is increasing with respect to f, while the optimal number of moves required in step 2 is decreasing with respect to f. Since we are taking the maximum number between the 2 cases, we know that the optimal choice, will be some 1<=f<=n, where case 1 and case 2 have exactly the same number of moves - basically we need to balance case 1 and case 2 moves.

Step 2: Program

With the above analysis in mind, we can write our Python program. We are using top-down Dynamic Programming with memoization. Note that our program can be easily extended to more than 2 eggs (as simple as changing line 25).

Step 3: Test

We can test a few floors.

The final output looks like:

1-floor building requires 1 moves.

2-floor building requires 2 moves.

10-floor building requires 4 moves.

50-floor building requires 10 moves.

100-floor building requires 14 moves.