Complexity analysis

Big-O, amortized cost (why dynamic array append is O(1)), space vs time trades. Every solution you present gets a complexity label — make it a reflex.

About 28 min read3-question knowledge check

The mental model

Complexity analysis answers one question: if the input gets ten times bigger, what happens to the work? Not "how many milliseconds" — machines differ, languages differ, the JIT warms up. What does not differ is the shape of the growth.

The analogy that sticks: you are quoting a moving job. You do not quote in seconds; you quote in a rule. "One box per trip" is linear — double the boxes, double the trips. "I must carry every box past every other box" is quadratic — double the boxes, quadruple the work. "I can halve the pile each trip" is logarithmic — double the boxes, one extra trip. The customer does not care about your van's top speed; they care about which rule you are on, because the rule is what decides whether 10,000 boxes takes an afternoon or a decade.

Why does this exist as a formal tool? Because intuition about scale is terrible. An O(n²) algorithm on 1,000 items does a million operations — instant. The same algorithm on 100,000 items does ten billion — that is minutes to hours, and it is the same code that passed review. Big-O exists so that "this will not survive growth" is a claim you can make before the incident, from the code alone, without a load test.

The interview framing is blunter: every solution you present gets a complexity label, stated before you write code. Interviewers use it as a proxy for whether you understand your own solution. A candidate who says "this is O(n log n) time, O(n) space, dominated by the sort" has demonstrated more than one who produces the same code silently.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision
Complexity analysis — SWE lesson · PrepAtlas Tech