Data Structures & AlgorithmsVery hardICPC regional · DP puzzles

Problem: You have 2 eggs and a building with 10 floors. What is the minimum number of drops needed in the worst case to find the highest safe floor? Run the code.

Problem: You have 2 eggs and a building with 10 floors. What is the minimum number of drops needed in the worst case to find the highest safe floor? Run the code.
floors = 10
m = 0
while m * (m + 1) // 2 < floors:
    m += 1
print(m)
What is printed?
  1. A. 3
  2. B. 5
  3. C. 10
  4. D. 4

Correct answer: D. 4

With 2 eggs and m drops you can distinguish m(m+1)/2 floors, and 4 * 5 / 2 = 10 is the first value at least 10. Binary search (needing a broken egg per halving) is the trap.

More Coding Interview Problems questions

Exams that ask this topic