Challenge - 5 Problems
Master of Overlapping Subproblems and Optimal Substructure
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
Recall the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13...
✗ Incorrect
The code calculates fib(6). The Fibonacci sequence starts with fib(0)=0, fib(1)=1, so fib(6) = 8.
❓ Predict Output
intermediateOutput of Recursive Coin Change Count
What is the output of this Java code that counts ways to make change using recursion?
DSA Java
public class CoinChange { public static int countWays(int[] coins, int n, int sum) { if (sum == 0) return 1; if (sum < 0 || n <= 0) return 0; return countWays(coins, n - 1, sum) + countWays(coins, n, sum - coins[n - 1]); } public static void main(String[] args) { int[] coins = {1, 2, 3}; System.out.println(countWays(coins, coins.length, 4)); } }
Attempts:
2 left
💡 Hint
Think about all combinations of coins that sum to 4.
✗ Incorrect
The code counts the number of combinations (order doesn't matter) to make 4 using unlimited coins {1,2,3}. The total ways are 4.
🔧 Debug
advancedIdentify the error in this memoized Fibonacci code
What error will this Java code produce when run?
DSA Java
import java.util.HashMap; public class FibMemoError { private static HashMap<Integer, Integer> memo = new HashMap<>(); public static int fib(int n) { if (n == 0) return 0; if (n == 1) return 1; 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(-1)); } }
Attempts:
2 left
💡 Hint
Consider what happens when fib is called with negative input.
✗ Incorrect
The code does not handle negative input, so fib(-1) calls fib(-2) and so on, causing infinite recursion and StackOverflowError.
🧠 Conceptual
advancedWhich property defines Optimal Substructure?
Which statement best describes the property of optimal substructure in dynamic programming?
Attempts:
2 left
💡 Hint
Optimal substructure means the best solution uses best solutions of parts.
✗ Incorrect
Optimal substructure means the optimal solution to a problem can be constructed from optimal solutions of its subproblems.
🚀 Application
expertNumber of Unique Paths in a Grid with Memoization
What is the output of this Java code that calculates the number of unique paths from top-left to bottom-right in a 3x3 grid using memoization?
DSA Java
import java.util.HashMap; public class UniquePaths { private static HashMap<String, Integer> memo = new HashMap<>(); public static int countPaths(int r, int c) { if (r == 0 && c == 0) return 1; if (r < 0 || c < 0) return 0; String key = r + "," + c; if (memo.containsKey(key)) return memo.get(key); int paths = countPaths(r - 1, c) + countPaths(r, c - 1); memo.put(key, paths); return paths; } public static void main(String[] args) { System.out.println(countPaths(2, 2)); } }
Attempts:
2 left
💡 Hint
Count paths moving only right or down in a 3x3 grid (indices 0 to 2).
✗ Incorrect
The number of unique paths from (0,0) to (2,2) moving only right or down is 6.
