Challenge - 5 Problems
Memoization Mastery
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 uses memoization to calculate Fibonacci numbers?
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
Recall that Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8, 13...
✗ Incorrect
The function calculates Fibonacci numbers using memoization. fib(6) returns the 6th Fibonacci number which is 13.
❓ Predict Output
intermediateMemoized Factorial Output
What will be printed after running this memoized factorial function?
DSA Javascript
function factorial(n, memo = {}) {
if (n <= 1) return 1;
if (memo[n]) return memo[n];
memo[n] = n * factorial(n - 1, memo);
return memo[n];
}
console.log(factorial(5));Attempts:
2 left
💡 Hint
Factorial of 5 is 5 * 4 * 3 * 2 * 1.
✗ Incorrect
The function uses memoization to calculate factorial efficiently. factorial(5) = 120.
🔧 Debug
advancedIdentify the Error in Memoized Fibonacci
What error will this code produce when run?
DSA Javascript
function fib(n, memo = {}) {
if (n <= 1) return n;
if (memo[n]) return memo[n];
memo[n] = fib(n - 1) + fib(n - 2, memo);
return memo[n];
}
console.log(fib(5));Attempts:
2 left
💡 Hint
Check how the memo object is passed in recursive calls.
✗ Incorrect
The first recursive call fib(n - 1) does not pass memo, so memo is undefined inside that call, causing a TypeError when accessing memo[n].
❓ Predict Output
advancedOutput of Memoized Climbing Stairs Problem
What is the output of this memoized function that counts ways to climb stairs?
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];
}
console.log(climbStairs(5));Attempts:
2 left
💡 Hint
Number of ways to climb 5 stairs with 1 or 2 steps at a time.
✗ Incorrect
The function counts ways to climb stairs using memoization. climbStairs(5) = 8 ways.
🧠 Conceptual
expertMemoization Impact on Recursive Time Complexity
Which statement best describes how memoization changes the time complexity of a naive recursive Fibonacci function?
Attempts:
2 left
💡 Hint
Think about how many times each Fibonacci number is calculated with and without memoization.
✗ Incorrect
Memoization stores results of subproblems, so each is computed once, reducing exponential calls to linear time.
