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 Code
What is the output of the following C++ code that calculates the number of ways to climb 4 stairs using recursion?
DSA C++
#include <iostream> using namespace std; int climbStairs(int n) { if (n <= 2) return n; return climbStairs(n - 1) + climbStairs(n - 2); } int main() { cout << climbStairs(4) << endl; return 0; }
Attempts:
2 left
💡 Hint
Think about how many ways to reach step 4 by summing ways to reach step 3 and step 2.
✗ Incorrect
The function climbStairs uses the Fibonacci pattern. For n=4, ways = climbStairs(3) + climbStairs(2) = 3 + 2 = 5.
❓ Predict Output
intermediateOutput of Climbing Stairs with Memoization
What is the output of this C++ code that uses memoization to calculate ways to climb 5 stairs?
DSA C++
#include <iostream> #include <vector> using namespace std; int climbStairsMemo(int n, vector<int>& memo) { if (n <= 2) return n; if (memo[n] != -1) return memo[n]; memo[n] = climbStairsMemo(n - 1, memo) + climbStairsMemo(n - 2, memo); return memo[n]; } int main() { int n = 5; vector<int> memo(n + 1, -1); cout << climbStairsMemo(n, memo) << endl; return 0; }
Attempts:
2 left
💡 Hint
Memoization stores results to avoid repeated calculations.
✗ Incorrect
For n=5, ways = climbStairs(4) + climbStairs(3) = 5 + 3 = 8.
🔧 Debug
advancedIdentify the Error in Iterative Climbing Stairs Code
What error will this C++ code produce when trying to calculate the number of ways to climb 3 stairs iteratively?
DSA C++
#include <iostream> using namespace std; int climbStairs(int n) { int dp[3]; dp[0] = 1; dp[1] = 2; for (int i = 2; i <= n; i++) { dp[i] = dp[i - 1] + dp[i - 2]; } return dp[n - 1]; } int main() { cout << climbStairs(3) << endl; return 0; }
Attempts:
2 left
💡 Hint
Check the size of the dp array and the loop index.
✗ Incorrect
The dp array has size 3 but the loop tries to access dp[3] when i=3, causing out of bounds access.
🧠 Conceptual
advancedNumber of Ways to Climb Stairs with Steps 1, 2, or 3
If you can climb stairs by taking 1, 2, or 3 steps at a time, how many ways are there to climb 4 stairs?
Attempts:
2 left
💡 Hint
Count all sequences of 1, 2, and 3 steps that sum to 4.
✗ Incorrect
Ways to climb 4 stairs with steps 1,2,3 are: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1, 2+2, 1+3, 3+1 totaling 7 ways.
🚀 Application
expertMinimum Steps to Reach Top with Variable Step Sizes
Given an array steps = {1, 3, 5}, representing allowed step sizes, what is the minimum number of steps to climb 11 stairs?
Attempts:
2 left
💡 Hint
Try to use the largest steps first and check combinations.
✗ Incorrect
One minimum way: 5 + 5 + 1 = 3 steps is valid because 5+5=10 +1=11 with 3 steps total.
