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 C++ code that uses memoization to compute Fibonacci numbers?
DSA C++
#include <iostream> #include <vector> int fib(int n, std::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; std::vector<int> memo(n+1, -1); std::cout << fib(n, memo) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Recall that Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8, 13...
✗ Incorrect
The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, ... so fib(6) = 8.
❓ Predict Output
intermediateMemoization Cache State After Calls
After calling fib(5) with memo initialized to -1, what is the state of the memo vector?
DSA C++
#include <iostream> #include <vector> int fib(int n, std::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 = 5; std::vector<int> memo(n+1, -1); fib(n, memo); for (int i = 0; i <= n; ++i) { std::cout << memo[i] << " "; } std::cout << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Base cases n <= 1 return without storing in memo; only n > 1 values are stored.
✗ Incorrect
After fib(5), memo[0] = -1 and memo[1] = -1 (base cases not stored), memo[2]=1, memo[3]=2, memo[4]=3, memo[5]=5.
🔧 Debug
advancedIdentify the Runtime Error in Memoized Fibonacci
What runtime error will occur when running this code?
DSA C++
#include <iostream> #include <vector> int fib(int n, std::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 = 10; std::vector<int> memo(5, -1); // Incorrect size std::cout << fib(n, memo) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check if memo vector size matches the maximum n used in recursion.
✗ Incorrect
The memo vector size is 5 but fib(10) accesses memo[10], causing out-of-range access and segmentation fault.
🧠 Conceptual
advancedWhy Memoization Improves Recursive Fibonacci
Why does memoization improve the performance of the recursive Fibonacci function?
Attempts:
2 left
💡 Hint
Think about how recursion repeats calls for the same inputs.
✗ Incorrect
Memoization saves results of function calls so the same calculations are not done multiple times, reducing time complexity.
❓ Predict Output
expertOutput of Memoized Recursive Function with Custom Cache
What is the output of this C++ code that uses memoization with a custom cache for a modified Fibonacci sequence?
DSA C++
#include <iostream> #include <unordered_map> int fib(int n, std::unordered_map<int,int>& cache) { if (n <= 1) return n; if (cache.find(n) != cache.end()) return cache[n]; cache[n] = fib(n-1, cache) + 2 * fib(n-2, cache); return cache[n]; } int main() { int n = 4; std::unordered_map<int,int> cache; std::cout << fib(n, cache) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Calculate fib(4) using fib(n) = fib(n-1) + 2*fib(n-2) with base fib(0)=0, fib(1)=1.
✗ Incorrect
fib(2)=fib(1)+2*fib(0)=1+0=1; fib(3)=fib(2)+2*fib(1)=1+2=3; fib(4)=fib(3)+2*fib(2)=3+2=5.
