Big O Notation
Describe growth rates with Big O, drop constants and lower-order terms, rank common complexity classes, and analyze the complexity of loops.
Welcome back!
Step 1 of 10Learn
Growth beats constants
Algorithm A takes 100n steps. Algorithm B takes n² steps. For n = 10, A does 1,000 steps and B only 100, so B looks better. At n = 100 they tie at 10,000. At n = 10,000, A does a million steps and B does 100 million. For large inputs, B loses badly, and nothing about constants can save it.
Big O notation captures this. It describes how an algorithm's cost grows as n gets large, keeping only what dominates. Formally, saying a function f(n) is O(g(n)) means that beyond some input size, f(n) is at most a constant multiple of g(n). Informally, it's an upper bound on the growth rate.
Two rules do most of the work:
- Drop constant factors. 100n and 3n are both O(n). Constants depend on hardware and implementation details; growth doesn't.
- Drop lower-order terms. 3n² + 50n + 1,000 is O(n²). For large n, the n² term dwarfs the rest.
So the pair-comparison loop from the last lesson, n(n − 1)/2 = 0.5n² − 0.5n, is simply O(n²).
This simplification isn't sloppiness. It reflects what matters for scalability: if you double the input, an O(n) algorithm does about twice the work, an O(n²) algorithm about four times, and an O(2ⁿ) algorithm squares its work. Constants still matter in practice for small inputs and tight performance budgets, but growth rate decides which algorithms survive large data.
Step 2 of 10Explore
Watch the dominant term take over
The chart shows T(n) = a·n² + b·n + c. Even with a tiny a and big b and c, the n² term eventually dominates. The readout shows what share of the total comes from the n² term at n = 1,000.
- T(1000)
- 1,051,000
- Share from the n² term at n = 1000
- 95.1 %
- n where a·n² first exceeds b·n
- 50
Step 3 of 10Learn
The common complexity classes
A handful of growth rates cover most algorithms you'll meet. From slowest-growing (best) to fastest-growing (worst):
| Big O | Name | Example | n = 1,000,000 |
|---|---|---|---|
| O(1) | constant | array access by index | 1 |
| O(log n) | logarithmic | binary search | about 20 |
| O(n) | linear | linear search, summing an array | 1,000,000 |
| O(n log n) | linearithmic | merge sort | about 20 million |
| O(n²) | quadratic | comparing all pairs | 10¹² |
| O(2ⁿ) | exponential | trying all subsets | unimaginably large |
| O(n!) | factorial | trying all orderings | worse still |
A useful rule of thumb: a typical computer does on the order of 10⁸ to 10⁹ simple operations per second. An O(n log n) algorithm on a million items finishes in a fraction of a second. An O(n²) algorithm on the same data needs about 10¹² operations, which could take many minutes or hours. An exponential algorithm becomes hopeless around n = 50 or so.
Logarithmic growth deserves special attention because it's so slow. log₂ n counts how many times you can halve n. Doubling n adds just one step. That's why binary search over the entire population of Earth takes only about 33 comparisons.
In interviews and real design discussions, people often first find an O(n²) brute-force solution, then look for an O(n log n) or O(n) improvement using sorting or hash tables.
Step 4 of 10Practice · Put in order
Order these growth rates from slowest-growing (most efficient for large n) to fastest-growing.
Drag the items, or use the arrow buttons, to put them in order.
- O(n)
- O(log n)
- O(n log n)
- O(n²)
- O(1)
- O(2ⁿ)
Step 5 of 10Try the calculator
How long would it take?
Enter an input size and an operation rate. The calculator estimates running time in seconds for different growth rates, assuming one operation per unit of work. Try n = 1,000, then 1,000,000.
- O(log n): log₂ n steps
- 20
- O(n) time
- 0.01 s
- O(n log n) time
- 0.1993 s
- O(n²) time
- 10,000 s
- O(n²) time in hours
- 2.78 h
Step 6 of 10Learn
Reading complexity from code
Most of the time you can read the complexity straight from the code's structure.
Sequential steps add. A loop over n items followed by another loop over n items is O(n) + O(n) = O(2n) = O(n).
Nested loops multiply. A loop over n items containing a loop over n items is O(n²). If the inner loop runs over a different input of size m, it's O(n·m), not O(n²).
Halving gives a logarithm. A loop that divides its range by 2 each time, like while (n > 1) n = Math.floor(n / 2);, runs about log₂ n times: O(log n). A loop over n items that halves something inside each iteration is O(n log n).
Different inputs get different letters. Looping over array a and then array b is O(a + b). Don't collapse them to O(n) unless they're the same size.
Watch for hidden loops. Built-in methods aren't free. In JavaScript, arr.includes(x), arr.indexOf(x), arr.slice(), arr.shift() and spreading [...arr] all take O(n) time. A loop that calls includes on every iteration is secretly O(n²). String concatenation in a loop can also be expensive in some languages, because strings are immutable and each concatenation may copy.
Space complexity follows the same logic for extra memory: creating a new array of size n is O(n) space; a few counters are O(1).
Step 7 of 10Practice · Match the pairs
Match each code pattern to its time complexity.
Tap an answer, then tap the place where it belongs (or drag it there).
- One loop over n items, then a second separate loop over n items
- A loop over n items containing a loop over the same n items
- A loop that halves n until it reaches 1
- A loop over n items that calls arr.includes() on an n-item array each time
- A loop over array a, then a loop over a different array b
Step 8 of 10Learn
Big O, Big Omega and Big Theta
Strictly speaking, Big O is an upper bound. Saying linear search is O(n²) is technically true, since it never does more than n² work, but it's not useful. In practice, people use Big O to mean the tightest bound they know.
Computer science has two related notations:
- Big Omega, Ω(g(n)): a lower bound. The algorithm does at least this much work, beyond some size.
- Big Theta, Θ(g(n)): a tight bound, both O and Ω. The growth rate is exactly this, up to constants.
Summing an array is Θ(n): it always touches every element. Linear search is O(n) in the worst case and Ω(1) overall, because it can finish after one comparison.
A few common mistakes to avoid:
- Confusing Big O with worst case. They're different ideas. You can give a Big O bound for the best, average or worst case. Saying insertion sort's best case is O(n) is perfectly meaningful.
- Assuming O(1) means fast. It means the time doesn't grow with n. A constant-time operation that takes a full second is still O(1).
- Ignoring constants entirely in practice. For small inputs, an O(n²) algorithm with tiny constants can beat an O(n log n) one. Many real sorting libraries switch to insertion sort for small subarrays for this reason.
- Forgetting space. An algorithm that's fast but uses O(n²) memory may not fit on the machine.
Step 9 of 10Practice · Spot the mistake
A study group wrote these six claims about Big O. Tap the two that are wrong.
Step 10 of 10Practice · Work it out
An O(n²) algorithm takes 2 seconds to process 10,000 items. Roughly how many seconds will it take for 30,000 items?
Lesson complete!