STUDY GUIDE · ABOUT 12 MIN READ

Data structures

How you store data decides what's fast and what's slow. Pick an everyday job to see which structure a programmer would reach for.

StackEvery edit is pushed on top; Undo pops the most recent one first. Last in, first out.

What a data structure is, in short

A data structure is a way of organising data in memory so that certain operations, such as looking something up, adding or removing it, are efficient. Choosing one is a trade-off: a structure that makes lookups instant may make insertions slow, or use more memory.

Niklaus Wirth summed up their importance in the title of his 1976 book: Algorithms + Data Structures = Programs.

LinearItems in a sequence: arrays, linked lists, stacks, queues.
HierarchicalParents and children: trees and heaps.
NetworkedAnything can link to anything: graphs.
KeyedFind by name, not position: hash tables and maps.
LINEAR STRUCTURES

Same items in, different item out

Add items to both, then remove one. A stack gives back the newest (last in, first out); a queue gives back the oldest (first in, first out).

STACK · LIFOpush / pop
ABCTOP · next out
Undo history, the browser Back button, and the call stack that tracks which function called which.
QUEUE · FIFOenqueue / dequeue
OUT ←
Anext outBC
← IN
REMOVED SO FAR
Stack:–Queue:–
Print jobs, messages waiting for a server, and breadth-first search through a graph.
HASH TABLES

Jump straight to the right drawer

A hash function turns a key into a bucket number, so you can find it without searching. Ours adds up the letter codes and takes the remainder after dividing by 8. Add names and watch for collisions.

N(78) + o(111) + a(97) = 286 → 286 mod 8 = bucket 6
bucket 0
Ana
bucket 1
bucket 2
bucket 3
bucket 4
bucket 5
Ben
bucket 6
Noa
bucket 7
No collisions yet. Keep adding names: with only 8 buckets, two will soon share one.
TREES

Left is smaller, right is bigger

In a binary search tree, every node's left branch holds smaller values and its right branch bigger ones. Each comparison throws away half of what's left. Pick a value to find.

503070204060801025354555657590
SEARCH PATH
1. 45 < 50 → go left2. 45 > 30 → go right3. 45 > 40 → go right4. 45 = 45 ✓ found
4tree steps
7list steps (in order)
TRADE-OFFS

The cheat sheet

fastokayslow
STRUCTUREACCESS BY POSITIONSEARCHINSERTDELETE
Array
O(1)O(n)O(n)O(n)
Linked list
O(n)O(n)O(1)O(1)
Stack
n/aO(n)O(1) top onlyO(1) top only
Queue
n/aO(n)O(1) top onlyO(1) top only
Hash table
n/aO(1)O(1)O(1)
Balanced BST
O(log n)O(log n)O(log n)O(log n)
Heap
n/aO(n)O(log n)O(log n)
Typical or average cases. Hash tables can degrade to O(n) with many collisions; the tree row assumes it stays balanced. “Insert” in an array means in the middle; adding at the end is usually O(1).

Milestones

1945Stored programs.Von Neumann's design put data and instructions in the same memory, making structures possible.
1953Hashing.Hans Peter Luhn at IBM described hashing for fast lookup.
1955Linked lists.Newell, Shaw and Simon used them in the Logic Theorist, an early AI program.
1962Balanced trees.Adelson-Velsky and Landis invented the AVL tree, which rebalances itself to stay fast.
1964The heap.J. W. J. Williams introduced the binary heap, along with heapsort.

Keep going

Algorithms + Data Structures = ProgramsNiklaus Wirth · 1976
Introduction to AlgorithmsCormen, Leiserson, Rivest & Stein · 1990
Grokking AlgorithmsAditya Bhargava · 2016

Organised data, organised mind. Test your reasoning.

Our full IQ test covers logical, numerical, verbal and spatial reasoning: 60 questions, about 30 minutes.

Take the full IQ test