Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: Min-cost climbing: cost = [10, 15, 20, 5, 1]. You may start at index 0 or 1, and from index i you can pay cost[i] to move up 1 or 2 steps. Reaching the top means going past the last index. What is the minimum total cost?

  1. A. 20
  2. B. 30
  3. C. 21
  4. D. 31

Correct answer: A. 20

dp[i] = min(dp[i − 1] + cost[i − 1], dp[i − 2] + cost[i − 2]): start at index 1 (15), jump to index 3 (5) and then past the end, total 20. Forcing a start at index 0 gives 30, requiring to land on the last index gives 21, and greedily stepping to the cheaper next stair gives 31.

More Coding Interview Problems questions

Exams that ask this topic