Sequence Storage Trade-offs: Contiguous Arrays vs Pointer Chains
Linked ListsModel answerLast updated
Contrast contiguous arrays, linked lists, and unrolled lists for indexed reads, inserts, growth, traversal speed, and cache behavior, and say when each one wins.
You are choosing a storage layout for an ordered sequence of elements. Discuss the following three candidates and how their memory layouts affect real performance:
- A fixed-size array.
- A resizable contiguous array (a dynamic array or vector).
- A singly or doubly linked list, and its variant where each node stores several consecutive elements (an unrolled linked list).
Your answer should cover:
- Reading the element at index
i. - Inserting an element, both when you already hold a node or iterator at the target position and when you only have an index.
- Appending, including what happens when capacity runs out.
- Full traversal, and why asymptotically equal traversals can differ greatly in wall-clock time.
- Memory overhead per element.
- Why storing several values per linked node is attractive, and what it does and does not improve.
State clearly what each operation takes as input (an index, a node pointer, or a pointer to the predecessor), because the costs differ by input. Finish by saying which questions you would ask about the workload before choosing, and give a recommendation for at least two different workloads.
Hints
Hint 1
Separate the cost of finding a position from the cost of changing the structure once you are there. Write the two costs separately for every operation.
Hint 2
For a resizable array, think about an append sequence rather than a single append: a rare expensive copy is spread across many cheap ones. Also think about what happens to existing pointers and iterators when the buffer moves.
Hint 3
For the unrolled list, ask what happens to the number of pointer hops, the number of allocations, and the cache lines touched, and then check whether jumping to index i becomes constant time. It does not.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Core idea
The two structures differ in how they place elements in memory. An array puts elements side by side, so the address of element i is base + i * size. A linked list puts each element in its own node and connects nodes with pointers, so the only way to reach a position is to follow links. Almost every trade-off below comes from this difference.
Operation by operation
Indexed read, get(i)
- Array (fixed or resizable): O(1), one address computation and one load.
- Linked list: O(i), since you must follow
ipointers from the head (a doubly linked list with a tail pointer can start from the nearer end, but this is still O(n) in the worst case). - Unrolled list: O(n / k + k) for chunk size
k. Skip whole nodes by their counts, then index inside one chunk. This is better by a constant factor but not O(1).
Insertion Keep the input distinction explicit:
- Linked list, given the predecessor node (or the node itself in a doubly linked list): O(1), just rewire two pointers.
- Linked list, given only an index: O(i) to find the position, then O(1) to link. The total is O(n).
- Array, insert at index
i: shift every later element, O(n - i). Appending at the end is the cheap special case. - Unrolled list: find the node, then insert into its small array by shifting at most
kelements. If the node is full, split it in two. Cost is O(k) after locating the node.
A frequent mistake is to say "linked list insertion is O(1)" without saying that this assumes the position is already known. In practice the search often dominates.
Append and capacity growth
- Fixed-size array: appending past capacity is impossible. You must allocate a new array and copy, O(n).
- Resizable array: when full, allocate a larger buffer (typically 1.5x to 2x), copy, and free the old one. A single append can cost O(n), but over
mappends the total copying is bounded by a geometric series, so the amortized cost is O(1). Worst-case latency of one append is still O(n), which matters for real-time systems. Growth also invalidates pointers, references, and iterators to elements, because the buffer moves. - Linked list with a tail pointer: O(1) worst case, and existing node addresses stay stable.
Traversal All three are O(n), but the constants differ sharply. A contiguous array streams through memory, uses every byte of each fetched cache line, and is easy for the hardware prefetcher to predict. A linked list does a dependent load per node: the next address is unknown until the current node arrives, so the CPU cannot overlap the misses, and nodes may be scattered across the heap. On large lists the array can be several times to an order of magnitude faster in practice, even though both are "O(n)".
Memory overhead
- Array: just the elements, plus unused capacity (up to roughly 50% for a doubling strategy right after a growth).
- Singly linked list: one pointer per element (8 bytes on 64-bit), plus allocator header and alignment padding. For 4-byte integers, overhead can exceed the data itself. A doubly linked list doubles the pointer cost.
- Each node is a separate allocation, which is slow compared with one bulk allocation and fragments the heap.
Why unrolled lists exist
An unrolled linked list stores up to k consecutive elements per node, for example chunk sizes that fill one or a few cache lines.
What it improves:
- Fewer pointers per element (one per
kelements), so lower overhead. - Fewer allocations.
- Better locality: elements within a node are contiguous, so traversal touches roughly
n / kpointer hops instead ofn. - Insertions and deletions shift at most
kelements, and a full node splits without moving the rest of the sequence, so there is no global reallocation and the cost of growth is local.
What it does not give:
- No O(1) indexed access. Nodes can be partially filled, so you cannot compute which node holds index
i. You still walk nodes, O(n / k). - Added complexity: split and merge rules, minimum fill to avoid waste, and more edge cases.
- A choice of
kthat must balance shift cost against pointer overhead.
Questions to ask before deciding
- Are reads mostly by index, or sequential?
- Do insertions happen at positions we already hold (iterators, nodes), or must we search by index?
- How large is the data, and do cache effects matter?
- Must references or iterators stay valid across inserts and growth?
- Is worst-case latency or amortized throughput the target?
Recommendations
- Read-heavy, random access, mostly appends (for example a lookup table or a log buffer): a resizable array. O(1) indexing, amortized O(1) append, best cache behavior. Reserve capacity up front if the size is known.
- Many splices at known positions with stable references (for example an LRU cache, where a hash map holds node pointers and you move nodes to the front): a doubly linked list. O(1) removal and insertion with a handle, and addresses never change.
- Large ordered sequence with frequent middle edits and sequential scans (for example a text editor buffer): an unrolled list or a similar chunked structure (a rope or gap buffer are alternatives), which cuts shifting cost versus a flat array and cuts pointer overhead versus a plain list.
Common mistakes
- Claiming linked-list insertion is O(1) without stating the position is already known.
- Saying a resizable array append is O(1) without the word "amortized", or forgetting that iterators are invalidated on growth.
- Treating equal big-O traversal as equal speed and ignoring cache misses.
- Claiming an unrolled list gives constant-time indexing.
- Forgetting per-node allocator overhead when estimating memory use.
Summary table
| Operation | Array | Resizable array | Linked list | Unrolled list |
|---|---|---|---|---|
get(i) | O(1) | O(1) | O(i) | O(n/k + k) |
| Insert at known node | n/a | O(n) shift | O(1) | O(k) |
| Insert at index | O(n) | O(n) | O(n) | O(n/k + k) |
| Append | not possible when full | amortized O(1) | O(1) with tail | O(1) amortized-ish (split when full) |
| Traversal | O(n), fastest | O(n), fastest | O(n), slowest | O(n), near array speed |