Sweaty Imposter

Big O

Complexity classes, common data structure operations, and standard algorithms in one reference.

Complexity Classes

O(1)Constant
Independent of input size.

The operation takes the same time whether the input is 10 or 10 billion. Look for direct lookups: array index, hashmap get/put (average), stack push/pop.

Array access by indexHashmap insert / lookup (average)Stack push / pop, Queue enqueue / dequeueMath operations on primitives
O(log n)Logarithmic
Doubling n adds one step.

Each step halves the remaining work. Recognized in divide-and-conquer over a structure that supports cheap halving.

Binary search on a sorted arrayBalanced BST insert / search / deleteBinary heap insert / extract-minSkip list operations
O(n)Linear
Doubling n doubles the work.

Touch every element once. The standard cost of a single pass.

Iterating an array or listBFS / DFS counting steps in a treeTwo-pointer sweep on sorted dataSliding window total work (amortized)
O(n + m)Linear in two inputs
Linear in each input independently.

When the work scales with two separate inputs. Common in graph traversal (V + E) and merging two streams.

Graph BFS/DFS: O(V + E)Merging two sorted lists of length n and mKMP / Z-algorithm pattern matching: O(text + pattern)
O(n log n)Linearithmic
Sorting baseline.

The lower bound for comparison-based sorting. Appears whenever you sort, build a heap iteratively, or do log-n work n times.

Merge sort, quicksort (average), heapsort, TimsortBuilding a heap by repeated insertionSweeping intervals after sorting them
O(n * m)Product of two inputs
Multiplicative in the two dimensions.

Touching every cell of a 2D grid or doing m work per element in an n-length list. Distinct from O(n+m), this scales with the product.

Filling an n×m DP table (edit distance, LCS, unique paths)Matrix traversalPairwise comparison of two collections
O(n^2)Quadratic
Doubling n quadruples the work.

Nested loops over the same input. Acceptable for small n (n < ~10k), dangerous beyond that.

Bubble / insertion / selection sortNaïve pair comparisons (brute-force two-sum)Floyd-Warshall shortest paths
O(n^3)Cubic
Doubling n multiplies work by 8.

Three nested loops. Useful for graph algorithms over small vertex counts.

Floyd-Warshall (also fits here, depending on how you slice n)Naïve matrix multiplicationTriple-nested brute force searches
O(2^n)Exponential
Each extra input doubles the work.

Generating all subsets, or unmemoized branching recursion. Becomes infeasible past n ≈ 25-30.

Naïve Fibonacci (without memoization)Power set / subset enumerationBrute-force subset-sum / knapsack
O(n!)Factorial
Each extra input multiplies by another factor.

All permutations. Infeasible past n ≈ 10-12. Always look for a smarter approach.

Brute-force traveling salesmanGenerating all permutationsNaïve constraint satisfaction without pruning

Data Structure Operations

StructureAccessSearchInsertRemove
ArrayO(1)O(n)O(n)O(n)
Dynamic array
Push is O(1) amortized; arbitrary insert/remove shifts elements.
O(1)O(n)O(1) amortizedO(n)
Linked list (doubly)
* given a node reference; locating the node is O(n).
O(n)O(n)O(1)*O(1)*
Hash mapn/aO(1) avg / O(n) worstO(1) avgO(1) avg
Balanced BSTO(log n)O(log n)O(log n)O(log n)
Binary heapO(1) for min/maxO(n)O(log n)O(log n)
Trie
L = key length.
O(L)O(L)O(L)O(L)
Union-Find (DSU)
With path compression + union by rank. α is the inverse Ackermann.
n/aO(α(n)) ≈ O(1)O(α(n)) ≈ O(1)n/a

Canonical Algorithms

AlgorithmTimeSpace
Binary searchO(log n)O(1)
Merge sortO(n log n)O(n)
Quicksort (avg / worst)O(n log n) / O(n²)O(log n)
HeapsortO(n log n)O(1)
Counting sortO(n + k)O(k)
BFS / DFS on a graphO(V + E)O(V)
Dijkstra (binary heap)O((V + E) log V)O(V)
Bellman-FordO(V · E)O(V)
Topological sort (Kahn)O(V + E)O(V)
Kruskal's MSTO(E log E)O(V)
Floyd-WarshallO(V³)O(V²)
KMP / Z-algorithmO(n + m)O(n + m)

Common Pitfalls

Average vs Worst Case

Hashmap operations are O(1) average but O(n) worst case (collision chain). Quicksort is O(n log n) average, O(n²) worst. Default to the average case unless the interviewer probes pathological inputs.

Amortized Analysis

Dynamic-array push is O(1) amortized: most pushes are O(1), but an occasional resize is O(n). Averaged over many pushes the cost stays constant. Same idea applies to union-find's α(n) and to most sliding-window inner loops.

n vs k vs L

Use the right symbol for the right dimension. A heap of size k gives O(log k) ops, not O(log n). Trie ops are O(L) where L is the key length, not the alphabet or the trie size. Mixing them up is a common mistake in senior interviews.

Log Base Doesn't Matter

O(log₂ n) = O(log₁₀ n) = O(ln n). Constant factors fall out of Big O, so say “log n” and leave out the base.