Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the count array with zeros.
DSA Rust
let mut count = vec![[1]; max + 1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 causes wrong counts.
Using max as initial value fills with wrong numbers.
✗ Incorrect
The count array must start with zeros to count occurrences correctly.
2fill in blank
mediumComplete the code to count each element's frequency.
DSA Rust
for &num in arr.iter() { count[[1]] += 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using count or arr as index causes errors.
Using an undefined variable like index.
✗ Incorrect
We use the number itself as index to increment its count.
3fill in blank
hardFix the error in updating the count array to cumulative counts.
DSA Rust
for i in 1..=max { count[i] = count[i] [1] count[i - 1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication changes counts incorrectly.
Division is invalid here.
✗ Incorrect
We add previous counts to get cumulative counts.
4fill in blank
hardFill both blanks to place elements in output array in stable order.
DSA Rust
for &num in arr.iter().rev() { output[count[[1]] [2] 1] = num; count[num] -= 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 1 causes index out of range.
Using count or wrong index causes errors.
✗ Incorrect
Use num as index and subtract 1 to get correct position.
5fill in blank
hardFill all three blanks to copy sorted elements back to original array.
DSA Rust
let mut i = 0; while i < arr.len() { arr[[1]] = output[[2]]; [3] += 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using count instead of i causes wrong copying.
Not incrementing index causes infinite loop.
✗ Incorrect
Use the loop index i to copy elements from output to arr.
