Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a value at a specific index in a vector.
DSA Rust
let mut v = vec![1, 2, 4, 5]; v.insert([1], 3); println!("{:?}", v);
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 3 inserts after 4, not between 2 and 4.
Using index 1 inserts before 2, not in the middle.
✗ Incorrect
The value 3 should be inserted at index 2 to appear between 2 and 4.
2fill in blank
mediumComplete the code to insert the value 10 at the middle index of the vector.
DSA Rust
let mut numbers = vec![5, 7, 9, 11, 13]; let mid = numbers.len() / [1]; numbers.insert(mid, 10); println!("{:?}", numbers);
Attempts:
3 left
💡 Hint
Common Mistakes
Using length directly instead of dividing by 2.
Dividing by 3 or 4 gives wrong middle position.
✗ Incorrect
Dividing length by 2 gives the middle index for insertion.
3fill in blank
hardFix the error in the code to insert 20 at index 1 in the vector.
DSA Rust
let mut vals = vec![10, 30, 40]; vals.insert([1], 20); println!("{:?}", vals);
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 2 inserts after 30, not between 10 and 30.
Using index 3 causes panic because it's out of bounds.
✗ Incorrect
Inserting at index 1 places 20 between 10 and 30.
4fill in blank
hardFill both blanks to insert 15 at the middle of the vector and print the updated vector.
DSA Rust
let mut data = vec![10, 20, 30, 40]; let mid = data.len() [1] 2; data.insert(mid, [2]); println!("{:?}", data);
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of division for middle index.
Inserting wrong value instead of 15.
✗ Incorrect
Divide length by 2 to find middle index, then insert 15 there.
5fill in blank
hardFill all three blanks to insert 25 at the middle index of the vector and print the result.
DSA Rust
let mut nums = vec![10, 20, 30, 40, 50]; let middle = nums.len() [1] 2; nums.insert(middle, [2]); println!("{:?}", [3]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of division for middle index.
Printing wrong variable name.
Inserting wrong value.
✗ Incorrect
Divide length by 2 for middle index, insert 25 there, and print nums.
