Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the memoization array for Fibonacci numbers.
DSA Java
int[] memo = new int[[1]];Attempts:
3 left
💡 Hint
Common Mistakes
Using n instead of n + 1 causes ArrayIndexOutOfBoundsException.
Using n - 1 misses the last Fibonacci number.
✗ Incorrect
The memo array size should be n + 1 to store Fibonacci values from 0 to n inclusive.
2fill in blank
mediumComplete the code to check if the Fibonacci value is already computed in memo.
DSA Java
if (memo[[1]] != 0) return memo[[1]];
Attempts:
3 left
💡 Hint
Common Mistakes
Checking memo[n - 1] instead of memo[n].
Checking memo[0] which is unrelated to current n.
✗ Incorrect
We check memo[n] to see if Fibonacci of n is already computed.
3fill in blank
hardFix the error in the recursive call to compute Fibonacci with memoization.
DSA Java
memo[n] = fib([1], memo) + fib([2], memo);
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n instead of n-1 or n-2 causes wrong results.
✗ Incorrect
Fibonacci is sum of fib(n-1) and fib(n-2).
4fill in blank
hardFill both blanks to complete the base case and return statement in Fibonacci with memoization.
DSA Java
if (n <= [1]) return [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 for n=0 is incorrect.
Using 2 as base case limit is wrong.
✗ Incorrect
Base case: if n <= 1, return n (0 or 1).
5fill in blank
hardFill all three blanks to complete the memoized Fibonacci function.
DSA Java
public static int fib(int n, int[] memo) {
if (n <= [1]) return [2];
if (memo[[3]] != 0) return memo[n];
memo[n] = fib(n - 1, memo) + fib(n - 2, memo);
return memo[n];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong base case values.
Checking memo with wrong index.
✗ Incorrect
Base case returns n if n <= 1; check memo[n] for cached value.
