Two Eggs: Minimum Worst-Case Drops
Updated
Problem statement
You have two identical eggs and a building with floors numbered 1 through floors. There is a hidden threshold t, where 0 <= t <= floors. An egg dropped from floor t or any lower floor survives. An egg dropped from any floor above t breaks.
Once an egg breaks, you cannot use it again. An egg that survives can be dropped again. You pick each next floor based on what happened in earlier drops, and you stop once you know t for sure. Return the smallest number of drops that is always enough to find t, no matter which threshold it is.
With zero floors, you already know the threshold, so the answer is zero. This is the well-known puzzle with two eggs and 100 floors, but for any number of floors.
Function
twoEggDrops(floors: int) → intExamples
Example 1
floors = 100return = 14Make the first drop from floor fourteen. Each time the egg survives, jump up one floor less than the last jump. If the first egg breaks, check the floors in that gap one by one with the second egg. This way fourteen drops can cover 14 + 13 + ... + 1 = 105 floors, which is enough. Thirteen drops cover only 91 floors, which is too few. So the answer is fourteen.
Example 2
floors = 3return = 2Drop the first egg from floor two. If it breaks, t is zero or one, so drop the other egg from floor one to tell which. If it survives, t is two or three, so drop the same egg from floor three. Either way you know t after two drops. One drop is not enough, because it cannot tell four possible thresholds apart. So the answer is two.
Constraints
0 <= floors <= 10^9.
More Apple Onsite Interview questions
- Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- FIFO Mutex Operation TraceONSITE INTERVIEW · Seen Oct 2026
- Implement Hit CounterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Parse Nested HTML into a TreeONSITE INTERVIEW · Seen Oct 2026
- Thread-Safe Concurrent Linked ListONSITE INTERVIEW · Seen Sep 2026
- Palindrome Match PercentageONSITE INTERVIEW · Seen Sep 2026
- Binary Tree Nodes at Distance KONSITE INTERVIEW · Seen Aug 2026
- BST to Sorted Circular Doubly Linked ListONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Koko Eating BananasTekion · ONSITE INTERVIEWTekion interview questions
- Koko Eating BananasAmazon · ONSITE INTERVIEW
- Maximum Supplier ProfitExpedia · ONSITE INTERVIEWExpedia interview questions
- First Register Overflow TickNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Find a Function Root by BisectionJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- Integer Square RootMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- K-th Amount a Payout Machine Can GiveRippling · ONSITE INTERVIEWRippling interview questions
- Integer Square Root with Error ChecksBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Server Upgrade PlanningWells Fargo · OAWells Fargo interview questions