Challenge - 5 Problems
Array Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting middle element from an array
What is the output of the following Rust code after deleting the middle element from the array?
DSA Rust
fn main() {
let mut arr = vec![10, 20, 30, 40, 50];
let mid = arr.len() / 2;
arr.remove(mid);
for val in &arr {
print!("{} ", val);
}
}Attempts:
2 left
💡 Hint
Remember that the middle index is calculated as length divided by 2, and the remove method deletes the element at that index.
✗ Incorrect
The array has 5 elements, so middle index is 2 (0-based). Removing element at index 2 removes 30. Remaining elements are 10, 20, 40, 50.
❓ Predict Output
intermediateResulting array after removing middle element from even-length array
What is the output of this Rust code after deleting the middle element from an even-length array?
DSA Rust
fn main() {
let mut arr = vec![5, 15, 25, 35, 45, 55];
let mid = arr.len() / 2;
arr.remove(mid);
for val in &arr {
print!("{} ", val);
}
}Attempts:
2 left
💡 Hint
For even length, middle index is length/2, which is the higher middle element index.
✗ Incorrect
Array length is 6, middle index is 3. Removing element at index 3 removes 35. Remaining elements are 5, 15, 25, 45, 55.
🔧 Debug
advancedIdentify the error in array deletion code
What error will this Rust code produce when trying to delete the middle element?
DSA Rust
fn main() {
let mut arr = vec![1, 2, 3, 4];
let mid = arr.len() / 2;
arr.remove(mid + 1);
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
Check the index used in remove method carefully.
✗ Incorrect
Array length is 4, mid = 2, so mid + 1 = 3. Index 3 is valid (indices 0-3). Removes element 4 at index 3. No panic. Output: [1, 2, 3].
❓ Predict Output
advancedOutput after multiple middle deletions
What is the output after deleting the middle element twice from this array?
DSA Rust
fn main() {
let mut arr = vec![2, 4, 6, 8, 10, 12, 14];
for _ in 0..2 {
let mid = arr.len() / 2;
arr.remove(mid);
}
for val in &arr {
print!("{} ", val);
}
}Attempts:
2 left
💡 Hint
Calculate middle index and remove element twice, updating array each time.
✗ Incorrect
Initial array length 7, mid=3, remove element at index 3 (8). Array becomes [2,4,6,10,12,14]. Next length 6, mid=3, remove element at index 3 (10). Array becomes [2,4,6,12,14]. Output prints 2 4 6 12 14
🧠 Conceptual
expertEffect of deleting middle element on array length and indexing
If you delete the middle element from an array of length n repeatedly until only one element remains, how many deletions will you perform and what will be the final element's index in the original array?
Attempts:
2 left
💡 Hint
Think about how many elements are removed and which element remains after repeatedly removing the middle.
✗ Incorrect
Each deletion removes one element, so to reduce from n to 1 element requires n-1 deletions. Removing at floor(len/2) repeatedly always preserves the first element (original index 0).
