Climbing Stairs: The Gateway to Dynamic Programming (Data-Backed Guide)

By DSA Prep Team · March 4, 2026 · 8 min read · Algorithms

Climbing Stairs is the #17 most asked LeetCode problem globally — and the single most important problem for understanding how Dynamic Programming works from first principles.

Our analysis of 10,385 verified interview questions across 259 companies shows it appeared 60 times across 25 companies — including Google, Amazon, Meta, Microsoft, Goldman Sachs, Citadel, Bloomberg, Grammarly, J.P. Morgan, and Intuit.

The problem is disguised Fibonacci. Once you see that ways(n) = ways(n-1) + ways(n-2), you understand the core DP pattern: break a problem into overlapping subproblems. Master this, then scale up to House Robber, Coin Change, and every 1D DP problem in interviews.

Table of Contents

The Data: 25 Companies Ask This

MetricValue
Total Interview Appearances60 verified mentions
Companies That Ask It25 companies
Global Rank#17 out of 10,385 questions
DifficultyEasy
Core PatternsDynamic Programming, Math, Memoization

📌 Companies That Ask This Problem

Google, Amazon, Meta, Microsoft, Goldman Sachs, Citadel, Bloomberg, Grammarly, J.P. Morgan, Intuit, AMD, Adobe, Apple, Barclays, Bolt, ByteDance, Cisco, Deloitte, Disney, Expedia, IBM, Infosys, Intel, and 2 more.

Problem Statement

LeetCode #70 — Climbing Stairs

You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Examples
Input: n = 2   →  Output: 2   (1+1, 2)
Input: n = 3   →  Output: 3   (1+1+1, 1+2, 2+1)
Input: n = 5   →  Output: 8

The Core Intuition: Fibonacci in Disguise

To reach step n, you came from either step n-1 (took 1 step) or step n-2 (took 2 steps). So ways(n) = ways(n-1) + ways(n-2) with base cases ways(1) = 1, ways(2) = 2. That's exactly the Fibonacci recurrence.

💡 The DP Recurrence

dp[i] = dp[i-1] + dp[i-2] where dp[1]=1, dp[2]=2. The answer is dp[n]. Since you only need the last two values, you can use O(1) space with just two variables.

Three Approaches

ApproachTimeSpaceNotes
Recursion (no memo)O(2ⁿ)O(n)TLE — exponential, never use
Top-down DP (memoization)O(n)O(n)Good for explaining DP concept
Bottom-up DP (2 vars)O(n)O(1)Optimal — use this in interviews

Code in Python, Java & JavaScript

Python 3 — Bottom-up O(1) space
def climbStairs(n: int) -> int:
    if n <= 2:
        return n

    prev2, prev1 = 1, 2  # ways(1), ways(2)

    for _ in range(3, n + 1):
        curr = prev1 + prev2
        prev2 = prev1
        prev1 = curr

    return prev1
Python 3 — Top-down Memoization
from functools import lru_cache

def climbStairs(n: int) -> int:
    @lru_cache(maxsize=None)
    def dp(i):
        if i <= 2:
            return i
        return dp(i - 1) + dp(i - 2)

    return dp(n)
Java
class Solution {
    public int climbStairs(int n) {
        if (n <= 2) return n;
        int prev2 = 1, prev1 = 2;

        for (int i = 3; i <= n; i++) {
            int curr = prev1 + prev2;
            prev2 = prev1;
            prev1 = curr;
        }
        return prev1;
    }
}
JavaScript
var climbStairs = function(n) {
    if (n <= 2) return n;
    let prev2 = 1, prev1 = 2;

    for (let i = 3; i <= n; i++) {
        [prev2, prev1] = [prev1, prev1 + prev2];
    }
    return prev1;
};

Complexity Analysis

📊 Complexity

1D DP Family

ProblemRecurrencePattern
LC #70 Climbing Stairsdp[i] = dp[i-1] + dp[i-2]Fibonacci
LC #198 House Robberdp[i] = max(dp[i-1], dp[i-2] + nums[i])Skip or take
LC #213 House Robber IIRun robber on [0..n-2] and [1..n-1]Circular array
LC #322 Coin Changedp[i] = min(dp[i-coin]+1) for each coinUnbounded knapsack
LC #746 Min Cost Climbing Stairsdp[i] = cost[i] + min(dp[i-1], dp[i-2])Climbing Stairs with cost

How to Actually Remember This

✅ Spaced Repetition Schedule

  1. Today: Solve LC #70. Then immediately solve LC #746 (Min Cost Climbing Stairs) — same pattern with a cost twist.
  2. Day 3: Solve LC #198 (House Robber) — the most important 1D DP after Climbing Stairs.
  3. Day 7: Solve LC #322 (Coin Change) — extends the idea to unbounded choices.
  4. Day 14: Solve LC #213 (House Robber II) — circular boundary condition.

Tag on DSAPrep.dev under Dynamic Programming.

Track on DSAPrep.dev →