EasyDynamic ProgrammingMathUpdated Sep 2026

70. Climbing Stairs

Asked at Google, Meta, Amazon, Microsoft and 4 more

Difficulty

Easy

Time

O(n)

Space

O(1)

Pattern

Dynamic Programming

Companies

8

Problem

You are standing at the bottom of a staircase with n steps. In one move you may go up either 1 step or 2 steps. Count how many different sequences of moves bring you exactly to the top.

Two ways are different if the order of your moves is different. For example, 1 + 2 and 2 + 1 are two separate ways, even though both add up to 3.

Return the total number of distinct ways as an integer.

Two ways to reach step 3

start at step 0+1 → step 1+1 → step 2+1 → step 3 (top)First way for n = 3: three single steps, 1 + 1 + 1.start at step 0+1 → step 1+2 → step 3 (top)Second way: 1 + 2. Swapping the order gives 2 + 1,which is a third distinct way.
From the bottom you may move 1 step or 2 steps at a time, and the order of moves matters.

Constraints

  • ●1 <= n <= 45

Examples

Example 1
Inputn = 2
Output2
WhyYou can take two single steps (1 + 1), or one double step (2). That is 2 different sequences.
Example 2
Inputn = 3
Output3
WhyThe sequences are 1 + 1 + 1, 1 + 2, and 2 + 1. All three reach the top, and each has a different order of moves.

Asked At

8 companies

How to Think About It

1

To reach step n, you could have come from step n-1 (took 1 step) or step n-2 (took 2 steps). So ways(n) = ways(n-1) + ways(n-2).

2

Base cases: ways(1) = 1 (one 1-step), ways(2) = 2 (two 1-steps or one 2-step).

3

This is the Fibonacci sequence: 1, 2, 3, 5, 8, 13, 21... Each number is the sum of the previous two.

4

Space optimization: you only need the last two values. Use two variables instead of an array.

5

Edge cases: n=1 (1 way), n=2 (2 ways).

Optimal Approach

Step 1: Base cases — if n <= 2, return n.
Step 2: Initialize prev2 = 1 (ways(1)), prev1 = 2 (ways(2)).
Step 3: For i from 3 to n:
current = prev1 + prev2
prev2 = prev1
prev1 = current
Step 4: Return prev1.

This is iterative Fibonacci with O(1) space. Each step is the sum of the previous two, just like Fibonacci.

Time: O(n). Space: O(1).

Each answer is the sum of the two before it

ways1235801234Index 1 holds ways(1) = 1, index 2 holds ways(2) = 2,and each later cell is the sum of the two cells beforeit.ways1235801234prev2prev1At step 5 we only need ways(3) = 3 and ways(4) = 5 tocompute ways(5) = 8, so two variables are enough.ways(1) = 1, ways(2) = 2ways(i) = ways(i-1) + ways(i-2)keep only the last two values
To land on step i you came from step i-1 or step i-2, so ways(i) = ways(i-1) + ways(i-2).
ComplexityTime O(n)Space O(1)We loop once from 3 to n, and we only keep the last two answers in two variables instead of a full array.

What Trips People Up in Real Interviews

1

Not recognizing this as the Fibonacci sequence. ways(n) = ways(n-1) + ways(n-2) with base cases ways(1) = 1, ways(2) = 2.

2

Using recursion without memoization. That's O(2^n) and will time out. Use iterative DP or memoization.

3

Forgetting the base cases. ways(1) = 1, ways(2) = 2. Without these, the recurrence doesn't work.

4

Not handling n = 0. The problem says n >= 1, but if n = 0, return 1 (one way to stay at the top).

5

Using a full DP array when only two variables are needed. Each step depends on the previous two — use two variables for O(1) space instead of an O(n) array.

Solution Code

O(n) time
def climbStairs(n):
    if n <= 2:
        return n
    a, b = 1, 2
    for _ in range(3, n + 1):
        a, b = b, a + b
    return b

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Climbing Stairs 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? This is the Fibonacci sequence in disguise.

How do you solve Climbing Stairs?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Climbing Stairs?

Climbing Stairs is asked at Google, Meta, Amazon, Microsoft, Apple, Adobe, Walmart, Bloomberg. It is a easy difficulty problem.

What are common mistakes on Climbing Stairs?
  • Not recognizing this as the Fibonacci sequence. `ways(n)` = `ways(n-1)` + `ways(n-2)` with base cases `ways(1)` = 1, `ways(2)` = 2.
  • Using recursion without memoization. That's `O(2^n)` and will time out. Use iterative DP or memoization.
  • Forgetting the base cases. `ways(1)` = 1, `ways(2)` = 2. Without these, the recurrence doesn't work.
  • Not handling `n = 0`. The problem says n >= 1, but if `n = 0`, return 1 (one way to stay at the top).
  • Using a full DP array when only two variables are needed. Each step depends on the previous two — use two variables for `O(1)` space instead of an `O(n)` array.