Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the DP table with zeros.
DSA Rust
let mut dp = vec![0; [1]];
Attempts:
3 left
💡 Hint
Common Mistakes
Using n instead of n + 1 causes index out of bounds errors.
Using n - 1 misses the last element.
✗ Incorrect
The DP table size should be n + 1 to include the base case at index 0.
2fill in blank
mediumComplete the code to fill the DP table bottom-up for Fibonacci numbers.
DSA Rust
for i in 2..=[1] { dp[i] = dp[i - 1] + dp[i - 2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Looping only to n - 1 misses the last value.
Looping beyond n + 1 causes index errors.
✗ Incorrect
The loop should run up to n inclusive to compute dp[n].
3fill in blank
hardFix the error in the DP table update for the coin change problem.
DSA Rust
for coin in coins.iter() { for amount in [1]..=target { if amount >= *coin { dp[amount] += dp[amount - *coin]; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 1 causes missing combinations for amount 0.
Starting from coin causes skipping smaller amounts.
✗ Incorrect
The inner loop should start from 0 to include all amounts up to target.
4fill in blank
hardFill both blanks to create a DP table for the knapsack problem.
DSA Rust
let mut dp = vec![vec![0; [1] + 1]; [2] + 1];
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping rows and columns causes logic errors.
Using values.len() instead of weights.len() for rows is incorrect.
✗ Incorrect
The DP table has rows for items (weights.len()) and columns for capacities (capacity + 1).
5fill in blank
hardFill all three blanks to complete the DP update for the knapsack problem.
DSA Rust
for i in 1..=[1] { for w in 0..=[2] { if weights[i - 1] <= w { dp[i][w] = dp[i - 1][w].max(dp[i - 1][w - [3]] + values[i - 1]); } else { dp[i][w] = dp[i - 1][w]; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using values.len() instead of weights.len() for items count.
Using capacity + 1 in loop range causes index errors.
Using values[i - 1] instead of weights[i - 1] in dp index.
✗ Incorrect
Loop over all items and capacities; use weights[i - 1] to check and update dp.
