Concepts.

Cheat sheet

The numbers worth memorizing, on one page. Every row links to the full lesson.

Growth rates

Rule of thumb: about 10⁸ simple operations per second.

ComplexityNameTypical exampleFeasible n (1 s)
O(1)ConstantArray index, hash lookupAny n
O(log n)LogarithmicBinary search, balanced BSTAny n
O(n)LinearSingle scann ≤ 10⁸
O(n log n)LinearithmicMerge sort, heap operations × nn ≤ 10⁶–10⁷
O(n²)QuadraticNested loops, simple DPn ≤ 10⁴
O(n³)CubicFloyd-Warshall, matrix multiplyn ≤ 500
O(2ⁿ)ExponentialAll subsets, bitmask DPn ≤ 20–25
O(n!)FactorialAll permutationsn ≤ 10–11

Estimation rules of thumb

For back-of-the-envelope capacity planning.

QuantityRule
Seconds per day86,400 ≈ 10⁵
Requests/day → QPSdivide by ~10⁵
1M requests/day≈ 12 QPS
Peak QPS≈ 2–3 × average
Storage per yeardaily writes × object size × 365
Cache size (80/20)≈ 20% of daily read volume

Sorting algorithms

AlgorithmBestAverageWorstSpaceStable
Bubble SortO(n)O(n²)O(n²)O(1)Yes
Selection SortO(n²)O(n²)O(n²)O(1)No
Insertion SortO(n)O(n²)O(n²)O(1)Yes
Merge SortO(n log n)O(n log n)O(n log n)O(n)Yes
Quick SortO(n log n)O(n log n)O(n²)O(log n)No
Heap SortO(n log n)O(n log n)O(n log n)O(1)No
Counting & Bucket SortO(n + k)O(n + k)O(n + k)O(n + k)Yes
Radix SortO(d·(n + b))O(d·(n + b))O(d·(n + b))O(n + b)Yes

Data structure operations

StructureAccessSearchInsertDeleteSpace
Arrays & Dynamic ArraysO(1)O(n)O(n)O(n)O(n)
Dynamic array (append)O(1)O(n)O(1) amortizedO(n)O(n)
Linked ListsO(n)O(n)O(1) at nodeO(1) at nodeO(n)
StacksO(n)O(n)O(1)O(1)O(n)
Queues & DequesO(n)O(n)O(1)O(1)O(n)
Hash Tablesn/aO(1) avg · O(n) worstO(1) avgO(1) avgO(n)
Binary Search TreesO(log n) avgO(log n) avg · O(n) worstO(log n) avgO(log n) avgO(n)
AVL / Red-black treeO(log n)O(log n)O(log n)O(log n)O(n)
Heaps & Priority QueuesO(1) peekO(n)O(log n)O(log n) rootO(n)
Tries (Prefix Trees)n/aO(L)O(L)O(L)O(Σ·N·L)
Skip ListsO(log n) avgO(log n) avgO(log n) avgO(log n) avgO(n)
Union-Find (Disjoint Set)n/aO(α(n)) findO(α(n)) unionn/aO(n)
Segment Treesn/aO(log n) queryO(log n) updaten/aO(n)
Fenwick Trees (BIT)n/aO(log n) prefixO(log n) updaten/aO(n)

Graph algorithms

AlgorithmTimeSpaceUse it for
Breadth-First SearchO(V + E)O(V)Shortest paths, unweighted
Depth-First SearchO(V + E)O(V)Cycles, components, ordering
Topological SortO(V + E)O(V)Dependency ordering in a DAG
Dijkstra's AlgorithmO((V + E) log V)O(V)Shortest paths, weights ≥ 0
Bellman-Ford & Floyd-WarshallO(V·E) · O(V³)O(V) · O(V²)Negative weights · all pairs
Minimum Spanning TreesO(E log E)O(V)Cheapest connected subgraph
Union-Find (Disjoint Set)O(α(n)) per opO(V)Dynamic connectivity
A* PathfindingO(E log V)O(V)Heuristic shortest path
SCCs, Bridges & Articulation PointsO(V + E)O(V)SCCs, bridges, articulation points

Latency numbers every engineer should know

L1 cache reference0.5 ns
Branch mispredict5 ns
L2 cache reference7 ns
Mutex lock/unlock25 ns
Main memory reference100 ns
Compress 1 KB (Snappy)3 µs
Send 1 KB over 1 Gbps10 µs
SSD random read150 µs
Read 1 MB sequentially from RAM250 µs
Round trip in a datacenter500 µs
Read 1 MB sequentially from SSD1 ms
Disk seek10 ms
Read 1 MB sequentially from disk20 ms
Packet CA → Netherlands → CA150 ms

Bars use a log scale: each step is roughly 10× slower than the last.

Powers of two

PowerApprox. valueBytes
2¹⁰1 thousand1 KB
2²⁰1 million1 MB
2³⁰1 billion1 GB
2⁴⁰1 trillion1 TB
2⁵⁰1 quadrillion1 PB

Availability nines

AvailabilityNameDown / yearDown / monthDown / day
99%Two nines3.65 days7.31 hours14.4 min
99.9%Three nines8.77 hours43.8 min1.44 min
99.95%Three and a half4.38 hours21.9 min43.2 s
99.99%Four nines52.6 min4.38 min8.64 s
99.999%Five nines5.26 min26.3 s864 ms