Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA

Problem: With k identical eggs and an n-floor building, find the minimum number of drops that guarantees finding the critical floor. DP on moves: dp[e] = floors coverable with e eggs in m moves; dp[e] ← dp[e] + dp[e−1] + 1.

Problem: With k identical eggs and an n-floor building, find the minimum number of drops that guarantees finding the critical floor. DP on moves: dp[e] = floors coverable with e eggs in m moves; dp[e] ← dp[e] + dp[e−1] + 1.
def min_moves(k, n):
    dp = [0] * (k + 1); m = 0
    while dp[k] < n:
        m += 1
        for e in range(k, 0, -1):
            dp[e] = dp[e] + dp[e - 1] + 1
    return m
print(min_moves(1, 10), min_moves(2, 10), min_moves(2, 100), min_moves(3, 100))
What is printed?
  1. A. 10 4 10 7
  2. B. 4 4 14 9
  3. C. 10 5 14 9
  4. D. 10 4 14 9

Correct answer: D. 10 4 14 9

One drop from some floor splits the problem: if the egg breaks, dp[e−1] floors below can be resolved with the remaining moves; if not, dp[e] floors above; plus the floor dropped from. One egg forces a linear scan (10). Two eggs: 1 + 2 + … + m ≥ n → m = 4 for 10 and 14 for 100; three eggs need 9 for 100. Binary search (7 for 100) is wrong because breaking eggs runs out. O(k · answer).

More Coding Interview Problems questions

Exams that ask this topic