Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the base case value for n equals 0.
DSA Typescript
function fibonacci(n: number): number {
if (n === 0) {
return [1];
}
return -1; // placeholder
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 instead of 0 for n=0
Returning n directly without checking the base case
✗ Incorrect
The Fibonacci sequence starts with 0 at position 0, so the function should return 0 when n is 0.
2fill in blank
mediumComplete the code to return the base case value for n equals 1.
DSA Typescript
function fibonacci(n: number): number {
if (n === 0) {
return 0;
}
if (n === 1) {
return [1];
}
return -1; // placeholder
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of 1 for n=1
Not handling the base case for n=1
✗ Incorrect
The Fibonacci sequence has 1 at position 1, so the function should return 1 when n is 1.
3fill in blank
hardFix the error in the recursive call to compute Fibonacci numbers.
DSA Typescript
function fibonacci(n: number): number {
if (n === 0) return 0;
if (n === 1) return 1;
return fibonacci(n [1] 1) + fibonacci(n - 2);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction in the recursive call
Using multiplication or division which are incorrect here
✗ Incorrect
To get the previous Fibonacci number, subtract 1 from n in the recursive call.
4fill in blank
hardFill both blanks to complete the recursive Fibonacci function correctly.
DSA Typescript
function fibonacci(n: number): number {
if (n === 0) return 0;
if (n === 1) return 1;
return fibonacci(n [1] 1) [2] fibonacci(n - 2);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators like multiplication or division
Mixing up the order of operations
✗ Incorrect
The Fibonacci number is the sum of the two previous Fibonacci numbers at n-1 and n-2.
5fill in blank
hardFill all three blanks to create a recursive Fibonacci function with a base case and recursive calls.
DSA Typescript
function fibonacci(n: number): number {
if (n === [1]) return 0;
if (n === [2]) return 1;
return fibonacci(n [3] 1) + fibonacci(n - 2);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping base case values
Using addition instead of subtraction in recursive call
✗ Incorrect
The base cases are for n=0 returning 0 and n=1 returning 1. The recursive call subtracts 1 from n.