Jump to section

Climbing Stairs asks how many different ways you can climb a staircase of n steps if each move is one step or two. The number of ways to reach a step equals the ways to reach the step below it plus the ways to reach the step two below, so you can build the answer up from the bottom keeping just two numbers. It runs in O(n) time and O(1) space.

The Climbing Stairs Problem

A staircase has n steps. Each move climbs either one step or two. Return the number of distinct ways to reach the top. Two ways are different if the sequence of moves is different.

Example 1

Input: n = 3
Output: 3
Why: the three ways are 1+1+1, 1+2 and 2+1.

Example 2

Input: n = 5
Output: 8
Why: list a few small cases (1, 2, 3, 5, 8) and a pattern appears: each answer is the sum of the two before it.

Assumptions to confirm

  • Is n at least 1? (What should n = 0 return? Many definitions say 1, the empty climb, but ask.)
  • How large can n get? (The answer grows exponentially, so very large n can overflow 32-bit integers.)
  • Are the allowed step sizes always 1 and 2?

Start With Plain Recursion

The last move onto step n is either a single step from n - 1 or a double step from n - 2. So the ways to reach n are ways(n - 1) + ways(n - 2), with ways(1) = 1 and ways(2) = 2. A recursive function that follows this definition is correct, but each call makes two more calls, so it takes exponential time: roughly O(2โฟ).

Say why it is slow: ways(n - 2) is computed again inside ways(n - 1), and the same smaller values are recalculated over and over. Storing each answer the first time you compute it (memoisation) brings it to O(n) time and O(n) space. Building the values from the bottom up, keeping only the last two, brings the space down to O(1).

Ready to put this into practice?

Practice this with MockIF →

Still applying for jobs? Let Auto Apply do it

The Optimal Approach: Build Up From the Bottom

1

Handle the small cases

If n is 1 or 2, the answer is n.

2

Keep the last two answers

Start with the ways to reach step 2 (2) and step 1 (1).

3

Walk up from step 3 to n

The ways to reach the current step are the sum of the last two values.

4

Slide the window

The value for the previous step becomes the value for two steps back, and the new value becomes the previous step.

5

Return the last value

After the loop, it holds the ways to reach step n.

Climbing Stairs Solution in Python and TypeScript

Both versions keep only two numbers, which is the bottom-up form of the recurrence ways(n) = ways(n - 1) + ways(n - 2).

Python

def climb_stairs(n: int) -> int:
    if n <= 2:
        return n
    one_back, two_back = 2, 1  # ways to reach step 2 and step 1
    for _ in range(3, n + 1):
        one_back, two_back = one_back + two_back, one_back
    return one_back

TypeScript

function climbStairs(n: number): number {
  if (n <= 2) return n;
  let oneBack = 2; // ways to reach step 2
  let twoBack = 1; // ways to reach step 1
  for (let step = 3; step <= n; step++) {
    const ways = oneBack + twoBack;
    twoBack = oneBack;
    oneBack = ways;
  }
  return oneBack;
}

The sequence 1, 2, 3, 5, 8, 13 is the Fibonacci sequence shifted by one. You do not need to know that to solve the problem, but noticing it shows you understand the recurrence.

Ready to put this into practice?

Practice this with MockIF →

Still applying for jobs? Let Auto Apply do it

Time and Space Complexity

ApproachTimeSpaceWhen to use it
Plain recursionO(2โฟ)O(n) call stackOnly to explain the recurrence
Recursion with memoisationO(n)O(n)A natural step from the recursive version
Bottom-up, two variablesO(n)O(1)The expected interview answer

How to Talk Through Climbing Stairs in an Interview

A strong spoken answer follows the same five beats every time:

  1. Clarify: "Only one or two steps at a time? What should n = 0 return? How large can n be?"
  2. Brute force: "The last move comes from n - 1 or n - 2, so ways(n) = ways(n - 1) + ways(n - 2). Plain recursion is exponential because it repeats work."
  3. Optimise: "Each value depends only on the two before it, so I can build up from the bottom and keep just two numbers."
  4. Code: name the variables after what they mean, such as ways to reach one step back and two steps back.
  5. Test: check n = 1, 2, 3 and 5 against values you listed by hand. State O(n) time and O(1) space.

This problem is a common first test of dynamic programming. Going from recursion to memoisation to two variables, and explaining each step, is exactly the reasoning interviewers want to see.

Ready to put this into practice?

Practice this with MockIF →

Still applying for jobs? Let Auto Apply do it

Edge Cases and Common Follow-Ups

n = 1 and n = 2

Return n directly so the loop setup is never wrong for tiny inputs.

Large n

The count grows quickly. In languages with 32-bit integers, ask whether to return the answer modulo a number.

Steps of 1, 2 or 3

Add the last three values instead of two: ways(n) = ways(n - 1) + ways(n - 2) + ways(n - 3).

A cost for each step

Minimum cost to climb is the same recurrence with min and an added cost instead of a sum.

Some steps are broken

Set the ways for a broken step to 0 and carry on; the recurrence does the rest.

Practise Climbing Stairs in a Mock Coding Round

Climbing Stairs is short enough that the code is rarely the problem. What interviewers listen for is whether you can derive the recurrence yourself and explain why plain recursion is slow. That reasoning is much easier to show if you have practised saying it out loud.

MockIF runs mock coding interviews with a voice AI interviewer in a real in-browser editor, in Python and TypeScript. It runs your code against tests, asks you to explain your approach, adds follow-up questions the way a real interviewer would, and then scores your answer and gives specific feedback. Each session costs 2 credits. It is free to start with 5 credits, then monthly or 3-monthly plans, or one-off credit packs (see pricing).

Frequently Asked Questions

How do you solve Climbing Stairs?
The ways to reach step n are the ways to reach step n - 1 plus the ways to reach step n - 2, because the last move is one or two steps. Build the values up from step 1, keeping only the last two, for O(n) time and O(1) space.
Why is Climbing Stairs a dynamic programming problem?
Each answer depends on answers to smaller versions of the same problem, and those smaller answers overlap. Storing them instead of recomputing them is dynamic programming.
Is Climbing Stairs the Fibonacci sequence?
Yes, shifted by one position: the answers 1, 2, 3, 5, 8 follow the same rule that each value is the sum of the two before it.
What is the time complexity of Climbing Stairs?
O(n) time and O(1) space with the bottom-up solution. Plain recursion is exponential, and recursion with memoisation is O(n) time and O(n) space.

Stop Preparing in Your Head. Start Practicing Out Loud.

Drop your resume, add a job description, and get a mock interview like the real thing.

Start Free Mock Interview →
No credit card required. 5 free credits to start.

Still applying for jobs? Let Auto Apply do it for you.

Try Auto Apply →