Unit 10 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
n
is 0?
HAPPY CASE
Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top: (1 step + 1 step) or (2 steps).
HAPPY CASE
Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top: (1 step + 1 step + 1 step), (1 step + 2 steps), or (2 steps + 1 step).
EDGE CASE
Input: n = 0
Output: 1
Explanation: There is one way to climb 0 steps (by not climbing at all).
EDGE CASE
Input: n = 1
Output: 1
Explanation: There is only one way to climb 1 step (by taking 1 step).
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Dynamic Programming problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use dynamic programming to store the number of ways to reach each step. The number of ways to reach step i
is the sum of the ways to reach steps i-1
and i-2
.
1) If n is 0 or 1, return 1.
2) Initialize two variables, prev2 and prev1, to 1 (ways to reach steps 0 and 1).
3) Iterate from 2 to n:
a) Calculate the current number of ways as the sum of prev1 and prev2.
b) Update prev2 to prev1.
c) Update prev1 to current.
4) Return prev1.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def climb_stairs(n):
if n == 0:
return 1
elif n == 1:
return 1
prev2 = 1
prev1 = 1
for i in range(2, n + 1):
current = prev1 + prev2
prev2 = prev1
prev1 = current
return prev1
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of steps.
O(N)
because we need to iterate through the steps.O(1)
because we only use a constant amount of extra space.