Magicsheet logo

Egg Drop With 2 Eggs and N Floors

Medium
37.1%
Updated 6/1/2025

Egg Drop With 2 Eggs and N Floors

What is this problem about?

The Egg Drop With 2 Eggs and N Floors coding problem is a classic optimization puzzle. You have 2 identical eggs and a building with n floors. There is a specific floor f (0 to n) such that any egg dropped from a floor higher than f will break, and any egg dropped from f or below will not. You need to find the minimum number of moves required to determine f with certainty in the worst-case scenario.

Why is this asked in interviews?

Companies like Google and Disney ask the Egg Drop interview question to evaluate a candidate's mathematical intuition and dynamic programming interview pattern skills. It requires thinking about minimizing the maximum number of trials. It’s a test of whether you can model a problem using a recurrence relation or derive a mathematical formula (triangular numbers) to solve it.

Algorithmic pattern used

This can be solved using Dynamic Programming or Math.

  1. DP: dp[k][n] is the minimum moves for k eggs and n floors.
    • For 2 eggs, if you drop from floor x:
      • Egg breaks: You have 1 egg left and must check x-1 floors linearly. (x trials total).
      • Egg survives: You have 2 eggs left and must check n-x floors.
    • dp[2][n] = 1 + min(max(x, dp[2][n-x])) for all x.
  2. Math: With m moves and 2 eggs, you can cover m + (m-1) + (m-2) + ... + 1 floors. This is the sum of an arithmetic progression: m(m+1)/2.
    • Solve for m such that m(m+1)/2 >= n.

Example explanation

Suppose n = 100.

  1. Using the formula: m(m+1)/2 >= 100.
  2. For m = 13, 13*14/2 = 91. (Too small).
  3. For m = 14, 14*15/2 = 105. (Enough). Result: 14. This means you first drop from floor 14. If it breaks, you check floors 1-13 (14 drops total). If it doesn't break, you next drop from floor 14 + 13 = 27. If that breaks, you check 15-26 (14 drops total).

Common mistakes candidates make

  • Binary Search Fallacy: Assuming binary search (dropping from the middle) is optimal. With only 2 eggs, if the first drop at floor 50 breaks, you must check 1-49 linearly, which is 50 moves—not optimal.
  • Ignoring the Worst Case: Forgetting that the strategy must work even if the "critical floor" is the most inconvenient one.
  • Complexity: Writing an O(N^2) DP when an O(1) or O(sqrt(N)) math solution exists.

Interview preparation tip

Always start with the case of 1 egg. With 1 egg, you have no choice but to drop from every floor starting from 1. This linear constraint is what makes the 2-egg problem a balance between jumping floors and linear checking.

Similar Questions