Question presented to candidate: "You are climbing a staircase with n steps. Each move you climb either 1 or 2 steps. In how many distinct ways can you reach the top? Please write it in Python, start with whatever is simplest, then make it efficient."
What a strong answer should cover:
- The recurrence from the last move,
ways(n) = ways(n - 1) + ways(n - 2), and why the two cases do not overlap. - Correct base cases (
ways(0) = ways(1) = 1) and the link to Fibonacci. - Why plain recursion is exponential, and the progression memo β table β two variables (O(n) time, O(1) space).
- Generalising to any set of step sizes, or to min cost climbing stairs.
- Python specifics: tuple assignment for the two-variable shift,
intnever overflowing (exactclimb(92)), and the 1000-frame recursion limit that makes the memo version fail for large n.
Clarifying questions expected:
- "Are the moves exactly 1 or 2 steps, and does order matter?"
- "How large can n be β do I need 64-bit numbers or a modulo?"
Code / implementation expected: Yes β a short Python function, ideally the O(1)-space loop.