Challenge - 5 Problems
Master of DP vs Recursion vs Greedy
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Fibonacci Function
What is the output of this Rust code that calculates the 5th Fibonacci number using recursion?
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
Remember the Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8...
✗ Incorrect
The recursive fib(5) returns 5, following the sequence: fib(0)=0, fib(1)=1, fib(2)=1, fib(3)=2, fib(4)=3, fib(5)=5.
❓ Predict Output
intermediateOutput of Greedy Coin Change Algorithm
What is the output of this Rust code that uses a greedy approach to find minimum coins for amount 27 with coins [10, 5, 2, 1]?
DSA Rust
fn min_coins_greedy(mut amount: u32, coins: &[u32]) -> u32 {
let mut count = 0;
for &coin in coins {
count += amount / coin;
amount %= coin;
}
count
}
fn main() {
let coins = [10, 5, 2, 1];
println!("{}", min_coins_greedy(27, &coins));
}Attempts:
2 left
💡 Hint
Count how many coins of each type fit into 27 starting from the largest coin.
✗ Incorrect
Greedy picks 2 coins of 10 (20), then 1 coin of 5 (5), then 1 coin of 2 (2), total 4 coins.
❓ Predict Output
advancedOutput of DP Fibonacci Memoization
What is the output of this Rust code that calculates the 7th Fibonacci number using dynamic programming with memoization?
DSA Rust
fn fib_dp(n: usize, memo: &mut Vec<u32>) -> u32 {
if n <= 1 {
return n as u32;
}
if memo[n] != 0 {
return memo[n];
}
memo[n] = fib_dp(n - 1, memo) + fib_dp(n - 2, memo);
memo[n]
}
fn main() {
let n = 7;
let mut memo = vec![0; n + 1];
println!("{}", fib_dp(n, &mut memo));
}Attempts:
2 left
💡 Hint
The 7th Fibonacci number (0-based) is 13.
✗ Incorrect
Using memoization, fib_dp(7) returns 13 as the sequence is 0,1,1,2,3,5,8,13...
❓ Predict Output
advancedOutput of Recursive vs DP for Coin Change Count
What is the output of this Rust code that calculates the number of ways to make change for 5 using coins [1, 2] with recursion and DP?
DSA Rust
fn count_ways_recursive(coins: &[u32], m: usize, n: u32) -> u32 {
if n == 0 {
return 1;
}
if n < 0 || m == 0 {
return 0;
}
count_ways_recursive(coins, m - 1, n) + count_ways_recursive(coins, m, n - coins[m - 1])
}
fn count_ways_dp(coins: &[u32], n: u32) -> u32 {
let m = coins.len();
let mut table = vec![vec![0; (n + 1) as usize]; m + 1];
for i in 0..=m {
table[i][0] = 1;
}
for i in 1..=m {
for j in 1..=n as usize {
if coins[i - 1] as usize > j {
table[i][j] = table[i - 1][j];
} else {
table[i][j] = table[i - 1][j] + table[i][j - coins[i - 1] as usize];
}
}
}
table[m][n as usize]
}
fn main() {
let coins = [1, 2];
let n = 5;
println!("{} {}", count_ways_recursive(&coins, coins.len(), n), count_ways_dp(&coins, n));
}Attempts:
2 left
💡 Hint
Count the ways to make 5 using 1 and 2 coins.
✗ Incorrect
Both recursive and DP methods find 3 ways: {1,1,1,1,1}, {1,1,1,2}, {1,2,2}.
🧠 Conceptual
expertChoosing the Right Tool: DP vs Recursion vs Greedy
Which of the following statements best explains when to use dynamic programming (DP) instead of recursion or greedy algorithms?
Attempts:
2 left
💡 Hint
Think about problem properties and algorithm efficiency.
✗ Incorrect
DP is best when subproblems overlap and greedy does not guarantee the best solution; recursion alone can be inefficient.
