Skip to content
CodeKruPro

Climbing stairs: count the ways

Coding problemEasyAsked in: Adobe, Amazon, Flipkart, Intel, Microsoft, Morgan Stanley

A staircase has n steps. Each move climbs either one step or two steps. Return the number of different sequences of moves that reach the top exactly.

For n = 3 there are three ways: 1+1+1, 1+2 and 2+1.

Examples

Example 1
Input: n = 2
Output: 2
1+1 or 2.
Example 2
Input: n = 3
Output: 3
Example 3
Input: n = 1
Output: 1

Constraints

  • 1 <= n <= 45 (the answer fits in a 32-bit integer)

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

to answer and keep your progress. Reading is free for everyone.