Egg Drop With 2 Eggs and N Floors - Problem

You are given two 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.

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

Input & Output

Example 1 — Small Building
$ Input: n = 2
Output: 2
💡 Note: Drop first egg from floor 1. If it breaks, critical floor is 0 (2 moves). If it doesn't break, drop from floor 2. If it breaks, critical floor is 1, otherwise critical floor is 2 (2 moves total).
Example 2 — Medium Building
$ Input: n = 10
Output: 4
💡 Note: Using optimal strategy: drop from floors 4, 7, 9, 10. This guarantees finding the critical floor in at most 4 moves regardless of where it is.
Example 3 — Single Floor
$ Input: n = 1
Output: 1
💡 Note: Only one floor to test. Drop the egg from floor 1. If it breaks, critical floor is 0, otherwise it's 1. Only 1 move needed.

Constraints

  • 1 ≤ n ≤ 1000

Visualization

Tap to expand
Egg Drop With 2 Eggs and N Floors INPUT Floor 2 Floor 1 Ground (f=0) 2 Identical Eggs n = 2 floors ALGORITHM STEPS 1 Drop from Floor 1 If breaks: f=0 (1 move) 2 If Egg 1 Survives Go to Floor 2 with same egg 3 Drop from Floor 2 If breaks: f=1, else f=2 4 DP Formula dp[k] = dp[k-1] + dp[k-1] + 1 Decision Tree F1 X F2 f=0 f=1,2 FINAL RESULT Worst Case Scenarios: Scenario 1: f=0 Drop F1 --> breaks Moves: 1 Scenario 2: f=1 or f=2 Drop F1, then F2 Moves: 2 OUTPUT 2 Minimum moves = 2 Guarantees finding f [OK] Key Insight: With 2 eggs and optimal strategy, we can cover at most k*(k+1)/2 floors with k moves. For n=2: We need minimum k where k*(k+1)/2 >= 2. With k=2: 2*3/2 = 3 >= 2. Answer: 2 moves. General formula: Find smallest k such that k*(k+1)/2 >= n (triangular number approach). TutorialsPoint - Egg Drop With 2 Eggs and N Floors | Optimal Solution Approach
Asked in
Google 35 Microsoft 28 Amazon 22 Facebook 18
32.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen