Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the memo array with base cases for Fibonacci.
DSA Javascript
const fib = (n, memo = [[1], 1]) => { if (memo[n] !== undefined) return memo[n]; memo[n] = fib(n - 1, memo) + fib(n - 2, memo); return memo[n]; };
Attempts:
3 left
💡 Hint
Common Mistakes
Starting memo with [1, 1] instead of [0, 1]
Using negative numbers in memo initialization
✗ Incorrect
The Fibonacci sequence starts with 0 and 1, so the memo array should start with [0, 1].
2fill in blank
mediumComplete the code to check if the Fibonacci value is already computed in memo.
DSA Javascript
if (memo[[1]] !== undefined) return memo[n];
Attempts:
3 left
💡 Hint
Common Mistakes
Checking memo[0] or memo[1] instead of memo[n]
Using n-1 or other indices incorrectly
✗ Incorrect
We check if the Fibonacci number for n is already computed and stored in memo[n].
3fill in blank
hardFix the error in the recursive call to compute Fibonacci using memoization.
DSA Javascript
memo[n] = fib(n [1] 1, memo) + fib(n - 2, memo);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causing wrong recursion
Using '*' or '/' which are invalid here
✗ Incorrect
We subtract 1 from n to get the previous Fibonacci number, so the operator must be '-'.
4fill in blank
hardFill both blanks to complete the base case check and return statement.
DSA Javascript
if (n [1] 0) return [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '==' causing incorrect base case
Returning n instead of 0 for base case
✗ Incorrect
We check if n equals 0 and return 0 as the base Fibonacci number.
5fill in blank
hardFill all three blanks to create a memoized Fibonacci function with base cases and recursion.
DSA Javascript
const fib = (n, memo = [[1], 1]) => { if (n [2] 0) return [3]; if (memo[n] !== undefined) return memo[n]; memo[n] = fib(n - 1, memo) + fib(n - 2, memo); return memo[n]; };
Attempts:
3 left
💡 Hint
Common Mistakes
Wrong memo initialization
Incorrect base case condition
Returning wrong value for base case
✗ Incorrect
Initialize memo with 0,1; check if n==0; return 0 for base case.
