Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the base case value for Fibonacci of 0.
DSA Java
if (n == 0) { return [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 instead of 0 for n=0.
Returning n which is 0 but less clear.
✗ Incorrect
The Fibonacci number for 0 is 0, so the function should return 0 in this base case.
2fill in blank
mediumComplete the code to return the base case value for Fibonacci of 1.
DSA Java
if (n == 1) { return [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of 1 for n=1.
Returning n which is 1 but less explicit.
✗ Incorrect
The Fibonacci number for 1 is 1, so the function should return 1 in this base case.
3fill in blank
hardFix the error in the recursive call to calculate Fibonacci of n-1.
DSA Java
return fibonacci([1]) + fibonacci(n - 2);
Attempts:
3 left
💡 Hint
Common Mistakes
Using n+1 which causes infinite recursion.
Using n or n-3 which are incorrect indices.
✗ Incorrect
The Fibonacci function calls itself with n-1 to get the previous Fibonacci number.
4fill in blank
hardFill both blanks to complete the recursive Fibonacci function.
DSA Java
public int fibonacci(int n) {
if (n == [1] || n == [2]) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 and 2 as base cases instead of 0 and 1.
Returning fixed values instead of n.
✗ Incorrect
The base cases for Fibonacci are when n is 0 or 1, returning n itself.
5fill in blank
hardFill all three blanks to create a recursive Fibonacci function with base cases and recursive calls.
DSA Java
public int fibonacci(int n) {
if (n == [1] || n == [2]) {
return n;
}
return fibonacci(n [3] 1) + fibonacci(n - 2);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction in recursive call.
Wrong base cases like 1 and 2.
✗ Incorrect
Base cases are n == 0 or n == 1 returning n, and the recursive call subtracts 1 from n.
