FreeCareerPath
Build the PrimitiveEasy

Climbing Stairs

Write `climb_stairs(n)` counting the distinct ways to climb `n` steps taking 1 or 2 at a time. climb_stairs(3) -> 3 (1+1+1, 1+2, 2+1) Ways to reach step n is ways(n-1) + ways(n-2) — it is Fibonacci wearing a costume. Say the recurrence out loud first. Naive recursion is exponential; two rolling variables are O(n) time and O(1) space.

What to expect: A timer starts when you begin. Edit the starter code, run it against the test suite as many times as you like, then finish when you're done. The reference solution and interviewer follow-up questions unlock only after you finish.

Log in to start this session

Timed sessions and your results for "Climbing Stairs" are saved to your account — logging in takes a few seconds.

Climbing Stairs — FreeCareerPath