Complexity Cheat Sheet

Big-O for data-structure operations and the common sorting algorithms — the table to glance at the morning of a coding round.

big-odata-structuressorting

Data-structure operations (average / worst)

StructureAccessSearchInsertDeleteNotes
ArrayO(1)O(n)O(n)O(n)contiguous, cache-friendly
Dynamic arrayO(1)O(n)O(1)*O(n)*amortized append
Linked listO(n)O(n)O(1)O(1)O(1) at a known node
Hash mapO(1)O(1)O(1)O(n) worst (collisions)
Balanced BSTO(log n)O(log n)O(log n)O(log n)ordered, range queries
HeapO(n)O(log n)O(log n)O(1) peek min/max
Stack / QueueO(1)O(1)LIFO / FIFO
TrieO(L)O(L)O(L)L = key length, prefix search

Sorting algorithms

AlgorithmBestAverageWorstSpaceStable
Quicksortn log nn log nlog n
Mergesortn log nn log nn log nn
Heapsortn log nn log nn log n1
Insertionn1
Counting / Radixn + kn + kn + kn + k✅ (non-comparison)

Defaults to remember: comparison sorts can't beat O(n log n). Quicksort is the usual in-memory pick (fast constants) but watch the O(n²) worst case; mergesort when you need stability or external/linked-list sorting; counting/radix only for small integer keys.

Common algorithm complexities

TaskComplexity
Binary searchO(log n)
BFS / DFS (graph)O(V + E)
Dijkstra (binary heap)O((V + E) log V)
Topological sortO(V + E)
Build a heapO(n)
Two pointers / sliding windowO(n)
Subsets / permutations (backtracking)O(2ⁿ) / O(n·n!)
Know why, not just what

Don't just memorize the table — be able to derive it. "Hash map is O(1) average because the key hashes to a bucket directly, but O(n) worst if everything collides into one bucket." The derivation is what survives a follow-up.