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?- A. 10 4 10 7
- B. 4 4 14 9
- C. 10 5 14 9
- 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
- Problem: Two players alternately take a pile from either END of a row; both play optimally. Compute (first player's total − second…
- Problem: Maximum sum of a non-empty contiguous subarray where you may delete at most one element (the result must stay non-empty). Track…
- Problem: Assign 4 workers to 4 jobs (one each) minimising total cost with a bitmask DP over the set of jobs already taken; compare with…
- Problem: Convert Roman numerals to integers: a symbol smaller than the one after it is subtracted. python def roman(s): v = {"I": 1, "V"…
- Problem: Is b a rotation of a? Check equal lengths and whether b occurs in a + a. python def isrot(a, b): return len(a) == len(b) and b in…
- Problem: String t is string s shuffled with one extra letter added. Find the extra letter by XOR-ing all character codes. python from…