Min Cost Climbing Stairs
You are given an integer array `cost` where `cost[i]` is the cost to step on the $i$-th stair. You can either start at index 0 or index 1. Find the minimum cost to reach the top of the floor. Use DP.
Why Interviewers Ask This
Spotify asks this to evaluate your ability to identify overlapping subproblems and apply dynamic programming efficiently. They want to see if you can recognize that the optimal path to any stair depends only on the previous two states, allowing you to optimize space complexity from O(n) to O(1). This tests your core algorithmic thinking and code optimization skills under pressure.
How to Answer This Question
Key Points to Cover
- Explicitly defining the recurrence relation as min(prev, prev2) + current_cost
- Recognizing the base cases allow starting at either index 0 or 1
- Optimizing space complexity from O(n) to O(1) using two variables
- Correctly identifying that the final answer is the min of the last two calculated values
- Demonstrating a clear thought process through a manual trace of the example
Sample Answer
Common Mistakes to Avoid
- Failing to realize the final step doesn't require adding the cost of a non-existent top floor
- Using an O(n) space array when interviewers expect O(1) space optimization
- Incorrectly setting base cases by ignoring that one can start at index 1 without paying index 0's cost
- Confusing the cost to reach a step versus the cost to climb off a step
Practice This Question with AI
Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.