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
| β 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.
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
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
A loop over an array of n items, with constant work inside. What's the time complexity?
A loop inside a loop, both over the same array of n items?
What's the average time complexity of looking up a key in a hash map (JS Map/object, Python dict)?
Binary search on a sorted array isβ¦
Create a free account to track your progress.