The count for the next step is the sum of the counts for the two preceding positions.
Problem trace
Last steps of size one and two split paths into disjoint sets
Read it this way:For five stairs, five paths end with one step and three with two. These groups do not overlap, so their counts add to eight. After removing the final two-step move, how many stairs remain in every path of that group?
Sources and provenance
Full interview answer 20 min read
Count the different ordered ways to reach a staircase’s top using steps of size one or two.
Problem
Input. A nonnegative integer step_count, the number of steps between the ground and the top.
Output. Return the number of ordered sequences of one-step and two-step moves that reach the top exactly.
Rules. Moves cannot overshoot the top. Different move orders count separately.
For zero steps, count one way: take no moves.
Negative or noninteger step counts are outside the contract.
Example. For step_count = 3, the sequences are [1, 1, 1], [1, 2], and [2, 1]; return 3.
For step_count = 5, return 8.
Picture
Remove a path’s final move.
It leaves exactly one path to either the previous step or the step before that.
Appending that final move reverses the removal.
No path belongs to both final-move cases, so addition does not double-count.
Reference drawing
Five paths ending in one step and three ending in two make eight
Derivation
Each row totals five steps. The highlighted final move places it in exactly one group. Five ending in +1 plus three ending in +2 gives eight.
Read it this way:Five paths end with one step; three end with two steps. The disjoint groups sum to eight paths up five stairs. Can any path appear in both final-one and final-two groups?
Counts for steps 0 through 5 are 1,1,2,3,5,8.
The empty path gives step zero one way.
The pre-loop previous=0 is a sentinel, not that empty-path count.
Problem trace
Last steps of size one and two split paths into disjoint sets
Input and goalCount ways to reach step `n` using moves of one or two steps.
Initialize two rolling totalsStart with current=1 for the empty path and previous=0 as the pre-loop sentinel.
c10?1?2?3?4?5
inputn = 5pointersp: previous, c: currentrollingStateprevious=0, current=1dependencycurrent=ways(0)=1; previous is the pre-loop sentinel 0
c
0
input
n = 5
pointers
p: previous, c: current
Update for step 1Parallel assignment computes (previous, current) = (1, 0 + 1), so there is 1 way to reach step 1.
The task counts paths to position n, every final move has one of two fixed lengths, and paths ending with different final moves are disjoint.
Keep true
Before each iteration, previous and current are consecutive recurrence values. Parallel assignment shifts current into previous and stores previous + current as the complete count for the next step.
Reuse it
Classify solutions by their final decision: if every state can only arrive from a fixed small set of predecessor states, add those disjoint counts and retain only the dependency horizon.
Start with current=1 for the empty path and previous=0 as the pre-loop sentinel. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why can the two predecessor counts be added without counting the same path twice?
Reference drawing
Problem trace
Last steps of size one and two split paths into disjoint sets
Execution trace
Input and goalCount ways to reach step `n` using moves of one or two steps.
Initialize two rolling totalsStart with current=1 for the empty path and previous=0 as the pre-loop sentinel.
c10?1?2?3?4?5
inputn = 5pointersp: previous, c: currentrollingStateprevious=0, current=1dependencycurrent=ways(0)=1; previous is the pre-loop sentinel 0
c
0
input
n = 5
pointers
p: previous, c: current
Update for step 1Parallel assignment computes (previous, current) = (1, 0 + 1), so there is 1 way to reach step 1.
The task counts paths to position n, every final move has one of two fixed lengths, and paths ending with different final moves are disjoint.
Keep true
Before each iteration, previous and current are consecutive recurrence values. Parallel assignment shifts current into previous and stores previous + current as the complete count for the next step.
Reuse it
Classify solutions by their final decision: if every state can only arrive from a fixed small set of predecessor states, add those disjoint counts and retain only the dependency horizon.
Start with current=1 for the empty path and previous=0 as the pre-loop sentinel. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why can the two predecessor counts be added without counting the same path twice?
Read it this way:For five stairs, five paths end with one step and three with two. These groups do not overlap, so their counts add to eight. After removing the final two-step move, how many stairs remain in every path of that group?
After a positive step, previous holds the preceding step’s count and current holds this step’s count.
Their old values produce the next pair together.
Code
def climb_stairs(step_count: int) -> int: previous, current = 0, 1 for _ in range(step_count): previous, current = current, previous + current return current
Updates produce (1,1), (1,2), (2,3), (3,5), then (5,8).
Parallel assignment reads both old values before storing either new value.
Overwriting previous first would destroy an input to the addition.
Zero steps return 1.
The literal negative-input result is also 1, but negative steps are outside this task.
There are O(n) updates and O(1) integer slots.
Exact Python counts grow, so arithmetic cost and bit storage are not constant.
No path witness is retained.
Change the Valid Landings
New task: Some positive landing steps are blocked.
Picture and change: A blocked destination contributes zero paths.
Keep the empty-path base and the same disjoint final-move cases.
def stairs_with_blocks(step_count, blocked): if step_count < 0: return 0 blocked = set(blocked) counts = [0] * (step_count + 1) counts[0] = 1 for step in range(1, step_count + 1): if step not in blocked: counts[step] = counts[step - 1] if step >= 2: counts[step] += counts[step - 2] return counts[-1]
Check: With step 3 blocked, step 4 has two paths: 1,1,2 and 2,2.
Both bypass the blocked landing.
The starting position remains available.
Use when: Intermediate-state feasibility changes.
Change the Move Set
New task: Accept a collection of allowed integer jumps.
Picture and change: Each distinct positive jump defines one final-move case.
Discard nonpositive jumps and deduplicate values before counting.
def stairs_with_jumps(step_count, jumps): if step_count < 0: return 0 moves = sorted({jump for jump in jumps if jump > 0}) counts = [0] * (step_count + 1) counts[0] = 1 for step in range(1, step_count + 1): counts[step] = sum(counts[step - jump] for jump in moves if jump <= step) return counts[-1]
Check: Jumps [1,3] give three paths to step 4: 1,1,1,1, 1,3, 3,1.
Jump-first combination counting would answer a different question.
Both adaptations retain O(n) table slots; the jump variant scans the distinct move set per step.
Use when: The final-move cases change.
Transfer Check
Why does adding a three-step move break the two-count update?
Answer
The next count also needs the count three positions earlier.
Two saved counts no longer cover the dependency horizon.
Zero as the empty-path count would instead erase every positive count.