Challenge - 5 Problems
Climbing Stairs Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Climbing Stairs Recursive Function
What is the output of the following Go code that calculates the number of ways to climb 4 stairs?
DSA Go
package main import "fmt" func climbStairs(n int) int { if n <= 2 { return n } return climbStairs(n-1) + climbStairs(n-2) } func main() { fmt.Println(climbStairs(4)) }
Attempts:
2 left
💡 Hint
Think about how many ways to climb 3 stairs and 2 stairs add up for 4 stairs.
✗ Incorrect
The function uses recursion where climbStairs(4) = climbStairs(3) + climbStairs(2). climbStairs(3) = 3 and climbStairs(2) = 2, so total is 5.
🧠 Conceptual
intermediateUnderstanding Base Cases in Climbing Stairs
Why is it important to have the base cases 'if n <= 2 return n' in the climbing stairs recursive function?
Attempts:
2 left
💡 Hint
Base cases prevent the function from calling itself forever.
✗ Incorrect
Base cases provide the simplest answers and stop the recursion from continuing infinitely.
❓ Predict Output
advancedOutput of Climbing Stairs with Memoization
What is the output of this Go code that uses memoization to calculate ways to climb 5 stairs?
DSA Go
package main import "fmt" func climbStairsMemo(n int, memo map[int]int) int { if n <= 2 { return n } if val, ok := memo[n]; ok { return val } memo[n] = climbStairsMemo(n-1, memo) + climbStairsMemo(n-2, memo) return memo[n] } func main() { memo := make(map[int]int) fmt.Println(climbStairsMemo(5, memo)) }
Attempts:
2 left
💡 Hint
Memoization stores results to avoid repeated calculations.
✗ Incorrect
The function calculates climbStairs(5) = climbStairs(4) + climbStairs(3) = 5 + 3 = 8.
🔧 Debug
advancedIdentify the Error in Climbing Stairs Iterative Code
What error will this Go code produce when trying to calculate ways to climb 3 stairs?
DSA Go
package main import "fmt" func climbStairsIter(n int) int { if n <= 2 { return n } dp := make([]int, n) dp[0], dp[1] = 1, 2 for i := 2; i < n; i++ { dp[i] = dp[i-1] + dp[i-2] } return dp[n-1] } func main() { fmt.Println(climbStairsIter(3)) }
Attempts:
2 left
💡 Hint
Check the loop boundary and array size carefully.
✗ Incorrect
The loop tries to access dp[3] when dp slice length is 3 (indices 0 to 2), causing index out of range error.
🚀 Application
expertNumber of Ways to Climb Stairs with Steps 1, 3, or 5
Given you can climb stairs by 1, 3, or 5 steps at a time, how many ways are there to climb 5 stairs? Choose the correct output of this Go code.
DSA Go
package main import "fmt" func climbStairsVariant(n int) int { dp := make([]int, n+1) dp[0] = 1 for i := 1; i <= n; i++ { if i-1 >= 0 { dp[i] += dp[i-1] } if i-3 >= 0 { dp[i] += dp[i-3] } if i-5 >= 0 { dp[i] += dp[i-5] } } return dp[n] } func main() { fmt.Println(climbStairsVariant(5)) }
Attempts:
2 left
💡 Hint
Count all combinations using steps 1, 3, and 5 to reach 5.
✗ Incorrect
The dp array counts ways to reach each step by summing ways from steps 1, 3, and 5 behind. For 5 stairs, total ways are 5.
