Menu
Coddy logo textTech

Stair Climbing

Lesson 1 of 3 in Coddy's Interview Coding Challenges - Pack VI course.

challenge icon

Challenge

Easy

How many unique methods can you use to climb to the top of a staircase with n steps if you are only allowed to climb 1 or 2 steps at a time?


Example 1:

Input: n = 2

Output: 2

Explanation: To climb to the top of a staircase with 2 steps, you can either climb 1 step twice or climb 2 steps at once. Thus, there are 2 distinct ways to climb to the top.


Example 2:

Input: n = 3

Output: 3

Explanation: To climb to the top of a staircase with 3 steps, you can climb 1 step three times, climb 1 step and then 2 steps, or climb 2 steps and then 1 step. Thus, there are 3 distinct ways to climb to the top.


Example 3:

Input: n = 4

Output: 5

Explanation: To climb to the top of a staircase with 4 steps, here are 5 the options: 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 1, 2, 2. Where each number represent a climb of 1 or 2 steps at a time, and the order matters.

Try it yourself

int climb_stairs(int n) {
    // Write code here
}

All lessons in Interview Coding Challenges - Pack VI