Big O Notation
Big O describes how the cost of an algorithm grows as the input grows. It is not a stopwatch: it does not tell you which of two algorithms is faster on twelve elements. It tells you what happens at a million, which is the question an interviewer is really asking.
The 60-second answer
Section titled “The 60-second answer”Big O is an upper bound on growth rate, ignoring constants and lower-order terms. O(n log n) sorting beats O(n²) sorting once the input is large enough. I state complexity in terms of the input size —
nfor one collection,nandmfor two — and I give time and space separately. A loop over n doing constant work is O(n); nested loops are O(n·m); halving the search space each step is O(log n); a sort followed by a scan is O(n log n).
O, Θ and Ω
Section titled “O, Θ and Ω”| Notation | Meaning | Plain English |
|---|---|---|
| O(f(n)) | Upper bound | “Grows no faster than f” |
| Ω(f(n)) | Lower bound | “Grows at least as fast as f” |
| Θ(f(n)) | Tight bound | “Grows exactly like f, up to constants” |
Interviews use O even when Θ is meant. Merge sort is O(n log n); it is also technically O(n²), but that bound is useless. When asked “what’s the complexity”, give the tight one.
Growth orders
Section titled “Growth orders”| Complexity | Name | Typical source | n = 1,000 |
|---|---|---|---|
| O(1) | Constant | Hash lookup, array index | 1 |
| O(log n) | Logarithmic | Binary search, balanced tree | ~10 |
| O(n) | Linear | Single pass, counting | 1,000 |
| O(n log n) | Linearithmic | Comparison sort | ~10,000 |
| O(n²) | Quadratic | Nested loops | 1,000,000 |
| O(n³) | Cubic | Triple nested loops | 10⁹ |
| O(2ⁿ) | Exponential | Subsets, naive Fibonacci | astronomically large |
| O(n!) | Factorial | Permutations, brute-force TSP | worse |
Counting operations
Section titled “Counting operations”- Sequential blocks add: O(a) then O(b) is O(a + b).
- Nested blocks multiply: a loop of n containing a loop of m is O(n·m).
- Drop constants and lower terms: O(2n + n²) is O(n²).
- Loops that halve are logarithmic.
# O(n): one pass, constant work per elementdef max_of(list) list.reduce { |a, b| a > b ? a : b }end
# O(n log n): the sort dominates the linear scandef has_duplicates?(list) list.sort.each_cons(2).any? { |a, b| a == b }end
# O(n²): every element compared against every otherdef all_pairs(list) list.flat_map { |a| list.map { |b| [a, b] } }endNote that array.include? inside a loop is O(n²), and repeated string concatenation (s = s + x)
is O(n²) because each operation allocates. Knowing where the hidden loops are is most of the skill.
Amortised analysis
Section titled “Amortised analysis”Some operations are occasionally expensive but cheap on average over a sequence. Dynamic arrays: appending is O(1), but when full the array doubles and copies everything — O(n). Because capacity doubles, a copy happens after 1, 2, 4, 8, … appends, so total copying for n appends is < 2n, and the amortised cost per append is O(1). Hash table insertion has the same property.
Amortised ≠ average. Average depends on input distribution; amortised is a worst-case guarantee over a sequence of operations.
Space complexity
Section titled “Space complexity”Count extra memory beyond the input. Recursion costs stack frames — a “linear” recursive solution can exhaust the stack at n = 100,000.
| Algorithm | Time | Extra space |
|---|---|---|
| Merge sort | O(n log n) | O(n) |
| Quick sort (in place) | O(n log n) avg, O(n²) worst | O(log n) stack |
| Heap sort | O(n log n) | O(1) |
| BFS | O(V + E) | O(V) queue |
| DFS (recursive) | O(V + E) | O(V) stack |
| Binary search (iterative) | O(log n) | O(1) |
Recursion and the master theorem
Section titled “Recursion and the master theorem”For T(n) = a·T(n/b) + O(n^d): if d > log_b(a) the cost is O(n^d); if equal, O(n^d log n); if
less, O(n^(log_b a)). Merge sort has a = 2, b = 2, d = 1 → O(n log n). Binary search has
a = 1, b = 2, d = 0 → O(log n). You rarely need to quote it; you do need to write the recurrence.
How to answer complexity questions
Section titled “How to answer complexity questions”- Define your variables. “n is the number of nodes, m is the number of edges.”
- Give time first, then space. They are separate questions.
- Say what dominates and why. “The sort is O(n log n), the scan is O(n), so O(n log n).”
- Name the bottleneck if the interviewer pushes for an optimisation.
- Mention the worst case when best and worst differ: quick sort, hash tables, unbalanced trees.
Follow-up questions
Section titled “Follow-up questions”- What is the complexity of your hash-map solution if the hash function is terrible?
- Can you trade space for time here, and what would that cost in memory?
- What is the space complexity of your recursion, and how deep can it go?
- What changes if the input is already sorted, or already reversed?
- Which operation dominates if n doubles?
Common traps
Section titled “Common traps”- Confusing O with speed. O(n) with a huge constant can lose to O(n²) at n = 20.
- Forgetting hidden loops.
include?inside a loop, string concatenation in a loop,shift. - Saying “O(n) for the sort”. Comparison sorts are O(n log n).
- Ignoring space. A memo table of n×m can be the thing that fails.
- Forgetting recursion depth. Linear time does not mean constant stack.