Challenge - 5 Problems
Climbing Stairs Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Climbing Stairs Recursive Function
What is the output of the following C code when n = 4?
DSA C
#include <stdio.h> int climbStairs(int n) { if (n <= 2) return n; return climbStairs(n - 1) + climbStairs(n - 2); } int main() { int result = climbStairs(4); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Think about how many ways to climb 3 steps and 2 steps add up for 4 steps.
✗ Incorrect
The function returns the number of ways to climb n stairs by summing ways to climb n-1 and n-2 stairs. For n=4, ways = climbStairs(3) + climbStairs(2) = 3 + 2 = 5.
❓ Predict Output
intermediate2:00remaining
Output of Climbing Stairs with Memoization
What is the output of this C code snippet when n = 5?
DSA C
#include <stdio.h> int memo[100] = {0}; int climbStairs(int n) { if (n <= 2) return n; if (memo[n] != 0) return memo[n]; memo[n] = climbStairs(n - 1) + climbStairs(n - 2); return memo[n]; } int main() { int result = climbStairs(5); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Memoization stores results to avoid repeated calculations.
✗ Incorrect
The memoized function calculates climbStairs(5) = climbStairs(4) + climbStairs(3) = 5 + 3 = 8.
🔧 Debug
advanced2:30remaining
Identify the Logical Error in Climbing Stairs Iterative Code
What is the output of this code snippet and what is the logical error?
DSA C
#include <stdio.h> int climbStairs(int n) { int a = 1, b = 1, c = 0; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return c; } int main() { int result = climbStairs(3); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Check the base cases and loop start index carefully.
✗ Incorrect
The code outputs 3 for n=3 which is correct. However, initial values a=1, b=1 cause the count to start from step 1 incorrectly, leading to confusion in interpretation.
🧠 Conceptual
advanced1:30remaining
Time Complexity of Climbing Stairs Recursive vs Dynamic Programming
Which option correctly describes the time complexity difference between the naive recursive and dynamic programming solutions for the climbing stairs problem?
Attempts:
2 left
💡 Hint
Think about how many times the recursive calls repeat work.
✗ Incorrect
Naive recursion repeats many calls leading to exponential time. Dynamic programming stores results and computes each subproblem once, leading to linear time.
🚀 Application
expert3:00remaining
Number of Ways to Climb Stairs with Variable Steps
Given you can climb 1, 3, or 5 steps at a time, what is the number of ways to climb 6 steps? Choose the correct output of the following code.
DSA C
#include <stdio.h> int climbVariableSteps(int n) { int dp[7] = {0}; dp[0] = 1; for (int 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]; } int main() { int result = climbVariableSteps(6); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Count ways by adding ways from steps 1, 3, and 5 behind.
✗ Incorrect
The dp array counts ways to reach each step by summing ways from i-1, i-3, and i-5. For n=6, dp[6] = dp[5] + dp[3] + dp[1] = 5 + 2 + 1 = 8.