Challenge - 5 Problems
Memoization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Memoized Fibonacci Function
What is the output of the following C++ code that uses memoization to compute Fibonacci numbers?
DSA C++
#include <iostream> #include <vector> using namespace std; int fib(int n, vector<int>& memo) { if (n <= 1) return n; if (memo[n] != -1) return memo[n]; memo[n] = fib(n-1, memo) + fib(n-2, memo); return memo[n]; } int main() { int n = 6; vector<int> memo(n+1, -1); cout << fib(n, memo) << endl; return 0; }
Attempts:
2 left
💡 Hint
Recall the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13...
✗ Incorrect
fib(0)=0, fib(1)=1, fib(2)=1, fib(3)=2, fib(4)=3, fib(5)=5, fib(6)=8. The code outputs 8.
🧠 Conceptual
intermediateMemoization Purpose in Top Down DP
What is the main purpose of using memoization in a top-down dynamic programming approach?
Attempts:
2 left
💡 Hint
Think about how recursion can repeat the same work multiple times.
✗ Incorrect
Memoization saves results of subproblems so that when the same subproblem occurs again, the stored result is used instead of recalculating.
🔧 Debug
advancedIdentify the Error in Memoized Climbing Stairs Code
What error will occur when running this C++ code for climbing stairs with memoization?
DSA C++
#include <iostream> #include <vector> using namespace std; int climbStairs(int n, vector<int>& memo) { if (n <= 1) return 1; if (memo[n] != 0) return memo[n]; memo[n] = climbStairs(n-1, memo) + climbStairs(n-2, memo); return memo[n]; } int main() { int n = 5; vector<int> memo(n+1); cout << climbStairs(n, memo) << endl; return 0; }
Attempts:
2 left
💡 Hint
Check how memo vector is initialized and how base cases are handled.
✗ Incorrect
The code initializes memo with zeros, so memo[n] != 0 check works. Base case returns 1 for n <= 1. climbStairs(5) returns 8.
❓ Predict Output
advancedOutput of Memoized Coin Change Ways
What is the output of this C++ code that uses memoization to count ways to make change?
DSA C++
#include <iostream> #include <vector> using namespace std; int countWays(int amount, const vector<int>& coins, int index, vector<vector<int>>& memo) { if (amount == 0) return 1; if (amount < 0 || index == coins.size()) return 0; if (memo[amount][index] != -1) return memo[amount][index]; int include = countWays(amount - coins[index], coins, index, memo); int exclude = countWays(amount, coins, index + 1, memo); memo[amount][index] = include + exclude; return memo[amount][index]; } int main() { int amount = 5; vector<int> coins = {1, 2, 5}; vector<vector<int>> memo(amount + 1, vector<int>(coins.size(), -1)); cout << countWays(amount, coins, 0, memo) << endl; return 0; }
Attempts:
2 left
💡 Hint
Count all combinations of coins that sum to 5.
✗ Incorrect
The ways to make 5 are: {5}, {2,2,1}, {2,1,1,1}, {1,1,1,1,1} totaling 4 ways.
🧠 Conceptual
expertWhy Memoization Can Use More Memory Than Bottom-Up DP
Why can memoization in top-down dynamic programming sometimes use more memory than bottom-up DP?
Attempts:
2 left
💡 Hint
Consider recursion stack and when memo entries are created.
✗ Incorrect
Memoization stores results as needed but recursion stack frames remain until calls return, increasing memory usage compared to iterative bottom-up DP.
