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 further recursion.
Click to reveal answer
beginner
Explain why recursion is a natural fit for calculating Fibonacci numbers.
Because each Fibonacci number is the sum of the two previous ones, recursion naturally expresses this by calling the function for n-1 and n-2.
Click to reveal answer
intermediate
What is the time complexity of the naive recursive Fibonacci algorithm?
It is exponential, specifically O(2^n), because it recalculates the same Fibonacci numbers many times.
Click to reveal answer
beginner
Show the Rust function signature for a recursive Fibonacci function.
fn fibonacci(n: u32) -> u32
Click to reveal answer
intermediate
What happens if you call the recursive Fibonacci function with n = 5? Describe the calls.
The function calls fibonacci(4) and fibonacci(3), then fibonacci(4) calls fibonacci(3) and fibonacci(2), and so on, breaking down until base cases are reached.
Click to reveal answer
What does the base case in a recursive Fibonacci function usually check?
AIf n is 0 or 1
BIf n is negative
CIf n is greater than 10
DIf n is even
✗ Incorrect
The base case returns the Fibonacci number directly when n is 0 or 1 to stop recursion.
What is the main problem with the naive recursive Fibonacci implementation?
AIt uses too much memory
BIt recalculates the same values multiple times
CIt does not return correct results
DIt only works for even numbers
✗ Incorrect
The naive recursion recalculates Fibonacci numbers repeatedly, causing exponential time complexity.
Which Rust keyword is used to define a function?
Afunc
Bdef
Cfn
Dfunction
✗ Incorrect
In Rust, functions are defined using the 'fn' keyword.
What is the return type of a Fibonacci function that takes a u32 in Rust?
Au32
Bi32
Cusize
Dbool
✗ Incorrect
The function returns a u32 to match the input type and represent Fibonacci numbers.
How many times is fibonacci(1) called when calculating fibonacci(5) recursively?
A1
B2
C3
D5
✗ Incorrect
fibonacci(1) is called 5 times during the recursive calls for fibonacci(5).
Describe how recursion works in the Fibonacci function and why base cases are important.
Think about how the function breaks down the problem into smaller parts.
You got /3 concepts.
Explain the drawbacks of the naive recursive Fibonacci approach and suggest a way to improve it.
Consider how to avoid recalculating the same Fibonacci numbers.
You got /3 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 calls the function for the two previous numbers and adds 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 last two numbers, not multiply or subtract [OK]
Common Mistakes:
Thinking Fibonacci multiplies previous numbers
Confusing subtraction or division with addition
Assuming Fibonacci squares previous number
2. Which of the following is the correct Rust function signature for a recursive Fibonacci function?
easy
A. fn fibonacci(n: i32) -> i32
B. fn fibonacci(n) -> i32
C. fn fibonacci(n: i32)
D. fn fibonacci(n: i32) -> void
Solution
Step 1: Check Rust function signature syntax
Rust requires parameter types and return type specified, e.g., fn name(param: type) -> return_type.
Step 2: Match correct signature for Fibonacci
Fibonacci takes an integer and returns an integer, so fn fibonacci(n: i32) -> i32 is correct.
Final Answer:
fn fibonacci(n: i32) -> i32 -> Option A
Quick Check:
Rust needs types and return type [OK]
Hint: Rust functions need parameter and return types declared [OK]
Common Mistakes:
Omitting parameter type
Missing return type
Using void instead of () or proper return type
3. What is the output of this Rust code?
fn fibonacci(n: i32) -> i32 {
if n <= 1 {
n
} else {
fibonacci(n - 1) + fibonacci(n - 2)
}
}
fn main() {
println!("{}", fibonacci(5));
}