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 JavaScript code that calculates Fibonacci numbers using memoization?
DSA Javascript
function fib(n, memo = {}) {
if (n <= 1) return n;
if (memo[n]) return memo[n];
memo[n] = fib(n - 1, memo) + fib(n - 2, memo);
return memo[n];
}
console.log(fib(6));Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8, 13...
✗ Incorrect
The function calculates fib(6) which is the 6th Fibonacci number starting from 0. The sequence is 0,1,1,2,3,5,8... so fib(6) = 8.
❓ Predict Output
intermediateMemoization Cache Content After Execution
After running the following code, what is the content of the memo object?
DSA Javascript
function climbStairs(n, memo = {}) {
if (n <= 2) return n;
if (memo[n]) return memo[n];
memo[n] = climbStairs(n - 1, memo) + climbStairs(n - 2, memo);
return memo[n];
}
climbStairs(4);Attempts:
2 left
💡 Hint
Check which values are stored during recursive calls for n=4.
✗ Incorrect
The memo stores results for n=3 and n=4 only because base cases (n=1,2) return directly without memo storage.
🔧 Debug
advancedIdentify the Error in Memoized Factorial
What error will the following code produce when calling factorial(5)?
DSA Javascript
function factorial(n, memo = {}) {
if (n === 0) return 1;
if (memo[n]) return memo[n];
memo[n] = n * factorial(n - 1, memo);
return memo[n];
}
factorial(5);Attempts:
2 left
💡 Hint
Check the recursive call parameters carefully.
✗ Incorrect
There is no error. The recursive call misses passing memo, but default parameter provides {}, so it computes correctly without shared memoization, returning 120.
🚀 Application
advancedNumber of Ways to Make Change with Memoization
What is the output of the following code that calculates the number of ways to make change for amount 5 using coins [1,2,5] with memoization?
DSA Javascript
function change(amount, coins, index = 0, memo = {}) { const key = amount + '-' + index; if (amount === 0) return 1; if (amount < 0 || index === coins.length) return 0; if (memo[key]) return memo[key]; memo[key] = change(amount - coins[index], coins, index, memo) + change(amount, coins, index + 1, memo); return memo[key]; } console.log(change(5, [1,2,5]));
Attempts:
2 left
💡 Hint
Count all combinations of coins that sum to 5.
✗ Incorrect
There are 4 ways: {1x5}, {2x2 + 1x1}, {1x2 + 3x1}, {5x1}.
🧠 Conceptual
expertMemoization Cache Size in Top-Down DP
In a top-down memoized solution for the Fibonacci sequence, what is the maximum number of unique entries stored in the memo object when calculating fib(n)?
Attempts:
2 left
💡 Hint
Consider how many different arguments are passed to the recursive function.
✗ Incorrect
The memo stores results for all integers from 0 up to n, so total entries are n+1.
