What happens when you delete an element from the middle of an array?
The elements after the deleted element shift one position to the left to fill the gap, reducing the array size by one.
Click to reveal answer
beginner
Why do elements shift after deleting from the middle of an array?
Because arrays have fixed order and continuous memory, shifting keeps elements contiguous and maintains order.
Click to reveal answer
beginner
In Rust, which method can be used to remove an element at a specific index in a vector?
The `remove(index)` method removes the element at the given index and shifts elements left automatically.
Click to reveal answer
intermediate
What is the time complexity of deleting an element from the middle of an array?
It is O(n) because all elements after the deleted one must be shifted left by one position.
Click to reveal answer
beginner
Show a simple Rust code snippet to delete the middle element from a vector `[10, 20, 30, 40, 50]`.
let mut v = vec![10, 20, 30, 40, 50];
v.remove(2); // removes element 30
println!("{:?}", v); // prints [10, 20, 40, 50]
Click to reveal answer
What happens to elements after deleting an element at index 3 in an array?
AArray elements are reversed
BElements after index 3 shift left by one position
CNo elements shift, array size stays the same
DElements before index 3 shift right by one position
✗ Incorrect
Deleting at index 3 causes elements after index 3 to shift left to fill the gap.
Which Rust vector method removes an element at a specific index?
Aremove(index)
Bpop()
Cpush(value)
Dinsert(index, value)
✗ Incorrect
`remove(index)` deletes the element at the given index and shifts elements left.
What is the time complexity of deleting an element from the middle of an array?
AO(n)
BO(log n)
CO(1)
DO(n log n)
✗ Incorrect
Because elements after the deleted one must shift left, it takes O(n) time.
If you delete the middle element from `[5, 10, 15, 20, 25]`, what is the resulting array?
A[10, 15, 20, 25]
B[5, 10, 15, 20]
C[5, 10, 20, 25]
D[5, 15, 20, 25]
✗ Incorrect
Deleting the middle element 15 (index 2) shifts elements left, resulting in [5, 10, 20, 25].
Why is deleting from the middle of an array less efficient than deleting from the end?
ABecause deleting from the end requires shifting
BBecause arrays are unordered
CBecause arrays are linked lists
DBecause shifting elements is required
✗ Incorrect
Deleting from the middle requires shifting elements left, which takes more time than deleting from the end.
Explain how deleting an element from the middle of an array works and why elements shift.
Think about how arrays keep elements next to each other.
You got /4 concepts.
Write a Rust code snippet to remove the middle element from a vector and print the result.
Use vec![...] and remove method.
You got /4 concepts.
Practice
(1/5)
1. What happens when you delete the middle element from an array with an odd number of elements?
easy
A. The array size remains the same but the middle element is set to zero.
B. The middle element is removed and the array size decreases by one.
C. The last element is removed instead of the middle element.
D. The array is reversed after deletion.
Solution
Step 1: Identify the middle index in an odd-length array
The middle index is calculated as length divided by 2 (integer division).
Step 2: Remove the element at the middle index
Deleting this element reduces the array size by one and shifts elements after it left.
Final Answer:
The middle element is removed and the array size decreases by one. -> Option B
Quick Check:
Deleting middle element shrinks array [OK]
Hint: Middle index = length / 2 for odd arrays [OK]
Common Mistakes:
Thinking array size stays the same after deletion
Removing last element instead of middle
Not shifting elements after deletion
2. Which Rust code snippet correctly deletes the middle element from a vector v with odd length?
easy
A. v.pop(v.len() / 2);
B. v.delete(v.len() / 2);
C. v.remove(v.len() / 2);
D. v.erase(v.len() / 2);
Solution
Step 1: Identify Rust method to remove element by index
Rust's Vec type uses remove(index) to delete element at given index.
Step 2: Calculate middle index and call remove
Middle index is v.len() / 2. Calling v.remove(v.len() / 2) deletes middle element.
Final Answer:
v.remove(v.len() / 2); -> Option C
Quick Check:
Use remove(index) to delete element [OK]
Hint: Use Vec::remove(index) to delete element [OK]
Common Mistakes:
Using non-existent methods like delete or erase
Using pop which removes last element only
Passing index to pop which takes no arguments
3. What is the output of this Rust code?
let mut v = vec![10, 20, 30, 40, 50];
v.remove(v.len() / 2);
println!("{:?}", v);
medium
A. [10, 30, 40, 50]
B. [10, 20, 30, 40]
C. [20, 30, 40, 50]
D. [10, 20, 40, 50]
Solution
Step 1: Calculate middle index of vector
Vector length is 5, so middle index = 5 / 2 = 2.
Step 2: Remove element at index 2 and print vector
Element at index 2 is 30, removed. Remaining vector is [10, 20, 40, 50].
Final Answer:
[10, 20, 40, 50] -> Option D
Quick Check:
Removed middle element 30, vector shrinks [OK]
Hint: Remove element at len()/2 index for middle deletion [OK]
Common Mistakes:
Removing wrong index element
Confusing index with value
Expecting vector size unchanged
4. Identify the error in this Rust code that tries to delete the middle element of a vector v:
let mut v = vec![1, 2];
v.remove(v.len() / 2 + 1);
println!("{:?}", v);
medium
A. Index used for removal is off by one, causing panic.
B. Vector cannot be mutable for removal.
C. remove() method does not exist for vectors.
D. The println! macro is used incorrectly.
Solution
Step 1: Calculate middle index for even-length vector
Vector length is 2, middle index should be 2 / 2 = 1.
Step 2: Check removal index used in code
Code uses index 1 + 1 = 2, which is out of bounds.
Step 3: Understand consequences
Index 2 >= length 2 causes runtime panic (out of bounds).
Final Answer:
Index used for removal is off by one, causing panic. -> Option A
Quick Check:
Use correct middle index = len()/2 [OK]
Hint: Middle index is len()/2, no +1 for removal [OK]
Common Mistakes:
Adding +1 to middle index incorrectly
Trying to remove from immutable vector
Misusing remove method name
5. Given a vector v of even length, how can you delete the lower middle element (the one at index len()/2 - 1) and then print the updated vector in Rust?
hard
A. v.remove(v.len() / 2 - 1); println!("{:?}", v);
B. v.remove(v.len() / 2); println!("{:?}", v);
C. v.pop(); println!("{:?}", v);
D. v.remove(v.len() / 2 + 1); println!("{:?}", v);
Solution
Step 1: Identify lower middle index for even-length vector
For even length, lower middle index is len()/2 - 1.
Step 2: Remove element at lower middle index and print vector
Use v.remove(v.len() / 2 - 1) to delete lower middle element, then print with println!("{:?}", v);.
Final Answer:
v.remove(v.len() / 2 - 1); println!("{:?}", v); -> Option A
Quick Check:
Remove lower middle with len()/2 - 1 [OK]
Hint: For even length, lower middle = len()/2 - 1 [OK]
Common Mistakes:
Removing upper middle instead of lower middle
Using pop which removes last element
Adding +1 to index incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.