FrontendInterviews.dev

Loading problem…

91. Climbing Stairs - Ways to Reach

Easy•
Acceptance: 100.00%
•
🔓3/3 Pro unlocks today

You're building a feature that calculates the number of ways to reach a destination. This is a classic dynamic programming problem that appears in many variations.

You are climbing a staircase. It takes n steps to reach the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Requirements

1. Basic Functionality

  • Calculate number of ways to reach top
  • Can take 1 or 2 steps at a time
  • Return total distinct ways
  • Handle edge cases (n=1, n=2)

Example Usage

climbStairs(2); // 2
// Ways: 1 step + 1 step, or 2 steps

climbStairs(3); // 3
// Ways: 1+1+1, 1+2, 2+1

climbStairs(4); // 5
// Ways: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1, 2+2

Real-World Context

This problem models real scenarios:

  • Path counting: Number of ways to reach destination
  • Combinatorics: Counting combinations
  • Dynamic programming: Classic DP problem
  • Fibonacci sequence: Same pattern as Fibonacci

Constraints

  • 1 <= n <= 45
  • Can only take 1 or 2 steps at a time
  • Return number of distinct ways