Challenge - 5 Problems
Array Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after deleting the middle element?
Given the array
[10, 20, 30, 40, 50], the middle element is removed. What is the resulting array?DSA Typescript
const arr = [10, 20, 30, 40, 50]; const mid = Math.floor(arr.length / 2); arr.splice(mid, 1); console.log(arr);
Attempts:
2 left
💡 Hint
Remember that array indices start at 0, and splice removes elements starting at the given index.
✗ Incorrect
The middle index for an array of length 5 is 2 (0-based). Removing the element at index 2 (which is 30) results in [10, 20, 40, 50].
❓ Predict Output
intermediateOutput after deleting middle element from even-length array
What is the output after deleting the middle element from
[5, 15, 25, 35, 45, 55] using the code below?DSA Typescript
const arr = [5, 15, 25, 35, 45, 55]; const mid = Math.floor(arr.length / 2); arr.splice(mid, 1); console.log(arr);
Attempts:
2 left
💡 Hint
For even length arrays, Math.floor(length/2) gives the higher middle index.
✗ Incorrect
Array length is 6, so mid = 3. Removing element at index 3 (which is 35) results in [5, 15, 25, 45, 55].
🔧 Debug
advancedPotential issue when not using Math.floor for middle index
Consider this code snippet that tries to delete the middle element from an array. What is the potential issue with using arr.length / 2 directly?
DSA Typescript
const arr = [1, 2, 3, 4, 5]; const mid = arr.length / 2; arr.splice(mid, 1); console.log(arr);
Attempts:
2 left
💡 Hint
Check the type of mid; best practices recommend explicit integer conversion.
✗ Incorrect
arr.length / 2 results in 2.5 (float). Although JS truncates it to 2 correctly here, using Math.floor(arr.length / 2) ensures an explicit integer index for clarity and reliability.
🧠 Conceptual
advancedWhat is the time complexity of deleting the middle element from an array?
If you delete the middle element from an array of size n, what is the time complexity of this operation?
Attempts:
2 left
💡 Hint
Think about what happens to elements after the removed one.
✗ Incorrect
Deleting an element in the middle requires shifting all elements after it one position left, which takes O(n) time.
🚀 Application
expertAfter multiple middle deletions, what is the final array?
Starting with
[2, 4, 6, 8, 10, 12, 14], you delete the middle element 3 times in a row using the same method: find middle index as Math.floor(length/2) and remove that element. What is the final array?DSA Typescript
let arr = [2, 4, 6, 8, 10, 12, 14]; for (let i = 0; i < 3; i++) { const mid = Math.floor(arr.length / 2); arr.splice(mid, 1); } console.log(arr);
Attempts:
2 left
💡 Hint
Perform each deletion step by step, updating the array each time.
✗ Incorrect
Step 1: length 7, mid=3, remove 8 → [2,4,6,10,12,14]
Step 2: length 6, mid=3, remove 10 → [2,4,6,12,14]
Step 3: length 5, mid=2, remove 6 → [2,4,12,14]
