Big O and Algorithm Efficiency
Use Big O notation to describe how running time grows with input size, recognize common complexity classes, and choose algorithms that scale.
Welcome back!
Step 1 of 14Learn
Fast today, unusable tomorrow
A startup builds a feature that finds duplicate accounts by comparing every user with every other user. With 1,000 users it takes a blink. A year later they have 1,000,000 users and the job runs for days.
What happened? The number of comparisons is roughly n × n. With 1,000 users, that's 1 million comparisons. With 1,000,000 users, it's 1 trillion: a million times more work for a thousand times more users.
Computer scientists describe this behavior with Big O notation. Big O answers the question: as the input size n grows, how does the work grow? It ignores details that depend on the particular computer or language, such as constant factors, and focuses on the growth rate.
The rules of thumb:
- Keep the fastest-growing term. An algorithm doing n² + 100n + 5000 steps is O(n²), because for large n the n² term dominates.
- Drop constant multipliers. 3n steps is O(n). Doubling the speed of a slow algorithm doesn't change its class.
- Usually think about the worst case. Linear search is O(n) even though sometimes the target is first.
Big O lets you predict, before you write code, whether an approach will still work when the data is 1,000 times larger.
Step 2 of 14Practice · Choose one
An algorithm takes 5n² + 300n + 20 steps for an input of size n. What is its Big O?
Step 3 of 14Learn
The common complexity classes
Most algorithms you'll meet fall into a handful of classes, listed from the slowest-growing amount of work to the fastest-growing:
| Big O | Name | Example | n = 1,000 → about |
|---|---|---|---|
| O(1) | Constant | Reading an array item by its index | 1 step |
| O(log n) | Logarithmic | Binary search | 10 steps |
| O(n) | Linear | Linear search, summing a list | 1,000 steps |
| O(n log n) | Linearithmic | Merge sort | 10,000 steps |
| O(n²) | Quadratic | Comparing every pair; selection sort | 1,000,000 steps |
| O(2ⁿ) | Exponential | Trying every subset of items | more than the atoms in the universe |
A few patterns help you recognize them in code:
- A single loop over the input is usually O(n).
- A loop inside a loop, both over the input, is usually O(n²).
- Halving the problem at each step gives O(log n).
- Doing O(log n) levels of O(n) work each gives O(n log n).
Exponential algorithms are only practical for tiny inputs. Some important problems, like finding the best route that visits many cities, have no known fast algorithm, which is one of the deepest open questions in computer science (the P versus NP problem).
Step 4 of 14Practice · Chart
How work grows with input size
At which input size does n² first need more than 1,000 steps? Tap that point.
Tap the point that answers the question, then press Check.
| Input size n | log₂ n | n | n log₂ n | n² |
|---|---|---|---|---|
| 2 | 1 | 2 | 2 | 4 |
| 4 | 2 | 4 | 8 | 16 |
| 8 | 3 | 8 | 24 | 64 |
| 16 | 4 | 16 | 64 | 256 |
| 32 | 5 | 32 | 160 | 1,024 |
| 64 | 6 | 64 | 384 | 4,096 |
Step 5 of 14Explore
Growth rate explorer
Compare how the work grows. The curve is c × n² (quadratic); the readouts compare other classes at the same n. Notice how fast n² overtakes n log n.
- O(log n) steps
- 10
- O(n) steps
- 1,000
- O(n log n) steps
- 9,966
- c × n² steps
- 1,000,000
Step 6 of 14Practice · Match the pairs
Match each operation with its typical Big O.
Tap an answer, then tap the place where it belongs (or drag it there).
- Looking up array item number 500 directly
- Binary search in a sorted list
- Finding the largest value in an unsorted list
- Merge sort
- Checking every pair of students for a shared birthday
Step 7 of 14Learn
Reading Big O from code
Let's estimate Big O for three small JavaScript functions.
function first(list) { return list[0]; }One step, whatever the list size: O(1).
function total(list) {
let sum = 0;
for (const x of list) sum += x;
return sum;
}One loop over n items: O(n).
function hasDuplicate(list) {
for (let i = 0; i < list.length; i++)
for (let j = i + 1; j < list.length; j++)
if (list[i] === list[j]) return true;
return false;
}For each item, compare it with every later item: about n²/2 comparisons, so O(n²).
Can we do better for duplicates? Yes: sort the list first (O(n log n)) and then check neighbors in one pass (O(n)). The total is O(n log n). Using a hash set, a data structure with near-instant lookups, gets it down to about O(n) on average.
This is how efficiency improves in practice: not by writing the same algorithm in a faster language, but by choosing a better algorithm or data structure. A smarter algorithm on a slow laptop can beat a poor one on a supercomputer once the input is big enough.
Step 8 of 14Practice · Work it out
An O(n²) algorithm takes 2 seconds for 10,000 items. Roughly how many seconds will it take for 30,000 items?
Step 9 of 14Chart
An O(n²) job that takes 2 s for 10,000 items
Double the input, four times the time; triple it, nine times. That is quadratic growth.
Step 10 of 14Note
Step 11 of 14Note
Step 12 of 14Practice · Estimate
Use the explorer: at n = 1,000, roughly how many times more steps does n² take than n log₂ n?
Step 13 of 14Practice · True or false
If a program uses an O(n²) algorithm, running it on a computer twice as fast changes its Big O to O(n).
Step 14 of 14Practice · What would you do?
What would you do?
Two teams solve the same problem. Team A's algorithm is O(n log n) but has a large constant; it takes 1 second for 1,000 items. Team B's is O(n²); it takes 0.1 seconds for 1,000 items. The product will soon handle 1,000,000 items. Which should they pick?
Lesson complete!