Data Structures & AlgorithmsHardProduct Company SDE pattern
What is printed? (egg dropping: minimum trials in the worst case)
What is printed? (egg dropping: minimum trials in the worst case)
def trials(eggs, floors):
# dp[m] = max floors checkable with m moves for the current number of eggs
dp = [0] * (eggs + 1); m = 0
while dp[eggs] < floors:
m += 1
for e in range(eggs, 0, -1): dp[e] = dp[e] + dp[e - 1] + 1
return m
print(trials(1, 10), trials(2, 100), trials(2, 36))- A. 10 14 8
- B. 10 10 8
- C. 4 14 8
- D. 10 14 9
Correct answer: A. 10 14 8
With m moves and e eggs one can cover f(m, e) = f(m − 1, e − 1) + f(m − 1, e) + 1 floors. One egg forces a linear scan (10). Two eggs and 100 floors need 14 (14·15/2 = 105 ≥ 100); 36 floors need 8 (8·9/2 = 36). O(e · answer).
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- What is printed? (0/1 knapsack with item reconstruction) python w = [1, 3, 4, 5]; v = [1, 4, 5, 7]; W = 7; n = 4 dp = [[0] (W + 1) for in…
- What is printed? (number of BSTs vs number of binary heaps on keys 1..4) python from math import comb from functools import lrucache…
- What is printed? (diameter of an unweighted tree with two BFS passes) python from collections import deque edges = [(1, 2), (1, 3), (2…
- What is printed? (articulation points by brute force removal) python edges = [(0, 1), (1, 2), (2, 0), (1, 3), (3, 4), (4, 5), (5, 3), (5…
- What is printed? (strongly connected components count, Kosaraju) python g = {0: [1], 1: [2], 2: [0, 3], 3: [4], 4: [5], 5: [3], 6: [5, 7]…
- What is printed? (greedy fractional knapsack) python items = [(10, 60), (20, 100), (30, 120)] # (weight, value) cap, total = 50, 0.0 for…