Module 0 · Lesson 2 · ~25 min

Memory — why arrays beat linked lists in practice

You write two functions. Same Big-O. One runs in 5ms; the other runs in 500ms. Both are O(n). Welcome to memory. This is the lesson where you stop trusting Big-O blindly and start asking the second question every senior engineer asks: how is this stored?

The metaphor: filing cabinet vs. sticky notes

Imagine two ways to keep a list of phone numbers.

Filing cabinet (an array). A long drawer with numbered slots. Slot 0, slot 1, slot 2… each slot is the same size and they're physically next to each other. To get slot 47, you walk to the cabinet, count 47 slots from the front, and pull. Direct addressing.

Scattered sticky notes (a linked list). Each phone number is on a sticky note somewhere in the building, and each note has scrawled on it the location of the next note. To get the 47th, you find the first sticky, read where to go for the second, drive there, read where to go for the third, drive there, and so on. Pointer chasing.

The metaphor

Both structures hold "the same list of numbers." The textbook says "linked lists insert in O(1)." True. But to find the spot to insert, you've already walked the building. The cabinet wins almost every real workload.

Contiguous vs. scattered — what hardware actually rewards

Real CPUs don't fetch memory one byte at a time. They fetch cache lines — typically 64 bytes — and stash them in fast on-chip memory. Reading any byte in that 64-byte block is then nearly free for the next few hundred cycles.

An array of 8-byte integers gets you 8 elements per cache line, all loaded together. Walking the array is mostly free after every 8th element. A linked list, with each node allocated independently somewhere on the heap, costs you a cache miss per node. On modern hardware, a cache miss is hundreds of times slower than a cache hit.

The lie inside Big-O

Big-O counts operations. It assumes every memory access costs the same. Real machines say: an L1 hit is ~1 ns, a main-memory miss is ~100 ns. Two algorithms with the same Big-O can differ by 100× because of cache behavior alone.

Visualize it

The array on the left is contiguous memory — a single block. The linked list on the right is the same data, but each node sits at an unrelated address.

Cache lines — why arrays beat linked lists in practice

The CPU doesn't fetch one byte at a time. It fetches a 64-byte cache line — typically 8 elements at once if they're 8-byte values. Here's a 16-element array. Each color stripe is one cache line. Click Walk array and notice the misses cluster at line boundaries while the rest are free hits. Click Random hops and watch every access miss — that's what a linked list traversal looks like to your CPU.

The hidden cost

An L1 cache hit is ~1ns. A main-memory miss is ~100ns. A linked-list traversal of n elements is ~n cache misses — 100× slower than the same work on an array even when both are "O(n)." Big-O can't see this; your wall clock can.

Why list.append is amortized O(1) in Python

Here's the thing that confused everyone in college. Python lists are arrays under the hood. Arrays need a fixed size. So how does append not require copying the whole array on every push?

Answer: Python doesn't allocate exactly the size you need. It allocates more — a "capacity" — and only copies into a bigger allocation when capacity runs out. The standard trick is doubling: when the array is full, allocate one twice as big and copy.

The cost analysis: across n appends, the copies sum to n + n/2 + n/4 + … = 2n. Total work is O(n). Per append on average: O(1). That's amortized O(1): any single append might be expensive, but the average over a sequence is constant.

The two questions to ask about every data structure

  1. How is it laid out in memory? Contiguous (cache-friendly) or scattered (pointer-heavy)?
  2. What does each operation cost — worst, average, amortized? Don't just say "O(1)" — say which O(1).
StructureLayoutRandom accessInsert at endInsert at frontSearch
Array (Python list, JS Array, std::vector)contiguousO(1)amortized O(1)O(n)O(n)
Linked listscatteredO(n)O(1) if you have the tailO(1)O(n)
Hash table (dict, Map)contiguous bucket array + scattered chains—amortized O(1)—amortized O(1)
Balanced BST (TreeMap, std::map)scattered—O(log n)—O(log n)
Interview reality

"Why use an array over a linked list?" is a classic warm-up. The textbook answer is "random access is O(1)." The L5 answer adds: "and contiguous memory means cache-friendly traversal — in practice, even an O(n) array operation is often faster than the equivalent linked-list operation because of cache locality."

The exception: when linked lists actually win

Don't completely write linked lists off. They earn their keep when:

For interview problems, you'll see linked lists almost exclusively as a way to test pointer-manipulation skills, not because they're the right tool. We'll go deep on that pattern in Module 3.

Notebook prompt