Quick Overview

Count the distinct ordered sequences of one-step and two-step moves that climb a staircase of n steps and land exactly on the top step. This coding question tests building a counting argument over smaller staircases and handling answers that grow beyond 32-bit integers as n increases.

Count Ways to Climb n Stairs Using One-Step and Two-Step Moves

Company: Goldman Sachs

Role: Quantitative Researcher

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You stand at the bottom of a staircase with `n` steps. Each move climbs either one step or two steps. Count the number of distinct ways to reach the top, that is, to land exactly on step `n`. ### Function Signature ```python def count_climb_ways(n: int) -> int: ``` `n` is the number of steps in the staircase. ### Rules - A way is an ordered sequence of moves, each of size 1 or 2, whose sizes add up to exactly `n`. - Sequences that use the same moves in a different order are different ways: climbing 1 then 2 differs from climbing 2 then 1. - You may not climb past step `n`. ### Constraints - `1 <= n <= 70` - The answer exceeds `2^31 - 1` once `n >= 46`, but stays below `2^53` for every allowed `n`. Python integers handle this. ### Examples **Example 1** ```text Input: n = 3 Output: 3 ``` The ways are 1 + 1 + 1, 1 + 2 and 2 + 1. **Example 2** ```text Input: n = 5 Output: 8 ``` With no two-step move there is 1 way (five single steps). With one two-step move among four moves there are 4 ways. With two two-step moves among three moves there are 3 ways. **Example 3** ```text Input: n = 46 Output: 2971215073 ``` This answer is already larger than `2^31 - 1 = 2147483647`.

Overview: Count the distinct ordered sequences of one-step and two-step moves that climb a staircase of n steps and land exactly on the top step. This coding question tests building a counting argument over smaller staircases and handling answers that grow beyond 32-bit integers as n increases.

Read the full Goldman Sachs Quantitative Researcher interview experience this question came from

You stand at the bottom of a staircase with `n` steps. Each move climbs either one step or two steps. Return the number of distinct ways to reach the top, that is, to land exactly on step `n`. A way is an ordered sequence of moves, each of size 1 or 2, whose sizes add up to exactly `n`. Sequences that use the same moves in a different order are different ways: climbing 1 then 2 differs from climbing 2 then 1. You may not climb past step `n`. Implement `count_climb_ways(n)`, which returns this count as an integer. ### Constraints - `1 <= n <= 70` - The answer exceeds `2^31 - 1 = 2147483647` once `n >= 46` (for example, `n = 46` gives `2971215073`), but stays below `2^53` for every allowed `n`. Return a 64-bit integer: `long` in Java and `long long` in C++. A JavaScript number holds every answer exactly. ### Examples **Example 1** ```text Input: n = 3 Output: 3 ``` The ways are 1 + 1 + 1, 1 + 2 and 2 + 1. **Example 2** ```text Input: n = 5 Output: 8 ``` With no two-step move there is 1 way (five single steps). With one two-step move among four moves there are 4 ways. With two two-step moves among three moves there are 3 ways.

Constraints

  • 1 <= n <= 70
  • The answer exceeds 2^31 - 1 once n >= 46, but stays below 2^53 for every allowed n: use long in Java and long long in C++; JavaScript numbers are exact.

Examples

Input: (1,)

Expected Output: 1

Explanation: Minimum n: the only way is a single one-step move.

Input: (2,)

Expected Output: 2

Explanation: Smallest staircase that allows a two-step move: 1 + 1 and 2.

Hints

  1. Write out every way for n = 1, 2, 3 and 4 by hand and compare the counts.
  2. Order matters: 1 + 2 and 2 + 1 are different ways, so count sequences of moves, not just how many of each move size you use.
  3. Answers for n >= 46 do not fit in a 32-bit integer, so choose a result type that can hold values up to 2^53.

Loading coding console...

Show the approach

Approach

Let W(k) be the number of ordered move sequences that land exactly on step k. W(0) = 1 (the empty sequence) and W(1) = 1. For k >= 2, the last move of any valid sequence is either a one-step move from step k - 1 or a two-step move from step k - 2. These two groups do not overlap (their last moves differ) and together cover every sequence, and stripping the last move is a bijection onto the sequences for k - 1 and k - 2, so W(k) = W(k - 1) + W(k - 2). Because moves are ordered, 1 + 2 and 2 + 1 fall into different groups and are counted separately, as the rules require.

The algorithm keeps only the last two values. Invariant: before the i-th loop iteration (0-based), prev = W(i) and curr = W(i + 1). Each iteration replaces the pair with (W(i + 1), W(i) + W(i + 1)) = (W(i + 1), W(i + 2)). After n - 1 iterations curr = W(n). W(n) equals the Fibonacci number F(n + 1), with F(1) = F(2) = 1; returning F(n) instead is the classic off-by-one.

Edge cases: for n = 1 the loop runs zero times and returns 1. For n >= 46 the answer exceeds 2^31 - 1, so Java uses long and C++ uses long long; the largest answer, W(70) = 308061521170129, is below 2^53, so JavaScript numbers remain exact. Plain recursion without memoization takes exponential time and is too slow near n = 70.

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