Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dp vector with a large number.
DSA Rust
let mut dp = vec![[1]; amount + 1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 will cause incorrect minimum calculations.
Using usize::MAX can cause overflow when adding 1.
✗ Incorrect
We initialize dp with amount + 1 because it's larger than any possible minimum coins needed, acting as infinity.
2fill in blank
mediumComplete the code to set the base case for dp.
DSA Rust
dp[0] = [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[0] to 1 or amount causes wrong results.
Using usize::MAX leads to overflow errors.
✗ Incorrect
dp[0] = 0 means zero coins are needed to make amount zero.
3fill in blank
hardFix the error in the inner loop condition to avoid index out of bounds.
DSA Rust
for coin in coins.iter() { if coin <= &[1] { dp[i] = dp[i].min(dp[i - coin] + 1); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing coin with amount instead of i causes logic errors.
Using coin or dp[i] in the condition causes type errors.
✗ Incorrect
We compare coin with i (current amount) to ensure we don't access negative indices.
4fill in blank
hardFill both blanks to complete the outer loop and return statement.
DSA Rust
for [1] in 1..=[2] { for coin in coins.iter() { if coin <= &i { dp[i] = dp[i].min(dp[i - coin] + 1); } } } if dp[amount] > amount { -1 } else { dp[amount] }
Attempts:
3 left
💡 Hint
Common Mistakes
Using coin as loop variable causes errors.
Looping beyond amount causes index errors.
✗ Incorrect
The outer loop variable is i iterating from 1 to amount inclusive.
5fill in blank
hardFill all three blanks to complete the function signature, dp initialization, and return statement.
DSA Rust
fn coin_change([1]: Vec<i32>, [2]: i32) -> i32 { let amount = [2] as usize; let mut dp = vec![amount + 1; amount + 1]; dp[0] = 0; for i in 1..=amount { for coin in [1].iter() { if (*coin as usize) <= i { dp[i] = dp[i].min(dp[i - *coin as usize] + 1); } } } if dp[amount] > amount { -1 } else { dp[amount] } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names causes errors.
Not casting amount to usize causes type errors.
✗ Incorrect
The function takes coins vector and amount integer; dp uses amount; coins vector is iterated.
