You are climbing a staircase that has steps. Each time you can either take 1 or 2 steps. In how many distinct ways can you climb to the top? This is one of the most famous and foundational problems in computer science.
Asked by nearly every major tech company (Apple, Amazon, Google, etc.), "Climbing Stairs" is the ultimate test of your understanding of Recursion and Dynamic Programming. It demonstrates how a problem that looks complex can be reduced to a simple mathematical sequence (Fibonacci). It evaluations your ability to identify overlapping subproblems and optimize your code from to .
The pattern is Dynamic Programming or Memoization. To reach step , you must have come from either step (with a 1-step move) or step (with a 2-step move). Therefore, . This is exactly the Fibonacci sequence. You can solve it iteratively using two variables to achieve space.
The most common mistake is using naive recursion without memoization, which leads to exponential time complexity and a "Time Limit Exceeded" error. Another mistake is not recognizing the base cases correctly ( is 1 way, is 2 ways). Some might also struggle with integer overflow if is very large (though standard 32-bit or 64-bit integers usually suffice for interview constraints).
This is the "Hello World" of Dynamic Programming. Master the transition from recursion -> memoization -> iterative DP -> space-optimized DP. If you can explain this progression, you've demonstrated a core competency in algorithm design.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| N-th Tribonacci Number | Easy | Solve | |
| Least Operators to Express Number | Hard | Solve | |
| Fibonacci Number | Easy | Solve | |
| Integer Break | Medium | Solve | |
| 2 Keys Keyboard | Medium | Solve |