Challenge - 5 Problems
Memoization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Fibonacci with Memoization
What is the output of the following Java code that calculates Fibonacci numbers using memoization?
DSA Java
import java.util.HashMap; public class FibMemo { private static HashMap<Integer, Integer> memo = new HashMap<>(); public static int fib(int n) { if (n <= 1) return n; if (memo.containsKey(n)) return memo.get(n); int result = fib(n - 1) + fib(n - 2); memo.put(n, result); return result; } public static void main(String[] args) { System.out.println(fib(6)); } }
Attempts:
2 left
💡 Hint
Remember that Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8, 13...
✗ Incorrect
fib(6) = fib(5) + fib(4) = 5 + 3 = 8.
❓ Predict Output
intermediateMemoization Cache State After Calls
After running fib(5) using the memoized Fibonacci function below, what is the content of the memo cache?
DSA Java
import java.util.HashMap; public class FibMemo { private static HashMap<Integer, Integer> memo = new HashMap<>(); public static int fib(int n) { if (n <= 1) return n; if (memo.containsKey(n)) return memo.get(n); int result = fib(n - 1) + fib(n - 2); memo.put(n, result); return result; } public static void main(String[] args) { fib(5); System.out.println(memo); } }
Attempts:
2 left
💡 Hint
Base cases are not stored in memo, only computed results for n > 1 are cached.
✗ Incorrect
The memo cache stores results for n > 1 only. fib(0) and fib(1) return directly without caching.
🔧 Debug
advancedIdentify the Error in Memoized Factorial
What error will the following Java code produce when calling factorial(5)?
DSA Java
import java.util.HashMap; public class FactorialMemo { private static HashMap<Integer, Integer> memo = new HashMap<>(); public static int factorial(int n) { if (n == 0) return 1; if (memo.containsKey(n)) return memo.get(n); int result = n * factorial(n - 1); memo.put(n, result); return result; } public static void main(String[] args) { System.out.println(factorial(5)); } }
Attempts:
2 left
💡 Hint
Check base case and recursive calls carefully.
✗ Incorrect
The code correctly implements memoized factorial and prints 120 for factorial(5).
❓ Predict Output
advancedOutput of Memoized Climbing Stairs Problem
What is the output of the following Java code that uses memoization to count ways to climb stairs?
DSA Java
import java.util.HashMap; public class ClimbStairs { private static HashMap<Integer, Integer> memo = new HashMap<>(); public static int climb(int n) { if (n <= 1) return 1; if (memo.containsKey(n)) return memo.get(n); int ways = climb(n - 1) + climb(n - 2); memo.put(n, ways); return ways; } public static void main(String[] args) { System.out.println(climb(4)); } }
Attempts:
2 left
💡 Hint
Number of ways to climb 4 stairs with 1 or 2 steps at a time is Fibonacci-like.
✗ Incorrect
climb(4) = climb(3) + climb(2) = 3 + 2 = 5 ways.
🧠 Conceptual
expertMemoization Impact on Time Complexity
Which statement best describes how memoization affects the time complexity of a naive recursive Fibonacci function?
Attempts:
2 left
💡 Hint
Think about how many times each Fibonacci number is computed with and without memoization.
✗ Incorrect
Memoization stores results to avoid repeated calculations, reducing exponential calls to linear.
