Challenge - 5 Problems
Master of Overlapping Subproblems and Optimal Substructure
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Fibonacci with Memoization
What is the output of the following Rust code that calculates the 5th Fibonacci number using memoization?
DSA Rust
fn fib(n: usize, memo: &mut Vec<Option<usize>>) -> usize {
if let Some(value) = memo[n] {
return value;
}
if n <= 1 {
memo[n] = Some(n);
return n;
}
let result = fib(n - 1, memo) + fib(n - 2, memo);
memo[n] = Some(result);
result
}
fn main() {
let n = 5usize;
let mut memo = vec![None::<usize>; 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...
✗ Incorrect
The 5th Fibonacci number (0-based index) is 5. The code uses memoization to avoid repeated calculations.
🧠 Conceptual
intermediateIdentifying Overlapping Subproblems
Which of the following problems best demonstrates the concept of overlapping subproblems?
Attempts:
2 left
💡 Hint
Overlapping subproblems means the same subproblems are solved multiple times.
✗ Incorrect
Computing Fibonacci numbers recursively without memoization solves the same subproblems repeatedly, demonstrating overlapping subproblems.
🔧 Debug
advancedDebugging Memoization Implementation
What error will the following Rust code produce when run, and why?
DSA Rust
fn fib(n: usize, memo: &mut Vec<Option<usize>>) -> usize {
if memo[n].is_some() {
return memo[n].unwrap();
}
if n <= 1 {
memo[n] = Some(n);
return n;
}
let result = fib(n - 1, memo) + fib(n - 2, memo);
memo[n] = Some(result);
result
}
fn main() {
let n = 5usize;
let mut memo = Vec::new();
let result = fib(n, &mut memo);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Check how the memo vector is initialized before use.
✗ Incorrect
The memo vector is empty, so accessing memo[n] causes an index out of bounds panic.
🚀 Application
advancedOptimal Substructure in Shortest Path
Which statement best describes the optimal substructure property in the context of shortest path algorithms?
Attempts:
2 left
💡 Hint
Think about what happens if a subpath is not shortest.
✗ Incorrect
Optimal substructure means that the best solution contains best solutions to subproblems. For shortest path, any part of the path is also shortest between those points.
❓ Predict Output
expertOutput of Dynamic Programming Table for Coin Change
What is the final state of the dp vector after running the following Rust code to find the number of ways to make change for 5 using coins [1, 2, 5]?
DSA Rust
fn main() {
let amount = 5usize;
let coins = vec![1usize, 2usize, 5usize];
let mut dp = vec![0usize; amount + 1];
dp[0] = 1;
for coin in coins {
for x in coin..=amount {
dp[x] += dp[x - coin];
}
}
println!("{:?}", dp);
}Attempts:
2 left
💡 Hint
dp[x] counts ways to make amount x using given coins.
✗ Incorrect
The dp vector after processing coins [1,2,5] for amount 5 is [1,1,2,2,3,4].
