Data Structures & AlgorithmsHardProduct Company SDE · DP round
Problem: What is the minimum path sum from the top to the bottom of the triangle [[2], [3, 4], [6, 5, 7], [4, 1, 8, 3]], moving to an adjacent entry in the next row? Run the bottom-up DP.
Problem: What is the minimum path sum from the top to the bottom of the triangle [[2], [3, 4], [6, 5, 7], [4, 1, 8, 3]], moving to an adjacent entry in the next row? Run the bottom-up DP.
tri = [[2], [3, 4], [6, 5, 7], [4, 1, 8, 3]]
dp = tri[-1][:]
for row in reversed(tri[:-1]):
dp = [x + min(dp[i], dp[i + 1]) for i, x in enumerate(row)]
print(dp[0])What is printed?- A. 21
- B. 11
- C. 15
- D. 16
Correct answer: B. 11
Bottom-up, each cell adds the smaller of its two children: the best path is 2 → 3 → 5 → 1 = 11. Using max instead of min gives 21, always going straight down the left edge gives 15, and a top-down DP that reads the last cell of the bottom row instead of its minimum gives 16.
More Coding Interview Problems questions
- Problem: How many unordered pairs (i < j) of 1..10 have a sum divisible by 5? Run the code. python a = list(range(1, 11)) print(sum(1 for…
- Problem: Minimum number of operations to reduce x = 5 to 0 by removing a number from either end of [1, 1, 4, 2, 3] (each removal subtracts…
- Problem: How many ordered sequences of 1s and 3s sum to 6? Run the code. python ways = [1] + [0] 6 for v in range(1, 7): for s in (1, 3)…
- Problem: What is the maximum XOR of any contiguous subarray of [8, 1, 2, 12]? Run the brute force. python a = [8, 1, 2, 12] best = 0 for i…
- Problem: Sort [1, 1, 2, 2, 2, 3] by frequency ascending, breaking ties by value ascending. What is printed? python from collections import…
- Problem: How many set bits are there in total across the numbers 0 to 7? Run the code. python print(sum(bin(i).count('1') for i in…