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?
  1. A. 21
  2. B. 11
  3. C. 15
  4. 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

Exams that ask this topic