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 odd-length array
What is the output of the following JavaScript code after deleting the middle element from the array?
DSA Javascript
let arr = [10, 20, 30, 40, 50]; let mid = Math.floor(arr.length / 2); arr.splice(mid, 1); console.log(arr);
Attempts:
2 left
💡 Hint
Remember that splice removes elements starting at the given index.
✗ Incorrect
The middle index for an array of length 5 is 2 (zero-based). Removing the element at index 2 (which is 30) results in [10, 20, 40, 50].
❓ Predict Output
intermediateOutput after deleting middle element from an even-length array
What is the output of the following JavaScript code after deleting the middle element from the array?
DSA Javascript
let arr = [5, 15, 25, 35, 45, 55]; let mid = Math.floor(arr.length / 2); arr.splice(mid, 1); console.log(arr);
Attempts:
2 left
💡 Hint
For even length, mid is the lower middle index.
✗ Incorrect
The array length is 6, so mid is 3. Removing element at index 3 (which is 35) results in [5, 15, 25, 45, 55].
🔧 Debug
advancedIdentify the error in deleting middle element
What error will the following code produce when trying to delete the middle element of an array?
DSA Javascript
let arr = [1, 2, 3, 4, 5]; let mid = arr.length / 2; arr.splice(mid, 1); console.log(arr);
Attempts:
2 left
💡 Hint
Check the type of mid and how splice handles non-integer indices.
✗ Incorrect
mid is 2.5 (not an integer). splice converts it to integer by truncation (2). So it deletes element at index 2 (which is 3). The output is [1, 2, 4, 5]. But since mid is not integer, this is unintended behavior, not an error.
❓ Predict Output
advancedOutput after deleting middle element using slice and concat
What is the output of the following JavaScript code that deletes the middle element without using splice?
DSA Javascript
let arr = [2, 4, 6, 8, 10]; let mid = Math.floor(arr.length / 2); let newArr = arr.slice(0, mid).concat(arr.slice(mid + 1)); console.log(newArr);
Attempts:
2 left
💡 Hint
slice(start, end) excludes the end index element.
✗ Incorrect
mid is 2, so slice(0,2) gives [2,4], slice(3) gives [8,10]. Concatenating results in [2,4,8,10].
🧠 Conceptual
expertChoosing the correct middle index for deletion in even-length arrays
In an array with even length, which index should be chosen as the middle to delete the element closest to the center?
Attempts:
2 left
💡 Hint
Consider zero-based indexing and which element is closer to the center.
✗ Incorrect
For even length arrays, the two middle elements are at indices length/2 - 1 and length/2. The element at length/2 - 1 is the lower middle and closer to the center from the left side, often considered the middle for deletion.
