Big-O for Beginners
Read a little, play a little. No scary maths, and no rush.
Growth, not stopwatches
Big-O doesn't measure seconds — it measures how cost grows when input grows. An O(n²) solution might beat O(n log n) on 10 items and still be the wrong answer, because interviews (and production) care about 10 million items. Think shapes of curves, not stopwatch readings.
The four shapes that cover 95% of interviews
- O(1) — the locker combo. Same effort regardless of size: array index, hash lookup, arithmetic. Flat line.
- O(n) — reading the class list. Touch each item once. Loops, single passes, linear scans.
- O(n log n) — sorting the class into order. Divide in halves (log n levels), linear work per level. Sorting, heaps of everything.
- O(n²) — everyone shakes everyone's hand. Nested loops over the same n. Fine for hundreds, fatal for millions.
One more shape: O(log n)
Each step cuts the remaining problem in half. Searching a sorted list of a million items takes about 20 steps, because a million halved 20 times reaches 1. A billion takes about 30. Binary search and balanced tree lookups live here, and it grows so slowly that it is nearly as good as O(1) in practice.
Best, worst, and the one that matters
Quicksort's worst case is O(n²) but its average is O(n log n) — interviews care about typical behavior plus awareness of the pathological case. (Python's built-in sort is not quicksort: it is Timsort, which stays O(n log n) even in the worst case.) When asked, give time and space: "time O(n), and O(n) extra space for the map" is a complete answer; time alone is half an answer.
The working method
Start with the honest brute force ("that's O(n²)"), then improve: a hash map often buys O(n) with O(n) space; sorting buys O(n log n) with O(1) extra. Naming both the cost and what you traded for it is what "analyze complexity" actually means.
Spotting hidden costs in real code
Complexity hides inside innocent-looking lines. This function looks like one loop:
def has_duplicates(items):
seen = []
for x in items:
if x in seen: # scans the whole list
return True
seen.append(x)
return False
But x in seen on a list checks every element, so it is O(n) on its own. Inside a loop of n items, the whole function is O(n²). Changing seen to a set (seen = set() and seen.add(x)) makes each membership check O(1) on average, and the function becomes O(n). Same logic, one changed data structure, a completely different curve.
Rules for simplifying
- Drop constants. Two separate passes over the data is O(2n), which simplifies to O(n). Big-O cares about the shape, not the multiplier.
- Keep the dominant term. O(n² + n) is O(n²), because for large n the square dwarfs the rest.
- Different inputs get different letters. Looping over a list of n users and, separately, a list of m orders is O(n + m). Looping over the orders inside the user loop is O(n · m). Writing O(n²) there would be wrong.
- Halving loops are logarithmic. A loop that divides n by 2 on every pass runs about log n times, so it is O(log n), not O(n).
Remember this
- Curves, not clocks. Time and space, always.
- Brute first (say its cost), optimize second (say what you traded).
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Related chapters
Spotted a mistake or want a topic covered? Report an issue