You need just enough complexity theory to talk about your solutions and choose an approach. No proofs, no limits — just intuition. This lesson gives you exactly that.
What Big-O means
Big-O describes how the amount of work grows as the input size n grows, ignoring constant factors and lower-order terms. O(n²) does not mean “n² operations exactly”; it means “if you double n, the work roughly quadruples.” We drop constants because for large n they don’t matter — O(2n) and O(n) are both just O(n). We care about the shape of the growth, because that’s what decides whether your solution is fast enough.
The classes you actually need
From fastest to slowest, with a real example of each:
- O(1) — constant. Same work regardless of n. A hash-map lookup, array index, arithmetic.
- O(log n) — logarithmic. Halving each step. Binary search. Growing n barely adds work (a billion items is only ~30 steps).
- O(n) — linear. One pass over the input. A single loop, a sliding window, a BFS/DFS of a list.
- O(n log n) — linearithmic. The speed of good comparison sorts (and anything that sorts then scans). Usually the target for “efficient” solutions.
- O(n²) — quadratic. Nested loops over the input. Fine for small n, too slow for large.
- O(2ⁿ) / O(n!) — exponential / factorial. Trying all subsets / permutations (backtracking). Only works for tiny n.
Estimating your code — simple rules
- A single loop over n items → O(n).
- Two nested loops over n → O(n²); three nested → O(n³).
- Each step halves the space → O(log n).
- Sort the data → O(n log n).
- Recursion that branches into 2 calls, depth n → O(2ⁿ); permutations → O(n!).
- Space counts too: an extra hash map or array is O(n) space; recursion uses O(depth) stack space. Always state both time and space.
Don’t forget to simplify: O(n + n²) is just O(n²); O(2n + 5) is O(n).
Read the constraints — they tell you the answer
This is the practical superpower. The problem’s input size tells you the intended complexity, and therefore often the pattern:
n ≤ 20 → O(2ⁿ) or O(n!) is fine → think backtracking / bitmask
n ≤ 1,000–5,000 → O(n²) is fine → nested loops, 2-D DP
n ≤ 100,000 → need O(n log n) or O(n) → sorting, sliding window, heap
n ≤ 10,000,000 → need O(n) or O(n log n)
n up to 10⁹ → need O(log n) or O(1) → binary search, mathsSo when you see n = 10⁵, you know a brute-force O(n²) will time out, and you should look for an O(n log n) or O(n) pattern. When you see n ≤ 20, exponential search is expected. Always read the constraints first — they are the interviewer quietly telling you what they want.
Pitfalls
- Forgetting space complexity. Interviewers often ask “can you do it in O(1) space?” Know your extra memory, not just time.
- Ignoring the constraints. Writing O(n²) when n = 10⁵ wastes everyone’s time. Let the input size guide the approach.
- Over-counting constants. O(3n) is O(n); don’t say “O(3n)“. But do mention a hidden factor like string length if it’s significant.
Recap
Big-O is the growth rate of work as n grows, constants ignored. Know the ladder — O(1), O(log n), O(n), O(n log n), O(n²), O(2ⁿ) — and how to read it off your code (loop = O(n), nested = O(n²), halving = O(log n)). State time and space for every solution. And read the constraints: the input size tells you the required complexity and hints at the pattern. That’s all the complexity theory an interview needs.
Next: Lesson 3 — Two Pointers, the first and most fundamental pattern.