Challenge - 5 Problems
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting at the middle index?
Consider the following JavaScript code that inserts a value into the middle of an array. What will be the printed array after the insertion?
DSA Javascript
const arr = [10, 20, 30, 40, 50]; const middleIndex = Math.floor(arr.length / 2); arr.splice(middleIndex, 0, 25); console.log(arr);
Attempts:
2 left
💡 Hint
Remember that splice inserts elements starting at the given index, shifting existing elements to the right.
✗ Incorrect
The middle index is 2 (floor of 5/2). Inserting 25 at index 2 shifts elements from index 2 onwards to the right.
❓ Predict Output
intermediateWhat is the output after inserting in an even-length array?
Given the code below, what will be the array printed after inserting 15 at the middle index?
DSA Javascript
const arr = [5, 10, 20, 25]; const middleIndex = Math.floor(arr.length / 2); arr.splice(middleIndex, 0, 15); console.log(arr);
Attempts:
2 left
💡 Hint
For even length arrays, middle index is floor of length/2, which is the first of the two middle positions.
✗ Incorrect
Length is 4, middle index is 2. Inserting 15 at index 2 shifts elements from index 2 onwards to the right.
🔧 Debug
advancedWhy does this insertion code not insert at the middle?
The code below is intended to insert 100 at the middle index of the array. Why does it not work as expected?
DSA Javascript
const arr = [1, 2, 3, 4, 5]; const middleIndex = arr.length / 2; arr.splice(middleIndex, 0, 100); console.log(arr);
Attempts:
2 left
💡 Hint
Check the value and type of middleIndex before using it in splice.
✗ Incorrect
middleIndex is 2.5 (float). splice converts it to integer by truncation, so insertion happens at index 2 instead of 3, which may not be the intended middle.
❓ Predict Output
advancedWhat is the output after multiple insertions at middle?
What will be the array after running this code that inserts multiple values at the middle index sequentially?
DSA Javascript
const arr = [1, 2, 3, 4]; let middleIndex = Math.floor(arr.length / 2); arr.splice(middleIndex, 0, 10); middleIndex = Math.floor(arr.length / 2); arr.splice(middleIndex, 0, 20); console.log(arr);
Attempts:
2 left
💡 Hint
Remember to recalculate the middle index after each insertion because array length changes.
✗ Incorrect
After first insertion at index 2, array is [1, 2, 10, 3, 4]. New length is 5, middle index is 2 again. Inserting 20 at index 2 shifts 10 and others right, resulting in [1, 2, 20, 10, 3, 4].
🧠 Conceptual
expertWhat is the time complexity of inserting an element at the middle of a JavaScript array?
Consider inserting an element at the middle index of a JavaScript array using splice. What is the time complexity of this operation in terms of the array length n?
Attempts:
2 left
💡 Hint
Think about what happens to elements after the insertion point.
✗ Incorrect
Inserting at the middle requires shifting half of the elements to the right, which is proportional to n, so time complexity is O(n).
