What is the base case in a recursive Fibonacci function?
The base case returns the first two Fibonacci numbers directly, usually when n is 0 or 1, to stop the recursion.
Click to reveal answer
beginner
Explain how recursion works in calculating Fibonacci numbers.
Recursion calls the same function with smaller inputs (n-1 and n-2) until it reaches the base case, then combines results to get the final Fibonacci number.
Click to reveal answer
intermediate
What is the time complexity of the naive recursive Fibonacci algorithm?
The time complexity is exponential, approximately O(2^n), because it recalculates the same values many times.
Click to reveal answer
beginner
Show the recursive formula used in Fibonacci calculation.
fib(n) = fib(n-1) + fib(n-2), with fib(0) = 0 and fib(1) = 1 as base cases.
Click to reveal answer
intermediate
Why can recursion be inefficient for Fibonacci calculation without optimization?
Because it repeats calculations for the same inputs multiple times, leading to many redundant calls and slow performance.
Click to reveal answer
What does the base case in a recursive Fibonacci function do?
AStops recursion by returning a known Fibonacci number
BCalls the function again with the same input
CCalculates Fibonacci using a loop
DReturns the sum of all previous Fibonacci numbers
✗ Incorrect
The base case stops recursion by returning a known Fibonacci number for n=0 or n=1.
Which of these is the correct recursive formula for Fibonacci?
Afib(n) = fib(n-1) + fib(n-2)
Bfib(n) = fib(n+1) - fib(n-1)
Cfib(n) = fib(n-1) * fib(n-2)
Dfib(n) = fib(n-2) - fib(n-3)
✗ Incorrect
The Fibonacci number at n is the sum of the two previous Fibonacci numbers.
What is the main problem with naive recursive Fibonacci calculation?
AIt uses too much memory
BIt does not return correct results
CIt recalculates the same values many times
DIt only works for even numbers
✗ Incorrect
Naive recursion recalculates the same Fibonacci numbers repeatedly, causing inefficiency.
What are the base cases for Fibonacci recursion?
Afib(1) = 1 and fib(2) = 2
Bfib(0) = 0 and fib(1) = 1
Cfib(0) = 1 and fib(1) = 1
Dfib(2) = 1 and fib(3) = 2
✗ Incorrect
The base cases are fib(0) = 0 and fib(1) = 1 to stop recursion.
What is the time complexity of naive recursive Fibonacci?
AO(n)
BO(n^2)
CO(log n)
DO(2^n)
✗ Incorrect
Naive recursion has exponential time complexity O(2^n) due to repeated calculations.
Describe how a recursive function calculates the nth Fibonacci number.
Think about breaking the problem into smaller parts.
You got /4 concepts.
Explain why naive recursion is inefficient for Fibonacci numbers and how it can be improved.
Consider how storing results can help.
You got /4 concepts.
Practice
(1/5)
1. What does a recursive Fibonacci function do when called with fib(4)?
easy
A. Throws a syntax error because recursion is not allowed
B. Returns 4 immediately without any calculation
C. Runs an infinite loop without returning
D. Calculates the 4th Fibonacci number by calling itself with smaller numbers
Solution
Step 1: Understand recursive calls for fib(4)
The function calls fib(3) and fib(2) to calculate fib(4).
Step 2: Combine results of smaller calls
fib(3) and fib(2) are calculated recursively and their sum is returned as fib(4).
Final Answer:
Calculates the 4th Fibonacci number by calling itself with smaller numbers -> Option D
Quick Check:
Recursive calls break problem into smaller parts [OK]
Hint: Recursion breaks problem into smaller calls [OK]
Common Mistakes:
Thinking recursion returns input directly
Assuming recursion causes infinite loop always
Confusing recursion with iteration
2. Which of the following is the correct base case syntax for a recursive Fibonacci function in JavaScript?
easy
A. if (n = 0) return 0;
B. if (n <= 1) return n;
C. if n == 1 then return 1
D. if (n > 1) return n;
Solution
Step 1: Identify correct JavaScript syntax for base case
The base case must check if n is 0 or 1 and return n using proper JavaScript syntax.
Step 2: Validate each option
if (n <= 1) return n; uses correct syntax: if (n <= 1) return n; Options A and B have syntax errors or wrong operators, D is logically incorrect.
Final Answer:
if (n <= 1) return n; -> Option B
Quick Check:
Base case uses if (n <= 1) return n; [OK]
Hint: Base case returns n when n is 0 or 1 [OK]
Common Mistakes:
Using assignment (=) instead of comparison (== or ===)
Missing parentheses in if condition
Incorrect logical condition for base case
3. What is the output of the following code?
function fib(n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
console.log(fib(5));
medium
A. 5
B. 8
C. 3
D. 13
Solution
Step 1: Trace fib(5) recursive calls
fib(5) = fib(4) + fib(3), fib(4) = fib(3) + fib(2), fib(3) = fib(2) + fib(1), and so on until base cases.