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 Java code that calculates the number of ways to climb 4 stairs?
DSA Java
public class ClimbingStairs { public static int climbStairs(int n) { if (n <= 2) return n; return climbStairs(n - 1) + climbStairs(n - 2); } public static void main(String[] args) { System.out.println(climbStairs(4)); } }
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 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 Java code that uses memoization to calculate ways to climb 5 stairs?
DSA Java
import java.util.HashMap; public class ClimbingStairsMemo { private static HashMap<Integer, Integer> memo = new HashMap<>(); public static int climbStairs(int n) { if (n <= 2) return n; if (memo.containsKey(n)) return memo.get(n); int result = climbStairs(n - 1) + climbStairs(n - 2); memo.put(n, result); return result; } public static void main(String[] args) { System.out.println(climbStairs(5)); } }
Attempts:
2 left
💡 Hint
Use the formula ways(n) = ways(n-1) + ways(n-2) and remember ways(1)=1, ways(2)=2.
✗ Incorrect
For 5 stairs, ways = ways(4) + ways(3) = 5 + 3 = 8.
🔧 Debug
advancedIdentify the Logical Error in Climbing Stairs Code
What is the output of this Java code snippet? Identify the logical error causing the output.
DSA Java
public class ClimbingStairsBug { public static int climbStairs(int n) { if (n == 1) return 1; if (n == 2) return 2; return climbStairs(n - 2) + climbStairs(n - 3); } public static void main(String[] args) { System.out.println(climbStairs(4)); } }
Attempts:
2 left
💡 Hint
Check the recursive calls and base cases carefully.
✗ Incorrect
The code incorrectly calls climbStairs(n-2) + climbStairs(n-3) instead of climbStairs(n-1) + climbStairs(n-2), so it returns 3 for input 4.
🧠 Conceptual
advancedTime Complexity of Climbing Stairs Recursive Solution
What is the time complexity of the naive recursive solution for the climbing stairs problem without memoization?
Attempts:
2 left
💡 Hint
Consider how many calls are made for each step.
✗ Incorrect
The naive recursive solution calls itself twice for each n, leading to exponential growth in calls, O(2^n).
🚀 Application
expertNumber of Ways to Climb Stairs with Steps 1, 3, or 5
Given you can climb stairs by taking 1, 3, or 5 steps at a time, what is the number of ways to climb 6 stairs? Use the code below to find the output.
DSA Java
public class ClimbingStairsVariant { public static int climbStairs(int n) { if (n == 0) return 1; if (n < 0) return 0; return climbStairs(n - 1) + climbStairs(n - 3) + climbStairs(n - 5); } public static void main(String[] args) { System.out.println(climbStairs(6)); } }
Attempts:
2 left
💡 Hint
Break down the problem by subtracting 1, 3, or 5 steps and summing ways.
✗ Incorrect
The function sums ways to climb n-1, n-3, and n-5 stairs. For n=6, total ways = 8.
