What is the base case in a recursive Fibonacci function?
The base case returns the first two Fibonacci numbers directly: fib(0) = 0 and fib(1) = 1. This stops the recursion from continuing infinitely.
Click to reveal answer
beginner
Explain how recursion works in the Fibonacci sequence calculation.
Recursion breaks the problem into smaller parts by calling the Fibonacci function for (n-1) and (n-2) until it reaches the base cases. Then it adds those results to get fib(n).
Click to reveal answer
intermediate
What is the time complexity of the naive recursive Fibonacci algorithm?
The time complexity is exponential, specifically O(2^n), because it recalculates the same Fibonacci numbers many times.
Click to reveal answer
beginner
Write the recursive formula for Fibonacci numbers.
fib(n) = fib(n-1) + fib(n-2) with base cases fib(0) = 0 and fib(1) = 1.
Click to reveal answer
intermediate
Why is recursion not efficient for large Fibonacci numbers without optimization?
Because it repeats calculations for the same values many times, leading to a lot of unnecessary work and slow performance.
Click to reveal answer
What does the base case in Fibonacci recursion return for fib(0)?
A0
B1
CUndefined
Dfib(1)
✗ Incorrect
The base case fib(0) returns 0 to stop recursion.
Which of these is the recursive call in Fibonacci calculation?
Afib(n) = fib(n-1) + fib(n-2)
Bfib(n) = n + fib(n-1)
Cfib(n) = fib(n-1) * fib(n-2)
Dfib(n) = fib(n-1) - fib(n-2)
✗ Incorrect
The Fibonacci number is the sum of the two previous Fibonacci numbers.
What is the main problem with naive recursive Fibonacci?
AIt uses too much memory
BIt recalculates values multiple times
CIt never reaches base case
DIt only works for even numbers
✗ Incorrect
Naive recursion recalculates the same Fibonacci numbers many times, causing inefficiency.
What is the output of fib(3) using recursion?
A1
B3
C2
D5
✗ Incorrect
fib(3) = fib(2) + fib(1) = 1 + 1 = 2.
Which of these is NOT a base case for Fibonacci recursion?
Afib(0) = 0
Bfib(1) = 1
Cfib(2) = 1
Dfib(3) = 2
✗ Incorrect
Only fib(0) and fib(1) are base cases; fib(3) is calculated recursively.
Describe how the Fibonacci sequence is calculated using recursion.
Think about how the function calls itself with smaller numbers until it reaches the simplest cases.
You got /4 concepts.
Explain why naive recursion for Fibonacci is inefficient and how it can be improved.
Consider what happens when the function calls itself many times for the same input.
You got /4 concepts.
Practice
(1/5)
1. What does the Fibonacci sequence represent when calculated using recursion?
easy
A. Each number is the square of the previous number
B. Each number is the product of the two preceding numbers
C. Each number is the difference of the two preceding numbers
D. Each number is the sum of the two preceding numbers
Solution
Step 1: Understand Fibonacci sequence definition
The Fibonacci sequence is defined such that each number is the sum of the two numbers before it.
Step 2: Relate to recursion
Recursion calculates this by calling the function for the two previous numbers and adding their results.
Final Answer:
Each number is the sum of the two preceding numbers -> Option D
Quick Check:
Fibonacci = sum of two previous numbers [OK]
Hint: Fibonacci adds two previous numbers, not multiply or subtract [OK]
Common Mistakes:
Thinking Fibonacci multiplies previous numbers
Confusing difference with sum
Assuming Fibonacci squares previous number
2. Which of the following is the correct base case syntax for a recursive Fibonacci function in Python?
easy
A. if n == 0 or n == 1: return n
B. if n < 0: return 0
C. if n == 2: return 1
D. if n == 1: return 0
Solution
Step 1: Identify base cases for Fibonacci
Fibonacci sequence starts with 0 at position 0 and 1 at position 1, so base cases are n == 0 and n == 1.
Step 2: Check syntax correctness
The correct syntax returns n when n is 0 or 1, matching the base cases.
Final Answer:
if n == 0 or n == 1: return n -> Option A
Quick Check:
Base cases return n for 0 or 1 [OK]
Hint: Base cases return n when n is 0 or 1 [OK]
Common Mistakes:
Using wrong base case values
Returning 0 for n == 1
Missing base case for n == 0
3. What is the output of the following code?
def fib(n):
if n == 0 or n == 1:
return n
else:
return fib(n-1) + fib(n-2)
print(fib(4))