← SciSim / Computer Science

[E] Egg Drop Problem

Minimax DP | Binomial Coefficient | O(n*k^2) and O(n log k)

Graduate

Section 1 -- Interactive Simulation

Ready. Press Play or Next.
Speed:
0
Operations
0
Comparisons
0/0
Step
--
n
n: 8
EGG-DROP(n, k): // n=eggs, k=floors
  // dp[i][j] = min trials needed with i eggs, j floors
  dp[i][0]=0, dp[i][1]=1, dp[1][j]=j for all i,j
  for i=2 to n:
    for j=2 to k:
      dp[i][j] = infinity
      for x=1 to j: // try floor x
        cost = 1 + max(dp[i-1][x-1], // egg breaks
                       dp[i][j-x]) // egg survives
        dp[i][j] = min(dp[i][j], cost)

Section 2 -- The Idea, Step by Step

Start -- the everyday picture. You have two identical phones and a very tall building, and you want the highest floor you can drop a phone from without cracking it. Drop from too high and the phone breaks -- and now you have one fewer phone to experiment with. With only two phones you can't test every floor one at a time (far too slow), but you also can't leap too greedily, or you might smash both before you've found the answer. The egg drop problem asks the careful version of this: what is the fewest number of drops that is guaranteed to work, even on the unluckiest building?
Build -- name the pieces. Let $n$ be the number of eggs (your breakable tries) and $k$ the number of floors. We want $T$, the number of drops in the worst case. The clever move is to turn the question inside out: instead of asking "which floor do I drop from first?", ask "if I'm allowed $t$ drops and have $n$ eggs, how many floors can I possibly cover?" Call that $f(t,n)$. A single drop splits the building: if the egg breaks, the floors below must be sorted out with $t-1$ drops and $n-1$ eggs; if it survives, the floors above use $t-1$ drops and all $n$ eggs. Add the one floor you just tested: $$f(t,n) = f(t-1,\,n-1) + 1 + f(t-1,\,n).$$ Worked number: with 2 eggs and 3 drops, $f(3,2)=6$. So a 6-floor building is solvable in just 3 drops -- start by dropping from floor 3, not floor 1.
Deepen -- the precise rule. The direct table $dp[i][j]$ holds the minimum worst-case drops with $i$ eggs and $j$ floors: $$dp[i][j]=\min_{1\le x\le j}\Big(1+\max\big(dp[i-1][x-1],\;dp[i][j-x]\big)\Big).$$ The inner $\max$ is the adversary picking the worse branch (break vs. survive); the outer $\min$ is you choosing the smartest floor $x$. As $x$ rises, $dp[i-1][x-1]$ grows while $dp[i][j-x]$ shrinks, so their $\max$ is unimodal -- a binary search on $x$ trims the naive $O(nk^2)$ to $O(nk\log k)$. The inside-out version has a tidy closed form, $f(t,n)=\sum_{i=1}^{n}\binom{t}{i}$, which is exactly the Pascal's-triangle identity behind the $O(n\log k)$ method (check it: $\binom{3}{1}+\binom{3}{2}=3+3=6$).
Try this in the panel above. The animation steps through a worked trace and tallies Operations and Comparisons as it goes. (1) Drag the $n$ slider from 4 to 16 and watch the step count climb -- more elements means a longer trace, the same way more floors means more drops. (2) Use the >| button to walk one step at a time and read the message bar to follow each move. (3) Press Play at 0.25x, then switch to 8x, to feel the difference between a slow, careful search and a fast one.

Section 3 -- Complexity Analysis

SymbolMeaning
nInput size
T(n)Time complexity
V,EVertices and Edges
WWeight or capacity
Step 1 -- Problem Statement. Given n eggs and a building with k floors, find the minimum number of trials (worst case) to determine the highest safe floor. An egg breaks if dropped from above the critical floor; doesn't break if dropped from at or below. Broken eggs are discarded.
Step 2 -- Minimax Recurrence. dp[i][j] = min trials with i eggs and j floors. Try floor x: egg breaks (dp[i-1][x-1] trials below) OR survives (dp[i][j-x] trials above). Worst case = max of two outcomes. Minimize over all x: dp[i][j] = min_x(1 + max(dp[i-1][x-1], dp[i][j-x])).
Step 3 -- Time O(nk^2) Space O(nk). For each (i,j) pair: linear search over x=1..j is O(j). Total: O(n * k^2). With binary search on the optimal x (since max(dp[i-1][x-1], dp[i][j-x]) is unimodal in x): O(nk log k).
Step 4 -- Inverse Formulation O(n log k). Alternative DP: f(t,n) = max floors solvable with t trials and n eggs. f(t,n) = f(t-1,n-1) + 1 + f(t-1,n). Solve for minimum t such that f(t,n) >= k. Binary search on t: O(log k) iterations, each O(n). Total: O(n log k).
Step 5 -- Base Cases. dp[1][j]=j (1 egg: must try linearly from floor 1 to j). dp[i][0]=0 (0 floors: 0 trials). dp[i][1]=1 (1 floor: 1 trial). dp[0][j]=0 is undefined (cannot solve with 0 eggs -- treat as infinity).
Step 6 -- Optimal Strategy Insight. With enough eggs, use binary search (O(log k) trials). With 1 egg, use linear search (O(k) trials). In between: hybrid. With 2 eggs and k floors, optimal is sqrt(k) trials -- try floors sqrt(k), 2sqrt(k),... and then linear within the block.

Section 4 -- FAQ

What is the minimax strategy? concept+
Minimax: choose the floor to drop from to MINIMIZE the WORST CASE. If egg breaks: search below. If survives: search above. The adversary picks the worst outcome. We pick the floor to minimize the adversary's damage. This is the minimax principle from game theory.
With 2 eggs and k floors, why sqrt(k) trials? complex+
Drop first egg at floor sqrt(k), 2*sqrt(k), 3*sqrt(k),... If first egg breaks at floor i*sqrt(k): use second egg linearly from (i-1)*sqrt(k)+1. Total trials: sqrt(k) (for first egg) + sqrt(k)-1 (for second) = 2*sqrt(k)-1 = O(sqrt(k)).
Where is egg drop used in practice? applied+
Software testing: finding the minimum test case that causes failure (binary search vs linear). Hardware stress testing: find maximum safe voltage/temperature. A/B testing: finding threshold effect. It models any 'find the threshold' problem with limited retries.
What is the O(n log k) solution? complex+
Reframe: given t trials and n eggs, what's the maximum floors solvable? f(t,n) = f(t-1,n-1)+1+f(t-1,n) (egg breaks: explore f(t-1,n-1) floors below; survives: f(t-1,n) floors above). Binary search on t to find min t with f(t,n)>=k. O(n log k) total.
What if eggs are unlimited? nonobv+
Unlimited eggs: binary search. Minimum trials = ceil(log2(k+1)). This is the optimal case. As egg count decreases, more trials needed. With 1 egg: k trials (must be linear). Egg drop interpolates between these extremes.
How does this connect to Pascal's triangle? deep+
f(t,n) = sum_{i=1}^{n} C(t,i) (binomial coefficients). The maximum floors solvable = sum of binomial coefficients. This is because each trial produces a binary outcome, and n eggs limit the depth of the 'breakage tree'. Pascal's triangle structure underlies the O(n log k) solution.

Section 5 -- Misconceptions

Conceptual

X More eggs always means fewer trials.

O More eggs can never increase trials. But diminishing returns: beyond floor = 2^n floors needed, extra eggs give no benefit (binary search is already optimal).

X Binary search is always optimal for egg drop.

O Only optimal with unlimited eggs. With limited eggs, binary search may leave you with 0 eggs mid-search. Must use the minimax DP for optimal strategy.

X dp[0][j] = j (0 eggs, j floors needs j trials).

O dp[0][j] is UNDEFINED -- impossible to determine critical floor with 0 eggs. Treat as infinity or handle as infeasible.

Implementation

X Forgetting the base case dp[1][j]=j.

O With 1 egg: must try linearly from floor 1 upward. Any other strategy risks losing the only egg. dp[1][j]=j is essential.

X Using min instead of max in the inner recurrence.

O Must take MAX of (egg breaks, egg survives) -- worst case. Then MINIMIZE over floor choice. min(max(...)) not max(min(...)).

X Implementing O(nk^2) when k=10000 (too slow).

O O(nk^2) with k=10^4: n=2,k=10^4 => 2*10^8 operations. Use binary search optimization O(nk log k) or the f(t,n) formulation O(n log k).