By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"This problem appears in 1 out of every 3 onsite interviews—master it, and you’ll instantly demonstrate dynamic programming maturity while solving a classic that stumps 60% of candidates."
Before tackling "Climbing Stairs," ensure you understand: 1. Recursion – Base cases, recursive calls, and call stack. 2. Memoization (Top-Down DP) – Caching repeated subproblems to avoid exponential time. 3. Dynamic Programming (Bottom-Up DP) – Iterative approach to build solutions from smaller subproblems.
climb(n) = climb(n-1) + climb(n-2)
climb(n)
dp[i] = dp[i-1] + dp[i-2]
prev1, prev2
climb(n) = fib(n+1)
dp[]
prev1
prev2
dp[i] = dp[i-1] + dp[i-2] + dp[i-3]
Mental checklist for every "Climbing Stairs" problem:
Read the problem carefully. Confirm:
1 <= n <= 45
Identify the Pattern
n=1,2,3,4
If n=1 → 1, n=2 → 2, n=3 → 3, n=4 → 5, then it’s Fibonacci(n+1).
n=1 → 1
n=2 → 2
n=3 → 3
n=4 → 5
Choose the Right Approach
n
n ≤ 20
n ≤ 45
n ≤ 10⁵
Space Optimization → If asked to optimize space (O(1)).
Implement the Solution
n=1
n=2
dp[1]
dp[2]
3
For space optimization, replace dp[] with two variables.
Test Edge Cases
1
2
n=3
n=45 → 1836311903 (max constraint in LeetCode)
n=45
1836311903
Optimize Further (If Needed)
If asked for k steps, generalize the recurrence.
Explain Time & Space Complexity
Problem: You are climbing a staircase. It takes n steps to reach the top. Each time you can climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Solution (Bottom-Up DP):
def climbStairs(n: int) -> int: if n == 1: return 1 dp = [0] (n + 1) dp[1] = 1 dp[2] = 2 for i in range(3, n + 1): dp[i] = dp[i - 1] + dp[i - 2] return dp[n]
Time Complexity: O(n) Space Complexity: O(n)
Why This Works: - The problem follows the Fibonacci sequence (dp[n] = dp[n-1] + dp[n-2]). - We build the solution iteratively to avoid recursion stack overhead.
dp[n] = dp[n-1] + dp[n-2]
Problem: Same as above, but optimize space to O(1).
Solution:
def climbStairs(n: int) -> int: if n == 1: return 1 prev1, prev2 = 1, 2 for _ in range(3, n + 1): curr = prev1 + prev2 prev1, prev2 = prev2, curr return prev2
Time Complexity: O(n) Space Complexity: O(1)
Why This Works: - Instead of storing the entire dp[] array, we only keep track of the last two values (prev1 and prev2). - This reduces space from O(n) → O(1).
Problem: You can climb 1, 2, or 3 steps at a time. How many distinct ways can you reach the top?
def climbStairs(n: int) -> int: if n == 1: return 1 if n == 2: return 2 if n == 3: return 4 dp = [0] (n + 1) dp[1], dp[2], dp[3] = 1, 2, 4 for i in range(4, n + 1): dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3] return dp[n]
Why This Works: - The recurrence generalizes to dp[i] = dp[i-1] + dp[i-2] + dp[i-3]. - We initialize the first three values (1, 2, 4) and build up.
1, 2, 4
if n == 1: return 1
if n == 2: return 2
dp[i] = dp[i-1] + dp[i-2] + ... + dp[i-k]
dp[i][last_step]
"Alright, let’s lock this in. The ‘Climbing Stairs’ problem is Fibonacci in disguise. Here’s your 30-second cheat sheet:
Now go crush that interview. You’ve got this!
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.