Complexity Analysis (Big-O)
Build real intuition for how O(1), O(log n), O(n), O(n log n), O(n²), and O(2ⁿ) actually diverge as input size grows — and practice recognizing which class a piece of code belongs to, the single most common warm-up question in any technical interview.
Operation count vs input size n
O(2ⁿ) is clipped above 1,000 on this chart — see the table below for true values. Dashed line marks the current n.
- O(1)
- O(log n)
- O(n)
- O(n log n)
- O(n²)
- O(2ⁿ)
| Complexity class | Operations at n = 10 |
|---|---|
| O(1) | 1 |
| O(log n) | 3.32 |
| O(n) | 10 |
| O(n log n) | 33.22 |
| O(n²) | 100 |
| O(2ⁿ) | 1,024 |
Name the growth rate
Sort each piece of code by its time complexity.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
O(1)
Drop items hereO(log n)
Drop items hereO(n)
Drop items hereO(n²)
Drop items hereTest your knowledge
A function with a single loop that runs n times, and inside it a nested loop that also runs n times — what is its time complexity?
The short version
Imagine four different ways to find a friend's phone number: flip through every page of a phone book (linear), or use the fact that it's alphabetized to keep splitting the search range in half (logarithmic), or — if you're truly unlucky — check every possible combination of names against every possible combination of digits (exponential). The number of pages barely matters for the first two even as the book gets huge. It matters enormously for the last one.
In more depth
Big-O and Algorithmic Complexity
Big-O notation describes how an algorithm's cost grows as its input grows — not how long it takes. It deliberately discards constant factors and lower-order terms, because those depend on the machine, the language and the compiler, while the growth rate does not.
Reading the notation
O(f(n)) is an upper bound: beyond some input size, the cost is at most a constant multiple of f(n). So O(2n + 50) is written O(n), and O(n² + n) is written O(n²) — at large n the squared term dominates everything else.
The ladder that matters in practice
The gap between neighbours on that ladder is what decides whether a solution survives a ten-fold increase in data.
Three cases, and why average matters most
Best, average and worst case can differ sharply: quicksort is O(n log n) on average and O(n²) in the worst case, which is why its pivot choice is a real engineering decision. Space complexity is measured the same way, counting only memory the algorithm allocates beyond its input — which is what separates an in-place sort from one that needs a second array.
Interview Questions
References & Further Reading
Love this visualization?
Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.
100% free • No credit card required