Challenge - 5 Problems
DP Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Fibonacci Without Memoization
What is the output of this Rust code that calculates Fibonacci numbers using plain recursion without any optimization?
DSA Rust
fn fib(n: u32) -> u32 {
if n <= 1 {
n
} else {
fib(n - 1) + fib(n - 2)
}
}
fn main() {
println!("{}", fib(5));
}Attempts:
2 left
💡 Hint
Trace the recursive calls for fib(5): fib(4) + fib(3).
✗ Incorrect
The Fibonacci sequence starts 0,1,1,2,3,5,8,... fib(5) is the 6th number (indexing from 0), which is 5.
🧠 Conceptual
intermediateWhy Recursion Alone Can Be Inefficient
Why does plain recursion without memoization or dynamic programming often lead to inefficiency in problems like Fibonacci calculation?
Attempts:
2 left
💡 Hint
Think about how many times fib(3) is calculated in fib(5).
✗ Incorrect
Plain recursion recalculates the same subproblems many times, causing exponential time complexity.
❓ Predict Output
advancedOutput of Fibonacci with Memoization in Rust
What is the output of this Rust code that uses memoization to optimize Fibonacci calculation?
DSA Rust
use std::collections::HashMap;
fn fib_memo(n: u32, memo: &mut HashMap<u32, u32>) -> u32 {
if n <= 1 {
return n;
}
if memo.contains_key(&n) {
return *memo.get(&n).unwrap();
}
let result = fib_memo(n - 1, memo) + fib_memo(n - 2, memo);
memo.insert(n, result);
result
}
fn main() {
let mut memo = HashMap::new();
println!("{}", fib_memo(10, &mut memo));
}Attempts:
2 left
💡 Hint
Recall Fibonacci sequence: 0,1,1,2,3,5,8,13,21,34,55,...
✗ Incorrect
fib(10) is the 11th Fibonacci number (indexing from 0), which is 55.
🔧 Debug
advancedIdentify the Problem in This Recursive Code
What error or problem will this Rust code cause when run?
DSA Rust
fn factorial(n: u32) -> u32 {
if n == 0 {
1
} else {
n * factorial(n - 1)
}
}
fn main() {
println!("{}", factorial(5));
}Attempts:
2 left
💡 Hint
Check the recursive call inside factorial function.
✗ Incorrect
The recursive call uses factorial(n) instead of factorial(n-1), causing infinite recursion.
🚀 Application
expertDynamic Programming for Minimum Coin Change Problem
Given coins with values [1, 3, 4] and a target amount 6, what is the minimum number of coins needed to make 6 using dynamic programming?
Attempts:
2 left
💡 Hint
Try combinations: 3+3 or 4+1+1
✗ Incorrect
The minimum coins to make 6 are two coins: 3 + 3.
