Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the memo array with -1.
DSA Typescript
const memo: number[] = new Array(n + 1).fill([1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 causes incorrect returns since fib(0) = 0 and memo[2] would wrongly return 0.
Using 1 causes confusion with fib(1) = 1.
Using null causes type errors in TypeScript.
✗ Incorrect
We fill the memo array with -1 to indicate that no Fibonacci values are computed yet.
2fill in blank
mediumComplete the base case check to return n if it is 0 or 1.
DSA Typescript
if (n === 0 || n === [1]) return n;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for n === 2 instead of 1.
Returning a fixed number instead of n.
✗ Incorrect
The Fibonacci of 0 is 0 and of 1 is 1, so we return n when n is 0 or 1.
3fill in blank
hardFix the error in the memoization check to return the stored value if already computed.
DSA Typescript
if (memo[n] !== [1]) return memo[n];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for undefined, 0, or null instead of -1.
Using == instead of !== causing wrong logic.
✗ Incorrect
We check if memo[n] !== -1 to know if the value is already computed.
4fill in blank
hardFill both blanks to compute and store the Fibonacci value in memo.
DSA Typescript
memo[n] = fibonacci[1](n - 1) + fibonacci[2](n - 2);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] which are for arrays, not function calls.
Missing parentheses causing syntax errors.
✗ Incorrect
We call the fibonacci function recursively with parentheses around the arguments.
5fill in blank
hardFill all three blanks to complete the memoized Fibonacci function.
DSA Typescript
function fibonacci[1](n: number): number { if (n === 0 || n === 1) return n; if (memo[n] !== -1) return memo[n]; memo[n] = fibonacci[2](n - 1) + fibonacci[3](n - 2); return memo[n]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets instead of parentheses.
Omitting parentheses causing syntax errors.
✗ Incorrect
Function calls and definitions use parentheses () around parameters and arguments.