FastPrepTwo Eggs: Minimum Worst-Case Drops

Two Eggs: Minimum Worst-Case Drops

Apple logoApple● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

You have two identical eggs and a building with floors numbered 1 through floors. An unknown threshold t, where 0 <= t <= floors, means an egg survives a drop from any floor at most t and breaks from any higher floor.

A broken egg cannot be reused; an intact egg can. Choose drop floors adaptively to determine t with certainty. Return the smallest possible number of drops in the worst case over all thresholds.

With zero floors, the threshold is already known and the answer is zero. This exercise generalizes the reported two-eggs, 100-floors puzzle to an input number of floors.

Function

twoEggDrops(floors: int) → int

Examples

Example 1

floors = 100return = 14

Fourteen drops cover 14 + 13 + ... + 1 = 105 floors; thirteen cover only 91.

Example 2

floors = 3return = 2

Drop first at floor two, then use the remaining egg or drop to distinguish the remaining thresholds.

Constraints

  • 0 <= floors <= 10^9.

More Apple problems

See Apple hiring insights
public int twoEggDrops(int floors) {
    // Write your solution here.
}
floors100
expected14
Checking account…