How to Explain Min Cost Climbing Stairs in a Coding Interview (Step-by-Step)
Min Cost Climbing Stairs is the gateway Dynamic Programming problem — the first place most candidates encounter the idea of subproblems, recurrence relations, and the bottom-up table. Knowing it cold is table stakes; knowing how to explain it — the DP formulation, the O(1) space optimisation, and the “top as a virtual step” framing — is what earns points.
In this post, you’ll learn exactly how to walk an interviewer through LeetCode 746: the recurrence relation from first principles, a full bottom-up DP table trace, the two-variable space optimisation, and every edge case worth naming.
Problem Statement
You are given an integer array cost where cost[i] is the cost of the ith step on a staircase. Once you pay the cost, you can climb either one or two steps.
You can either start from step index 0 or step index 1. Return the minimum cost to reach the top of the floor — the top is one step beyond the last index.
Examples
Output: 15
Explanation: Start at index 1, pay 15, jump 2 steps to the top.
Output: 6
Explanation: Take steps 0→2→4→6→7→9→top, paying 1+1+1+1+1+1 = 6.
Constraints
2 <= cost.length <= 10000 <= cost[i] <= 999
What Is the Problem?
“I’m on a staircase. Each step has a cost to enter it. Once I pay, I can jump 1 or 2 steps forward. I can start for free at step 0 or step 1, and I want to exit at a virtual ‘top’ step one beyond the last index. I need the minimum total cost to reach that top. This is a classic DP problem because the minimum cost to reach any step depends on the minimum cost of reaching the two steps before it.”
The phrase “minimum cost to reach any step depends on the two steps before it” is the entire recurrence relation in plain English. Stating it before writing a formula signals that you understand why DP applies.
Step 1: Compare Approaches Before Jumping to Code
“I’ll present the full bottom-up DP table first — it’s the clearest way to derive and verify the recurrence. Then I’ll show how to compress it to O(1) space using just two rolling variables, since each step only depends on the previous two.”
The Key Insight — The Top is a Virtual Step
Think of the “top” as a virtual step at index n (one beyond the last index), with cost zero. The answer is simply the minimum cost to reach step n.
To reach any step i, you either came from step i−1 (paid cost[i−1]) or from step i−2 (paid cost[i−2]). The minimum cost at i is the minimum of those two paths.
Steps 0 and 1 are free to start — their entry cost in the DP table is 0 (you haven’t paid to arrive there yet; you pay when you leave). This is the base case.
“Framing the top as a virtual step at index n unifies all cases — there’s no need to separately handle ‘can I reach the top from the last step or second-to-last step?’ The recurrence handles it automatically.”
The Recurrence Relation
Definition: Let dp[i] = the minimum cost to reach step i (not including the cost of step i itself — you haven’t paid to leave yet).
The answer is dp[n] where n = len(cost).
Why not include cost[i] in dp[i]? Because you pay the cost of a step when you leave it, not when you arrive. dp[i] is the cost to get your feet onto step i; the cost of step i will be added when computing dp[i+1] or dp[i+2].
Step 2: Explain the Algorithm in Plain English
Allocate dp of size n+1. Set dp[0] = 0 and dp[1] = 0. These represent the free start positions.
For i from 2 to n: dp[i] = min(dp[i−1] + cost[i−1], dp[i−2] + cost[i−2]). Each entry is the cheapest way to land on step i.
dp[n] is the minimum cost to reach the top — the virtual step beyond the last index.
Since dp[i] only depends on dp[i−1] and dp[i−2], replace the full array with two rolling variables prev2 and prev1. Update them in each iteration: curr = min(prev1 + cost[i−1], prev2 + cost[i−2]), then shift.
DP Table Visualised — cost = [10, 15, 20]
n = 3. The virtual top is at index 3. dp[i] = minimum cost to arrive at step i.
| Step i | cost[i] | dp[i] = min cost to ARRIVE at i | Calculation |
|---|---|---|---|
| 0 | 10 | 0 | Base case — free start |
| 1 | 15 | 0 | Base case — free start |
| 2 | 20 | 10 | min(dp[1]+cost[1], dp[0]+cost[0]) = min(0+15, 0+10) = 10 |
| 3 (top) | — | 15 | min(dp[2]+cost[2], dp[1]+cost[1]) = min(10+20, 0+15) = 15 ✓ |
The answer is dp[3] = 15. The optimal path is: start at step 1 (free), pay cost[1]=15, jump 2 steps to the top. Total: 15.
The O(1) Space Optimisation
The full DP table uses O(n) space — but each row only ever looks back two positions. We can replace the entire array with two rolling variables: prev2 (was dp[i−2]) and prev1 (was dp[i−1]).
At each step: curr = min(prev1 + cost[i−1], prev2 + cost[i−2]). Then shift: prev2 = prev1, prev1 = curr. After n−1 iterations, prev1 holds dp[n] — the answer.
This is the same pattern used in Fibonacci — when the recurrence only looks back a fixed number of steps, the table collapses to those variables.
“Mentioning the connection to Fibonacci is a strong signal to the interviewer — it shows you recognise a pattern class, not just a one-off solution.”
Step 3: Talk Through the Code
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len(cost)
# O(1) space — two rolling variables replacing the full DP array
prev2 = 0 # dp[i-2]: min cost to reach two steps back (base case dp[0] = 0)
prev1 = 0 # dp[i-1]: min cost to reach one step back (base case dp[1] = 0)
# Compute dp[2] through dp[n] iteratively
for i in range(2, n + 1):
curr = min(prev1 + cost[i - 1], prev2 + cost[i - 2])
prev2 = prev1
prev1 = curr
return prev1 # prev1 now holds dp[n] — min cost to reach the top
Three things to narrate: (1) the loop runs from 2 to n inclusive — that’s n−1 iterations, producing dp[2] through dp[n]; (2) cost[i−1] and cost[i−2] access the cost of the step below — because we’re computing the cost to arrive at i, not to leave it; (3) the update order matters — prev2 = prev1 must happen before prev1 = curr, or you’d overwrite the value you still need.
Step 4: Highlight Edge Cases
“With only two steps, the loop runs once (i=2). curr = min(prev1 + cost[1], prev2 + cost[0]) = min(cost[1], cost[0]). Return whichever step is cheaper — which is the correct answer: start at the cheaper step and jump 1 to the top.”
“Every step is free. The minimum cost is 0, reached by any path. The DP correctly returns 0 since all additions are zero throughout.”
“Like Example 2: [1,100,1,1,1,100,1,1,100,1]. The DP naturally skips the 100-cost steps by always choosing the cheaper of the two predecessors. This is exactly what makes DP superior to greedy — greedy could make a locally cheap choice that leads to an expensive step later.”
“With costs like [1,2,3,4,5], the optimal path is always to take 2-step jumps from the cheaper odd steps. The DP finds this automatically without any special casing.”
“If both paths are equally cheap,min()returns the first one — the path fromi−1. The answer is still correct either way — the problem asks for the minimum cost, not a unique path.”
Step 5: State Time and Space Complexity
| Dimension | Full DP Table | Space-Optimised | Reason |
|---|---|---|---|
| Time | O(n) | O(n) | Single pass through all n steps; each step does O(1) work |
| Space | O(n) | O(1) | Full table stores n+1 values; optimised version stores only 2 variables |
“Time is O(n) regardless — one pass, one O(1) operation per step. The DP table approach uses O(n) space; the rolling-variable approach brings it down to O(1). The optimisation is possible because the recurrence only looks back two positions — like Fibonacci. If the recurrence looked back k positions, we’d need O(k) space.”
Interview Checklist for Min Cost Climbing Stairs
- Restate — “minimum cost to reach the virtual top step beyond the last index; can start free at step 0 or 1”
- Explain why naive recursion is exponential — overlapping subproblems
- State the DP definition clearly: dp[i] = min cost to arrive at step i (not including cost[i])
- Write the recurrence: dp[i] = min(dp[i−1] + cost[i−1], dp[i−2] + cost[i−2])
- Explain the base cases: dp[0] = dp[1] = 0 (free start positions)
- Frame the top as a virtual step at index n — unifies the final answer with the recurrence
- Show the full DP table for the example before introducing the space optimisation
- Explain the O(1) space optimisation — two rolling variables, same update-order caveat as Fibonacci
- Mention the Fibonacci connection — recurrence looking back a fixed number of steps collapses to rolling variables
- State O(n) time and O(1) space for the optimised solution
Related Problems to Practice
If you can explain Min Cost Climbing Stairs cleanly, try these next:
