Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the result is already computed in the memo object.
DSA Javascript
if (memo.hasOwnProperty([1])) { return memo[n]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for a wrong variable name in memo.
Using memo instead of the key variable.
✗ Incorrect
We check if the memo object has the key 'n' to avoid recomputing the value.
2fill in blank
mediumComplete the code to store the computed result in the memo object.
DSA Javascript
memo[n] = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Storing the input 'n' instead of the computed result.
Trying to store the recursive call directly.
✗ Incorrect
We store the computed result in memo[n] to reuse it later.
3fill in blank
hardFix the error in the base case condition to stop recursion correctly.
DSA Javascript
if (n [1] 1) { return n; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which only covers n=1.
Using '!=' which causes infinite recursion.
✗ Incorrect
The base case for Fibonacci is when n is less than or equal to 1, return n.
4fill in blank
hardFill both blanks to complete the recursive calls with memoization.
DSA Javascript
const result = fib([1]) + fib([2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or multiplication instead of subtraction.
Using wrong indices causing wrong results.
✗ Incorrect
Fibonacci is sum of fib(n-1) and fib(n-2).
5fill in blank
hardFill all three blanks to complete the memoized Fibonacci function.
DSA Javascript
function fib(n, memo = {}) {
if (n [1] 1) {
return n;
}
if (memo.hasOwnProperty([2])) {
return memo[n];
}
const result = fib(n [3] 1, memo) + fib(n - 2, memo);
memo[n] = result;
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' in recursive call.
Checking wrong base case condition.
Using wrong memo key.
✗ Incorrect
Base case uses '<=', memo key is 'n', recursive call subtracts 1 from n.
