Challenge - 5 Problems
Fibonacci DP Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Fibonacci DP with memoization
What is the output of this Rust code that calculates the 7th Fibonacci number using memoization?
DSA Rust
fn fib(n: usize, memo: &mut Vec<Option<u64>>) -> u64 {
if let Some(val) = memo[n] {
return val;
}
if n <= 1 {
memo[n] = Some(n as u64);
return n as u64;
}
let result = fib(n - 1, memo) + fib(n - 2, memo);
memo[n] = Some(result);
result
}
fn main() {
let n = 7;
let mut memo = vec![None; n + 1];
let result = fib(n, &mut memo);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Remember that Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8, 13...
✗ Incorrect
The 7th Fibonacci number (0-based index) is 13. The code uses memoization to store intermediate results and avoid repeated calculations.
❓ Predict Output
intermediateOutput of bottom-up Fibonacci DP
What is the output of this Rust code that calculates the 10th Fibonacci number using bottom-up dynamic programming?
DSA Rust
fn fib(n: usize) -> u64 {
let mut dp = vec![0u64; n + 1];
dp[0] = 0;
if n > 0 {
dp[1] = 1;
}
for i in 2..=n {
dp[i] = dp[i - 1] + dp[i - 2];
}
dp[n]
}
fn main() {
let result = fib(10);
println!("{}", result);
}Attempts:
2 left
💡 Hint
The 10th Fibonacci number (0-based) is 55.
✗ Incorrect
The bottom-up approach fills the dp array from 0 to n. For n=10, the Fibonacci number is 55.
🔧 Debug
advancedIdentify the error in this Fibonacci DP code
What error does this Rust code produce when calculating the 5th Fibonacci number using memoization?
DSA Rust
fn fib(n: usize, memo: &mut Vec<Option<u64>>) -> u64 {
if memo[n].is_some() {
return memo[n].unwrap();
}
if n <= 1 {
return n as u64;
}
let result = fib(n - 1, memo) + fib(n - 2, memo);
memo[n] = Some(result);
result
}
fn main() {
let n = 5;
let mut memo = vec![None; n + 1];
let result = fib(n, &mut memo);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Check the size of the memo vector compared to the index n.
✗ Incorrect
The memo vector is created with size n (5), but the code accesses memo[5], which is out of bounds (indices 0 to 4). This causes a runtime panic.
🧠 Conceptual
advancedWhy use dynamic programming for Fibonacci?
Which reason best explains why dynamic programming improves Fibonacci number calculation compared to naive recursion?
Attempts:
2 left
💡 Hint
Think about what happens when naive recursion calls the same function multiple times with the same input.
✗ Incorrect
Dynamic programming stores results of subproblems to avoid recalculating them, making Fibonacci calculation efficient.
🚀 Application
expertCalculate sum of first N Fibonacci numbers using DP
Given the Rust function below that calculates Fibonacci numbers using DP, which option correctly prints the sum of the first 8 Fibonacci numbers (0-based)?
DSA Rust
fn fib(n: usize) -> u64 {
let mut dp = vec![0u64; n + 1];
dp[0] = 0;
if n > 0 {
dp[1] = 1;
}
for i in 2..=n {
dp[i] = dp[i - 1] + dp[i - 2];
}
dp[n]
}
fn main() {
let n = 8;
// Calculate sum of first n Fibonacci numbers here
// Fill in the blank
}Attempts:
2 left
💡 Hint
Sum includes Fibonacci numbers from index 0 to 8 inclusive.
✗ Incorrect
The range 0..=n includes all indices from 0 to n. Using map and sum calculates the total sum correctly.
