Challenge - 5 Problems
Fibonacci Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Fibonacci Recursive Call
What is the output of the following Rust code that calculates the 5th Fibonacci number using recursion?
DSA Rust
fn fibonacci(n: u32) -> u32 {
if n <= 1 {
n
} else {
fibonacci(n - 1) + fibonacci(n - 2)
}
}
fn main() {
println!("{}", fibonacci(5));
}Attempts:
2 left
💡 Hint
Remember that Fibonacci sequence starts with 0, 1, 1, 2, 3, 5...
✗ Incorrect
The Fibonacci sequence starts with 0 and 1. fibonacci(5) returns the 5th number in the sequence, which is 5.
❓ Predict Output
intermediateOutput of Fibonacci Recursive Call with n=7
What is the output of this Rust code that calculates the 7th Fibonacci number recursively?
DSA Rust
fn fibonacci(n: u32) -> u32 {
if n <= 1 {
n
} else {
fibonacci(n - 1) + fibonacci(n - 2)
}
}
fn main() {
println!("{}", fibonacci(7));
}Attempts:
2 left
💡 Hint
Count the Fibonacci numbers starting from 0: 0,1,1,2,3,5,8,13...
✗ Incorrect
The 7th Fibonacci number (starting from 0) is 13.
🧠 Conceptual
advancedUnderstanding Recursive Calls in Fibonacci
How many total calls to the fibonacci function are made when calculating fibonacci(4) using the recursive function below?
DSA Rust
fn fibonacci(n: u32) -> u32 {
if n <= 1 {
n
} else {
fibonacci(n - 1) + fibonacci(n - 2)
}
}Attempts:
2 left
💡 Hint
Draw the recursive call tree for fibonacci(4).
✗ Incorrect
The call tree for fibonacci(4) has 9 total calls including repeated calls.
🔧 Debug
advancedIdentify the Runtime Error in Fibonacci Code
What error will this Rust code produce when run, and why?
DSA Rust
fn fibonacci(n: i32) -> i32 {
if n <= 1 {
n
} else {
fibonacci(n - 1) + fibonacci(n - 2)
}
}
fn main() {
println!("{}", fibonacci(-1));
}Attempts:
2 left
💡 Hint
Consider what happens when n is negative in the recursive calls.
✗ Incorrect
The function never reaches the base case for negative n, causing infinite recursion and stack overflow.
🚀 Application
expertOptimizing Fibonacci with Memoization
Which Rust code snippet correctly implements memoization to optimize the recursive Fibonacci function?
Attempts:
2 left
💡 Hint
Memoization stores results to avoid repeated calculations. Check how memo is used and initialized.
✗ Incorrect
Option A uses a mutable vector initialized with zeros and checks if a value is already computed before recursive calls.
