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, taking 1 or 2 steps at a time?
DSA C#
using System; class Program { static int ClimbStairs(int n) { if (n <= 2) return n; return ClimbStairs(n - 1) + ClimbStairs(n - 2); } static void Main() { Console.WriteLine(ClimbStairs(4)); } }
Attempts:
2 left
💡 Hint
Think about how many ways to reach step 4 by summing ways to reach steps 3 and 2.
✗ Incorrect
The number of ways to climb 4 stairs is 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 find the number of ways to climb 5 stairs?
DSA C#
using System; using System.Collections.Generic; class Program { static Dictionary<int, int> memo = new Dictionary<int, int>(); static int ClimbStairs(int n) { if (n <= 2) return n; if (memo.ContainsKey(n)) return memo[n]; memo[n] = ClimbStairs(n - 1) + ClimbStairs(n - 2); return memo[n]; } static void Main() { Console.WriteLine(ClimbStairs(5)); } }
Attempts:
2 left
💡 Hint
Use the relation ways(n) = ways(n-1) + ways(n-2) and base cases.
✗ Incorrect
For n=5, ways = ways(4) + ways(3) = 5 + 3 = 8.
🔧 Debug
advancedIdentify the Error in Climbing Stairs Iterative Code
What error will this C# code produce when trying to calculate the number of ways to climb 3 stairs?
DSA C#
using System; class Program { static int ClimbStairs(int n) { int[] dp = new int[n + 1]; 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]; } static void Main() { Console.WriteLine(ClimbStairs(3)); } }
Attempts:
2 left
💡 Hint
Check the size of the dp array and the loop boundaries.
✗ Incorrect
The dp array has size n=3, but the loop tries to access dp[3], which is out of bounds.
❓ Predict Output
advancedOutput of Climbing Stairs Using Bottom-Up DP
What is the output of this C# code that uses bottom-up dynamic programming to find ways to climb 6 stairs?
DSA C#
using System; class Program { static int ClimbStairs(int n) { int[] dp = new int[n + 1]; dp[0] = 1; dp[1] = 1; for (int i = 2; i <= n; i++) { dp[i] = dp[i - 1] + dp[i - 2]; } return dp[n]; } static void Main() { Console.WriteLine(ClimbStairs(6)); } }
Attempts:
2 left
💡 Hint
Calculate dp array values step by step from 0 to 6.
✗ Incorrect
dp[6] = dp[5] + dp[4] = 8 + 5 = 13.
🧠 Conceptual
expertNumber of Ways to Climb Stairs with Steps 1, 2, or 3
If you can climb stairs taking 1, 2, or 3 steps at a time, how many ways are there to climb 4 stairs?
Attempts:
2 left
💡 Hint
Use the relation ways(n) = ways(n-1) + ways(n-2) + ways(n-3) with base cases.
✗ Incorrect
ways(4) = ways(3) + ways(2) + ways(1) = 4 + 2 + 1 = 7.
