Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the ways vector with 1 at index 0.
DSA Rust
let mut ways = vec![0; amount + 1]; ways[1] = 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Setting ways[amount] = 1 instead of ways[0] = 1
Forgetting to initialize ways[0]
Using an index out of range
✗ Incorrect
The ways vector should have 1 way to make amount 0, so ways[0] = 1.
2fill in blank
mediumComplete the code to iterate over each coin in the coins vector.
DSA Rust
for [1] in coins.iter() {
Attempts:
3 left
💡 Hint
Common Mistakes
Using the plural 'coins' instead of singular 'coin'
Using an index variable instead of iter()
Using an undefined variable
✗ Incorrect
We iterate over each coin by using coin as the variable name for each element.
3fill in blank
hardFix the error in the inner loop to update ways correctly.
DSA Rust
for x in [1]..=amount { ways[x] += ways[x - *coin]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 causing negative index
Using 1 instead of coin as start
Using multiplication instead of coin value
✗ Incorrect
The loop should start from the coin value to avoid negative indexing.
4fill in blank
hardFill both blanks to complete the function signature and return statement.
DSA Rust
fn count_ways(coins: Vec<usize>, [1]: usize) -> [2] { // function body }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter name
Returning Vec instead of usize
Using signed integer types
✗ Incorrect
The function takes amount as usize and returns usize representing number of ways.
5fill in blank
hardFill all three blanks to complete the main logic of updating ways.
DSA Rust
for [1] in coins.iter() { for x in [2]..=[3] { ways[x] += ways[x - *coin]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using coins instead of coin in loop
Starting inner loop from 0 instead of *coin
Using wrong upper bound
✗ Incorrect
We iterate over coins as coin, start inner loop from *coin to amount.
