Climbing Stairs
Table of Contents + β
This one looks like a simple counting puzzle. But the interviewer is not really asking about stairs. They want to see if you can spot a pattern, write the rule behind it, and then make that rule run fast. So if you can see that this follows the same sum-of-the-previous-two pattern as Fibonacci, you are showing them you understand dynamic programming. Dynamic programming means you break a big problem into smaller ones, solve each small one once, and reuse the answers.
πͺ The Problem
You are standing at the bottom of a staircase with n steps. Each time you move, you can climb either 1 step or 2 steps. The question is simple: how many different ways can you reach the top?
Input: n = 3Output: 3
The 3 ways are: 1 + 1 + 1 1 + 2 2 + 1See, the order matters here. Going 1 then 2 is a different way from going 2 then 1. So we count both.
π€ First Thought: Just Try Every Path
The first idea most people get is to try every possible path. From the bottom, you take 1 step or 2 steps. Then from there you again take 1 or 2. You keep going until you reach the top, and you count every full path that lands exactly on step n.
This works, but it is slow. The same small staircase gets solved again and again. To reach step 5, you solve step 3 many times over. The work roughly doubles with each extra step, so the time blows up quickly. So for a large n, this plain try-everything method becomes too slow to use.
π‘ The Better Idea: Find the Pattern
Now think about the very last move you made to reach the top step n. There are only two ways that last move could have happened.
- Your last move was a single step. That means you were standing on step
n-1just before. - Your last move was a double step. That means you were standing on step
n-2just before.
So every way to reach step n ends with one of those two cases. Nothing else is possible. That gives us a clean rule:
ways(n) = ways(n-1) + ways(n-2)Look at that rule closely. Each value is the sum of the two values before it. That is exactly the pattern behind the Fibonacci sequence. Fibonacci is a number list where every number is the sum of the two numbers before it. Our series here is 1, 2, 3, 5, 8, which is Fibonacci shifted by one. So climbing stairs follows the same sum-of-the-previous-two rule.
We only need the starting values. There is 1 way to reach step 1 (one single step). There are 2 ways to reach step 2 (1+1 or 2). After that, the rule fills in the rest.
Note
You do not need an array to store all the answers. To find the next count, you only ever look at the last two counts. So keep just two variables and slide them forward. That drops your extra memory from O(n) down to O(1).
πͺ Steps to Solve It
These steps turn the recurrence into a loop that uses only two variables.
- If
nis 1 or 2, the answer is justn. Return it directly. - Keep two variables:
prevforways(n-1)andprevPrevforways(n-2). Start them at 2 and 1. - Loop from step 3 up to step
n. - In each loop, add the two variables to get the current count.
- Slide the variables forward: the old
prevbecomesprevPrev, and the new count becomesprev. - After the loop ends,
prevholds the answer.
# Count distinct ways to climb n stairs using O(1) extra spacedef climb_stairs(n): if n <= 2: return n # base cases: 1 way for 1, 2 ways for 2 prev_prev = 1 # ways to reach step n-2 prev = 2 # ways to reach step n-1 for i in range(3, n + 1): curr = prev + prev_prev # the Fibonacci rule prev_prev = prev # slide the window forward prev = curr return prev
print(climb_stairs(3))print(climb_stairs(5))The output of the above code will be:
38β±οΈ Time and Space Complexity
Here is how the two approaches compare. The space-optimized version is the one you want to write in the interview.
| Approach | Time | Space |
|---|---|---|
| Try every path (plain recursion) | O(2^n) | O(n) |
| DP with an array | O(n) | O(n) |
| DP with two variables (optimal) | O(n) | O(1) |
Tip
When the interviewer hears you say βthis follows the Fibonacci patternβ, that is a strong signal. Say it out loud, then write the recurrence ways(n) = ways(n-1) + ways(n-2) on the board before you code. It shows you understood the pattern, not just memorized a solution.
π§© Key Takeaways
- β
The last move to the top came from either step
n-1or stepn-2, soways(n) = ways(n-1) + ways(n-2). - β That recurrence follows the same sum-of-the-previous-two pattern as Fibonacci (shifted by one), so the same trick solves both.
- β You only ever need the last two counts, so two variables are enough and space drops to O(1).
- β
The plain try-every-path method repeats work and runs in O(2^n), which is too slow for large
n. - β
The base cases are
1way forn = 1and2ways forn = 2.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
Why does climbing stairs follow the Fibonacci pattern?
Why: The final move lands from step n-1 or n-2, which gives the sum-of-the-two-before rule that defines Fibonacci.
- 2
What is the time complexity of the space-optimized solution?
Why: You loop once from step 3 to n, doing constant work each time, so the time is O(n).
- 3
Why can you use only two variables instead of a full array?
Why: ways(n) needs only ways(n-1) and ways(n-2), so keeping two variables is enough and space becomes O(1).
- 4
For n = 4, how many distinct ways are there to climb the stairs?
Why: ways(4) = ways(3) + ways(2) = 3 + 2 = 5.