Lesson 5 of 30
An array is a row of numbered boxes sitting side by side in memory. That single picture explains why reading is instant but inserting in the middle is slow.
11 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
This topic is about judgment and practical tradeoffs more than a single right answer — a mock interview is the better way to test yourself on it.
Be interviewed on this →Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Picture a shelf of library books, all pushed right up against each other with no gaps, numbered 0, 1, 2, 3, and so on from left to right. That shelf is an array. Each book sits in its own numbered spot, and the numbers are called indexes.
Because the books sit side by side with no gaps, the librarian can jump straight to spot #5 without checking spots #1 through #4 first. They know exactly how far #5 is from the start of the shelf, so they walk there directly. That's why reading array[5] in code is instant, no matter how long the array is — it's not a search, it's a direct walk to a known spot. This is the O(1) access you met two lessons ago.
Now picture inserting a new book into the middle of that shelf — say, at spot #3. There's no gap there. Every book from #3 onward has to slide one spot to the right to make room, all the way to the end of the shelf. The more books after spot #3, the more sliding. That's why inserting in the middle of an array is slow for a big array — it's O(n), because in the worst case, everything after the insertion point has to move.
This one picture — a row of numbered boxes with no gaps — explains almost everything confusing about arrays. Fast reads, because you can walk straight to a known spot. Slow middle-inserts, because there's no room without shifting everything over.
const shelf = ["Book A", "Book B", "Book C", "Book D", "Book E"];
// Reading spot #3 is instant — a direct walk to a known position.
// This doesn't get slower even if the shelf has a million books.
console.log(shelf[3]); // "Book D"
// Inserting at spot #2 means everything from #2 onward
// has to slide one spot to the right to make room.
shelf.splice(2, 0, "New Book");
console.log(shelf);
// ["Book A", "Book B", "New Book", "Book C", "Book D", "Book E"]
// "Book C", "Book D", "Book E" all had to shift over by one spot.
push() is much cheaper than inserting near the front.This "row of numbered boxes" picture is also why the time-space trade-off lesson's caching idea works so well alongside arrays — a cache is often just another array (or an array-like object), read from directly by position instead of searched. The next lesson takes this same picture and applies it to something you already use constantly: strings.