Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the base case for 1 stair.
DSA Go
func climbStairs(n int) int {
if n == 1 {
return [1]
}
return 0
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of 1 for the base case.
Returning n which is unnecessary here.
✗ Incorrect
When there is only 1 stair, there is exactly 1 way to climb it.
2fill in blank
mediumComplete the code to return the base case for 2 stairs.
DSA Go
func climbStairs(n int) int {
if n == 1 {
return 1
}
if n == 2 {
return [1]
}
return 0
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 instead of 2 for the base case of 2 stairs.
Returning n which is 2 but not explaining the reasoning.
✗ Incorrect
For 2 stairs, you can climb either 1 step + 1 step or 2 steps at once, so 2 ways.
3fill in blank
hardFix the error in the recursive return statement to calculate ways correctly.
DSA Go
func climbStairs(n int) int {
if n == 1 {
return 1
}
if n == 2 {
return 2
}
return climbStairs(n [1] 1) + climbStairs(n - 2)
}Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of - causing syntax error or wrong logic.
Using * or / which do not make sense here.
✗ Incorrect
We subtract 1 from n to get the previous step count, then add ways from n-2 steps.
4fill in blank
hardFill both blanks to create a map of stairs to ways using a loop.
DSA Go
func climbStairs(n int) int {
ways := make(map[int]int)
ways[1] = 1
ways[2] = 2
for i := 3; i <= n; i++ {
ways[i] = ways[i [1] 1] + ways[i [2] 2]
}
return ways[n]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using + or * instead of - inside the map keys.
Using division which is invalid here.
✗ Incorrect
We subtract 1 and 2 from i to get previous counts and sum their ways.
5fill in blank
hardFill all three blanks to create a slice and fill it with ways to climb stairs.
DSA Go
func climbStairs(n int) int {
dp := make([]int, n+1)
dp[1] = 1
dp[2] = 2
for i := 3; i <= n; i++ {
dp[i] = dp[i [1] 1] [2] dp[i [3] 2]
}
return dp[n]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of addition.
Using addition inside indices instead of subtraction.
✗ Incorrect
We subtract 1 and 2 from i to get previous dp values and add them to get current ways.
