Skip to content

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.

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 — n for one collection, n and m for 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).

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.

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
  1. Sequential blocks add: O(a) then O(b) is O(a + b).
  2. Nested blocks multiply: a loop of n containing a loop of m is O(n·m).
  3. Drop constants and lower terms: O(2n + n²) is O(n²).
  4. Loops that halve are logarithmic.
# O(n): one pass, constant work per element
def max_of(list)
list.reduce { |a, b| a > b ? a : b }
end
# O(n log n): the sort dominates the linear scan
def has_duplicates?(list)
list.sort.each_cons(2).any? { |a, b| a == b }
end
# O(n²): every element compared against every other
def all_pairs(list)
list.flat_map { |a| list.map { |b| [a, b] } }
end

Note 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.

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.

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)

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.

  1. Define your variables. “n is the number of nodes, m is the number of edges.”
  2. Give time first, then space. They are separate questions.
  3. Say what dominates and why. “The sort is O(n log n), the scan is O(n), so O(n log n).”
  4. Name the bottleneck if the interviewer pushes for an optimisation.
  5. Mention the worst case when best and worst differ: quick sort, hash tables, unbalanced trees.
  1. What is the complexity of your hash-map solution if the hash function is terrible?
  2. Can you trade space for time here, and what would that cost in memory?
  3. What is the space complexity of your recursion, and how deep can it go?
  4. What changes if the input is already sorted, or already reversed?
  5. Which operation dominates if n doubles?
  • 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.