Complexity Analysis in Practice

Difficulty
Importance

The problem

Given real code, you need a repeatable procedure for reading off its time and space cost — not just the abstract vocabulary of Big-O, but the practical habit of counting loops, calls, and allocations to arrive at a growth rate.

Why now

Asymptotic growth supplies the formal notation, but says nothing about how to look at a nested loop, a recursive call, or an allocation and turn it into that notation. This topic is the missing bridge: a repeatable method for reading real code and producing its complexity.

Mental model

Count the loops, multiply nested ones, add sequential ones, and match recursive calls to their recurrence — the same sum/product rules from combinatorics, now applied to lines of code instead of possibilities. Space complexity asks the identical question about memory instead of time: what grows, and how fast, as input grows?

Requires

Helps

Unlocks

Used in

Projects

  • Annotate five functions from a real codebase with their time and space complexity, showing the counting work
  • Find and fix an accidental O(n²) loop (e.g. using indexOf inside a loop) by introducing a hash-based lookup

Examples

  • A single loop over n items is O(n); a loop over n items with an indexOf search inside is O(n²)
  • A function creating a new array of size n at each of k recursive calls uses O(n·k) space, not just O(k)

Resources

Mastery checklist