FastPrepTwo-Egg Dropping

Two-Egg Dropping

NVIDIA logoNVIDIA● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

A building has floors 1 through n and an unknown highest safe floor f, where 0 <= f <= n. You have two identical eggs. A drop above f breaks the egg; a drop at or below f leaves it reusable. A broken egg cannot be used again.

Choose each drop based on previous outcomes. Return the smallest number of drops that guarantees you can determine f in the worst case.

Function

twoEggDrop(n: int) → int

Examples

Example 1

n = 1return = 1

One drop distinguishes highest safe floor 0 from 1.

Example 2

n = 6return = 3

Start at floor 3, then 5, then 6 if the first egg survives. If it breaks, linearly check the remaining lower floors with the second egg; at most three drops are needed.

Example 3

n = 50return = 10

Nine drops can cover at most 45 floors; ten drops can cover 55.

Constraints

  • 1 <= n <= 1000.
  • Drop outcomes follow one fixed threshold and are deterministic.

More NVIDIA problems

See NVIDIA hiring insights
public int twoEggDrop(int n) {
    // Write your code here.
}
n1
expected1
Checking account…