Lesson 1 of 13 Β· 12 min

Big-O in Plain English

How to describe the speed of your code, and why interviewers always ask.

Why Big-O?

Big-O describes how the work grows as the input grows. It doesn't measure seconds. It tells you whether your code will still be fast with 10 items, 10,000 or 10 million.

In interviews, after you solve a problem, you'll almost always be asked: "What's the time and space complexity?"

See it: drag the slider

πŸ“ˆ How work grows as input grows
β–  O(1)1 steps
β–  O(log n)4 steps
β–  O(n)16 steps
β–  O(n log n)64 steps
β–  O(nΒ²)256 steps

At n = 1,000,000: O(log n) β‰ˆ 20 steps, O(n) = 1 million, O(nΒ²) = 1 trillion. That's why the right algorithm matters.

Drag n to the right. The gap between curves grows fast. That gap is the difference between an app that works and one that times out.

The common ones

Big-O Name Example
O(1) constant read arr[0], look up a key in a hash map
O(log n) logarithmic binary search
O(n) linear one loop over the input
O(n log n) linearithmic sorting
O(nΒ²) quadratic a loop inside a loop

Reading code for Big-O

JavaScript Β· try it
Loading…

Rules of thumb

  • Drop constants: O(2n) is just O(n).
  • Keep the biggest term: O(nΒ² + n) is O(nΒ²).
  • Space complexity counts extra memory you create (new arrays, maps), not the input.

How to talk about it

"My first solution is O(nΒ²) because of the nested loop. I can get it to O(n) time by trading memory: store each number I've seen in a Set, at the cost of O(n) space."

Naming the trade-off is what earns points. The next lessons show the patterns that make these optimizations.

βœ… Check your understanding

  1. A loop over an array of n items, with constant work inside. What's the time complexity?

  2. A loop inside a loop, both over the same array of n items?

  3. What's the average time complexity of looking up a key in a hash map (JS Map/object, Python dict)?

  4. Binary search on a sorted array is…

Create a free account to track your progress.