Algorithms Playbook
ConceptTime — · Space —8 min read

Big-O: how to tell if your code will survive a million users

Big-O describes how the work grows as the input grows. The six classes you'll see in every interview, how to read them from code, and the rules for adding and dropping terms.

In short

Big-O answers "if the input doubles, what happens to the work?" It ignores constants and keeps the fastest-growing term: O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(2ⁿ).

The idea: growth, not speed

Two programs can both take 10 ms on your laptop. With 1,000,000 items, one still takes milliseconds and the other takes hours. Big-O is the tool that tells them apart before you find out in production.

How the number of operations grows with n
O(1)1Read an array element by indexO(log n)10Binary searchO(n)1,000Loop over the array onceO(n log n)9,966Sort (merge sort, quicksort)O(n²)1,000,000Compare every pair (two nested loops)O(2ⁿ)∞Try every subset
O(n) · At 100M ops/s
10 µs
O(n log n) · At 100M ops/s
100 µs
O(n²) · At 100M ops/s
10 ms
O(2ⁿ) · At 100M ops/s
longer than the universe
Big-O ignores constants and keeps the term that grows fastest. Move n and watch which classes stay usable: at n = 1,000,000 an O(n²) algorithm needs about 3 hours; O(n log n) needs 0.2 seconds.
Slide n and compare the classes. The times assume 100 million simple operations per second.

Reading Big-O from code

Code shapeComplexityWhy
A fixed number of stepsO(1)Doesn't depend on n: arr[5], map.get(key)
Halve the problem each stepO(log n)Binary search, balanced trees
One loop over the inputO(n)Each element is touched a constant number of times
Sort, or divide and mergeO(n log n)log n levels, n work per level
A loop inside a loopO(n²)Every pair of elements
Try every subset or choiceO(2ⁿ)Each element doubles the options
  • Sequential steps add: a loop then another loop is O(n + n) = O(n).
  • Nested steps multiply: a loop inside a loop is O(n × n) = O(n²).
  • Drop constants and smaller terms: O(3n + 50) is O(n); O(n² + n) is O(n²).
  • Different inputs keep different letters: looping over a then over b is O(a + b), not O(n).

Space complexity counts too

Space complexity is the extra memory your algorithm needs, not counting the input. A hash map with every element is O(n) space; two pointers is O(1). Recursion uses space too: every pending call sits on the call stack.

From constraints to the target complexity

Interview problems often state the input size. With roughly 10⁸ simple operations per second, the constraint tells you what complexity is expected:

n up toYou probably need
10–20O(2ⁿ) or O(n!) is fine: backtracking
1,000–5,000O(n²) is fine
100,000–1,000,000O(n log n) or O(n)
10⁹ or moreO(log n) or O(1): math or binary search

Key takeaways

  • 01Big-O describes how work grows with input size, ignoring constants.
  • 02Sequential steps add, nested steps multiply; keep only the biggest term.
  • 03Always give both time and extra space complexity.
  • 04The input size in the constraints hints at the complexity you need.

Check your understanding

1 of 3

What's the complexity of a loop over the array followed by another loop over the same array?

Related topics