0
0
DSA Typescriptprogramming~10 mins

Fibonacci Using DP in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Anull
B1
C0
D-1
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.
2fill in blank
medium

Complete 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'
A1
B2
C3
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for n === 2 instead of 1.
Returning a fixed number instead of n.
3fill in blank
hard

Fix 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'
A0
B-1
Cnull
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for undefined, 0, or null instead of -1.
Using == instead of !== causing wrong logic.
4fill in blank
hard

Fill 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'
A(
B[
C]
D)
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] which are for arrays, not function calls.
Missing parentheses causing syntax errors.
5fill in blank
hard

Fill 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'
A(
B)
C[
D]
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets instead of parentheses.
Omitting parentheses causing syntax errors.