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 uses memoization to compute Fibonacci numbers?
DSA Java
import java.util.*; public class Main { static Map<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
Recall the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13...
✗ Incorrect
The code computes fib(6) using memoization. fib(6) = fib(5) + fib(4) = 5 + 3 = 8.
❓ Predict Output
intermediateMemoized Climbing Stairs Output
What is the output of this Java code that uses memoization to count ways to climb stairs?
DSA Java
import java.util.*; public class Main { static Map<Integer, Integer> memo = new HashMap<>(); public static int climbStairs(int n) { if (n <= 1) return 1; if (memo.containsKey(n)) return memo.get(n); int ways = climbStairs(n - 1) + climbStairs(n - 2); memo.put(n, ways); return ways; } public static void main(String[] args) { System.out.println(climbStairs(4)); } }
Attempts:
2 left
💡 Hint
Number of ways to climb 4 stairs with 1 or 2 steps at a time.
✗ Incorrect
Ways to climb 4 stairs = ways(3) + ways(2) = 3 + 2 = 5.
🔧 Debug
advancedIdentify the Bug in Memoized Fibonacci
What error will this Java code produce when run?
DSA Java
import java.util.*; public class Main { static Map<Integer, Integer> memo = new HashMap<>(); public static int fib(int n) { if (n == 0 || n == 1) return n; if (memo.containsKey(n)) return memo.get(n); int result = fib(n - 1) + fib(n - 3); memo.put(n, result); return result; } public static void main(String[] args) { System.out.println(fib(5)); } }
Attempts:
2 left
💡 Hint
Check recursive calls and base cases carefully.
✗ Incorrect
fib(n) calls fib(n-3) without base case for n<0, causing infinite recursion and StackOverflowError.
❓ Predict Output
advancedOutput of Memoized Coin Change Count
What is the output of this Java code that counts ways to make change using memoization?
DSA Java
import java.util.*; public class Main { static Map<String, Integer> memo = new HashMap<>(); public static int countWays(int[] coins, int n, int index) { if (n == 0) return 1; if (n < 0 || index == coins.length) return 0; String key = n + "," + index; if (memo.containsKey(key)) return memo.get(key); int ways = countWays(coins, n - coins[index], index) + countWays(coins, n, index + 1); memo.put(key, ways); return ways; } public static void main(String[] args) { int[] coins = {1, 2, 5}; System.out.println(countWays(coins, 5, 0)); } }
Attempts:
2 left
💡 Hint
Count all combinations of coins to make 5.
✗ Incorrect
Ways to make 5 with {1,2,5} are 4: {1,1,1,1,1}, {1,1,1,2}, {1,2,2}, {5}.
🧠 Conceptual
expertMemoization Cache Size for Top-Down DP
Consider a top-down memoized DP solution for a problem with input parameter n. If the recursive function calls itself with parameters from 0 up to n, what is the maximum number of entries stored in the memoization cache?
Attempts:
2 left
💡 Hint
Think about how many unique states the function can have.
✗ Incorrect
If the function parameter ranges from 0 to n, the memo cache stores at most one entry per unique parameter value, so O(n).
