Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert the value 10 at the middle index of the vector.
DSA Rust
let mut numbers = vec![1, 2, 3, 4]; numbers.insert([1], 10); println!("{:?}", numbers);
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 or 3 instead of 2 causes insertion not exactly in the middle.
Using index 0 inserts at the start, not middle.
✗ Incorrect
The middle index of a vector with 4 elements is 2. Inserting at index 2 places the new element in the middle.
2fill in blank
mediumComplete the code to insert the value 99 at the middle index of the vector.
DSA Rust
let mut values = vec![5, 6, 7, 8, 9]; values.insert([1], 99); println!("{:?}", values);
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 3 inserts after the middle, not at the middle.
Using index 1 inserts before the middle.
✗ Incorrect
For a vector with 5 elements, the middle index is 2 (integer division). Inserting at index 2 places the value in the middle.
3fill in blank
hardFix the error in the code to correctly insert 42 at the middle index.
DSA Rust
let mut data = vec![10, 20, 30, 40]; let mid = data.len() / [1]; data.insert(mid, 42); println!("{:?}", data);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as divisor returns full length, which appends to the end instead of inserting at the middle.
Using 3 or 4 gives wrong index.
✗ Incorrect
To find the middle index, divide the length by 2. Using 2 correctly calculates the middle.
4fill in blank
hardFill both blanks to insert 100 at the middle index of the vector.
DSA Rust
let mut arr = vec![2, 4, 6, 8, 10, 12]; let middle = arr.len() [1] 2; arr.insert(middle, [2]); println!("{:?}", arr);
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of division for middle index.
Inserting wrong value.
✗ Incorrect
Dividing length by 2 finds the middle index. Inserting 100 places the value at the middle.
5fill in blank
hardFill all three blanks to insert 77 at the middle index of the vector.
DSA Rust
let mut nums = vec![11, 22, 33, 44, 55]; let mid_index = nums.len() [1] 2; nums.insert([2], [3]); println!("{:?}", nums);
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed index instead of middle index variable.
Inserting wrong value.
✗ Incorrect
Divide length by 2 to get middle index, then insert 77 at that index.
