LearnThatStack Ace your next interview
Arrays, Strings & Hash Tables · question
Question 5 of 85

Why does inserting into the middle of an array cost O(n)?

beginner
← All Arrays, Strings & Hash Tables questions
Re-explain
Visual ↓

Inserting in the middle forces every later element to move. An array keeps its values packed with no gaps. Making room at position k means shifting everything from k onward one slot to the right.

If you insert near the front of a million-element array, you shift almost a million elements. That shifting is the O(n) cost, and it grows with how much sits after the insertion point.

Appending at the end avoids this, because nothing follows the last element. Deleting from the middle costs the same, because shifting later elements left closes the gap. When you need frequent middle inserts, an array is often the wrong tool.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Arrays, Strings & Hash Tables One question at a time. Answer out loud, get graded, see what you missed.
See it - interactive diagram
Counting the moves for an insert and an append
def insert(a, n, k, v):
moves = 0
for i in range(n, k, -1):
a[i] = a[i - 1]
moves += 1
a[k] = v
print(moves, "moves")
a = ["A","B","C","D","E","_","_"]
insert(a, 5, 1, "X")
insert(a, 6, 6, "Y")
Array a_ marks a spare slot
k
ABCDE__
must shift rightspare slots
Variablesinside this insert call
n5
k1
moves0
Console
1step

The array packs five values together with no gaps. X belongs at index 1, so B, C, D and E must all shift right.

1 / 10

Inserting at index k shifts every value from k to the end, so the cost grows with the array. That is O(n). Appending shifts nothing.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • If order does not matter, how can you delete from the middle of an array without shifting? A strong answer moves the last element into the hole instead of shifting, then names the price. The order of elements is lost.
  • What happens when the dynamic array is already full and you insert in the middle? A strong answer says the array grows first, copying every element to a new block, and still has to shift. That insert stays O(n).
  • If your workload needs frequent middle inserts, what would you use instead, and what do you give up? Name a linked list or a balanced tree. Say that finding the position in a list is still O(n) and that you lose O(1) indexing and cache locality.
  • Shifting is O(n), so why do array inserts often beat linked list inserts in practice? A strong answer cites one bulk move over contiguous, cache-friendly memory, while a list has to walk scattered nodes just to reach position k.

What you can say

  1. Inserting in the middle of an array is O(n) because every element after the insertion point has to move.
  2. An array keeps its values packed side by side with no gaps, so there is no empty slot waiting at position k.
  3. To make room, you shift everything from k onward one slot to the right, and that work grows with how many elements sit after k.
  4. Insert near the front of a million-element array and you shift almost a million elements.
  5. Appending at the end avoids this because nothing follows the last element, but deleting from the middle costs the same, since you shift left to close the gap.
  6. So if I need frequent middle inserts, I would question whether an array is the right structure at all.

Weak answers to avoid

  • Blames finding the position Reaching index k in an array is O(1). The cost is moving the elements after k, so name the shift as the O(n) work.
  • Says every array insert is O(n) Appending at the end shifts nothing. Say the cost grows with how many elements sit after the insertion point.
  • Points to resizing as the cause Even with spare capacity, the later elements still have to move. Resizing is a separate cost. The shift is what makes a middle insert O(n).
  • Jumps to a linked list with no catch A list splices in O(1) only once you hold the node. Reaching position k is O(n) and you lose cache locality, so say both.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Arrays, Strings & Hash Tables cheatsheet.

← Back to all Arrays, Strings & Hash Tables questions
Pro · $10/mo

74 of 85 Arrays, Strings & Hash Tables answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime