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
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
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
- Write out every way for n = 1, 2, 3 and 4 by hand and compare the counts.
- 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.
- 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.