DevPrep

All topics › Algorithms & data structures

Array vs linked list — what's the trade-off?

Junior PRO

The gist

An array stores elements contiguously: O(1) index access and excellent cache locality, but O(n) insertion/removal in the middle because of shifting. A linked list stores nodes with pointers: O(1) insertion/removal when you hold a reference to the node, but O(n) index access and search, plus every node is a separate allocation and a cache miss.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store