Bird
Raised Fist0
DSA Typescriptprogramming

Array Insertion at Middle Index in DSA Typescript

Choose your learning style10 modes available
Mental Model
To insert a new item in the middle of an array, we make space by moving items after the middle one step to the right.
Analogy: Imagine a row of chairs with people sitting. To add a new person in the middle, everyone from that spot onward stands up and moves one chair to the right, freeing a chair in the middle.
Index: 0   1   2   3   4
Array: [1] [2] [3] [4] [5]
               ↑ middle index
Dry Run Walkthrough
Input: array: [1, 2, 3, 4, 5], insert value 99 at middle index 2
Goal: Insert 99 at index 2, shifting elements right to keep order
Step 1: Identify middle index 2 where insertion happens
Index: 0   1   2   3   4
Array: [1] [2] [3] [4] [5]
               ↑ insert here
Why: We need to insert at index 2, so we prepare to shift elements from here
Step 2: Shift element at index 4 (5) to index 5 (new slot)
Index: 0   1   2   3   4   5
Array: [1] [2] [3] [4] [5] [5]
               ↑ insert here
Why: Make space at index 4 by moving last element right
Step 3: Shift element at index 3 (4) to index 4
Index: 0   1   2   3   4   5
Array: [1] [2] [3] [4] [4] [5]
               ↑ insert here
Why: Continue shifting elements right to free index 3
Step 4: Shift element at index 2 (3) to index 3
Index: 0   1   2   3   4   5
Array: [1] [2] [3] [3] [4] [5]
               ↑ insert here
Why: Shift element at insertion index to right to free index 2
Step 5: Insert value 99 at index 2
Index: 0   1   2   3   4   5
Array: [1] [2] [99] [3] [4] [5]
Why: Place new value in the freed middle position
Result:
Index: 0   1    2    3    4    5
Array: [1] [2] [99] [3] [4] [5]
Annotated Code
DSA Typescript
class ArrayInsertion {
  static insertAtMiddle(arr: number[], value: number): number[] {
    const middle = Math.floor(arr.length / 2);
    // Shift elements right from end to middle
    for (let i = arr.length - 1; i >= middle; i--) {
      arr[i + 1] = arr[i]; // move element right to make space
    }
    arr[middle] = value; // insert new value at middle
    return arr;
  }
}

// Driver code
const arr = [1, 2, 3, 4, 5, undefined];
const result = ArrayInsertion.insertAtMiddle(arr, 99);
console.log(result.join(' '));
const middle = Math.floor(arr.length / 2);
Calculate middle index for insertion
for (let i = arr.length - 1; i >= middle; i--) {
Loop backward from last element to middle to shift elements right
arr[i + 1] = arr[i]; // move element right to make space
Shift current element one position right
arr[middle] = value; // insert new value at middle
Place new value at the freed middle index
OutputSuccess
1 2 99 3 4 5
Complexity Analysis
Time: O(n) because we may need to shift up to half the elements to the right
Space: O(1) because insertion is done in-place without extra arrays
vs Alternative: Compared to creating a new array and copying elements, this in-place shift saves space but still requires shifting elements which is O(n)
Edge Cases
empty array
Inserts value at index 0 without shifting
DSA Typescript
const middle = Math.floor(arr.length / 2);
array with one element
Inserts value at index 0, shifts the single element right
DSA Typescript
for (let i = arr.length - 1; i >= middle; i--) {
array with even length
Inserts at lower middle index (length/2), shifting elements right
DSA Typescript
const middle = Math.floor(arr.length / 2);
When to Use This Pattern
When you need to add an element inside an array without losing order, especially in the middle, use the shifting pattern to make space.
Common Mistakes
Mistake: Shifting elements left to right causing overwriting data
Fix: Shift elements right to left (backwards) to avoid overwriting before moving
Summary
Inserts a new value into the middle of an array by shifting elements to the right.
Use when you want to add an element inside an array without losing existing order.
Shift elements from the end towards the middle to safely make space for insertion.

Practice

(1/5)
1. What does inserting an element at the middle index of an array mean?
easy
A. Adding the element exactly in the center position of the array
B. Adding the element at the start of the array
C. Adding the element at the end of the array
D. Replacing the middle element with a new element

Solution

  1. Step 1: Understand array indexing

    Arrays have positions starting from 0 to length-1. The middle index is usually length/2 (rounded down).
  2. Step 2: Define insertion at middle

    Inserting at middle means placing the new element at this middle index, shifting elements after it to the right.
  3. Final Answer:

    Adding the element exactly in the center position of the array -> Option A
  4. Quick Check:

    Middle insertion = center position [OK]
Hint: Middle index = array length divided by 2 [OK]
Common Mistakes:
  • Confusing insertion with replacement
  • Inserting at start or end instead of middle
  • Not shifting elements after insertion
2. Which of the following TypeScript code snippets correctly inserts the value 99 at the middle index of array arr?
easy
A. arr.unshift(99);
B. arr.push(99);
C. arr.splice(Math.floor(arr.length / 2), 0, 99);
D. arr[ Math.floor(arr.length / 2) ] = 99;

Solution

  1. Step 1: Understand splice method

    splice(index, 0, value) inserts value at index without removing elements.
  2. Step 2: Check other options

    push adds at end, unshift adds at start, direct assignment replaces element.
  3. Final Answer:

    arr.splice(Math.floor(arr.length / 2), 0, 99); -> Option C
  4. Quick Check:

    splice with 0 delete count inserts [OK]
Hint: Use splice(index, 0, value) to insert without removal [OK]
Common Mistakes:
  • Using push or unshift instead of splice
  • Replacing element instead of inserting
  • Incorrect index calculation
3. What is the output of the following TypeScript code?
const arr = [10, 20, 30, 40];
arr.splice(Math.floor(arr.length / 2), 0, 25);
console.log(arr);
medium
A. [10, 20, 30, 25, 40]
B. [10, 20, 25, 30, 40]
C. [10, 25, 20, 30, 40]
D. [25, 10, 20, 30, 40]

Solution

  1. Step 1: Calculate middle index

    arr.length = 4, middle index = Math.floor(4/2) = 2.
  2. Step 2: Insert 25 at index 2

    splice(2, 0, 25) inserts 25 before element at index 2 (which is 30), shifting elements right.
  3. Final Answer:

    [10, 20, 25, 30, 40] -> Option B
  4. Quick Check:

    Inserted 25 at index 2 [OK]
Hint: Middle index = floor(length/2), splice inserts at that index [OK]
Common Mistakes:
  • Inserting at wrong index
  • Replacing element instead of inserting
  • Misunderstanding splice parameters
4. What is the output of the following TypeScript code?
const nums = [1, 2, 3, 4, 5];
const mid = nums.length / 2;
nums.splice(mid, 0, 50);
console.log(nums);
medium
A. [1, 50, 2, 3, 4, 5]
B. [1, 2, 3, 4, 5, 50]
C. [1, 2, 3, 50, 4, 5]
D. [1, 2, 50, 3, 4, 5]

Solution

  1. Step 1: Check mid calculation

    nums.length = 5, mid = 5 / 2 = 2.5 (a float).
  2. Step 2: Understand splice behavior

    splice coerces 2.5 to integer 2 (ToInteger(2.5) = 2). Inserts 50 at index 2, shifting 3,4,5 right: [1, 2, 50, 3, 4, 5].
  3. Final Answer:

    [1, 2, 50, 3, 4, 5] -> Option D
  4. Quick Check:

    Float index coerced to 2 [OK]
Hint: Always floor length/2 for middle index [OK]
Common Mistakes:
  • Confusing with unshift (insert at start)
  • Confusing with push (append at end)
  • Inserting at index 3 instead of 2
5. Given an array arr = [5, 10, 15, 20, 25], write a TypeScript function insertMiddle that inserts a given value exactly in the middle of the array. What will be the array after calling insertMiddle(arr, 12)?
hard
A. [5, 10, 12, 15, 20, 25]
B. [5, 10, 15, 12, 20, 25]
C. [12, 5, 10, 15, 20, 25]
D. [5, 10, 15, 20, 25, 12]

Solution

  1. Step 1: Calculate middle index for odd length

    arr.length = 5, middle index = Math.floor(5/2) = 2.
  2. Step 2: Insert 12 at index 2

    Using splice(2, 0, 12) inserts 12 before element at index 2 (which is 15), shifting elements right.
  3. Final Answer:

    [5, 10, 12, 15, 20, 25] -> Option A
  4. Quick Check:

    Inserted 12 at middle index 2 [OK]
Hint: Use Math.floor(length/2) for middle insertion [OK]
Common Mistakes:
  • Inserting at wrong index for odd length
  • Appending instead of inserting
  • Replacing element instead of inserting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard