Beginners often treat O(1), O(N), and O(N²) as formulas to memorize. Big O answers a more useful question: as the input size grows, how does the amount of work grow? It describes a growth pattern, not an exact stopwatch time.
Three Growth Patterns
| Input size N | O(1) | O(N) | O(N²) |
|---|---|---|---|
| 10 | 1 | 10 | 100 |
| 100 | 1 | 100 | 10,000 |
| 1,000 | 1 | 1,000 | 1,000,000 |
Constant work stays roughly unchanged. Linear work grows in proportion to N. Quadratic work grows as N × N, so increasing N by 100 can increase the work by 10,000.
O(1): Fixed Work
distance = abs(a - c) + abs(b - d)
This expression performs a fixed number of arithmetic operations regardless of the coordinate values. The CCC 2017 J3 Exactly Electrical decision is O(1). Constant time does not mean one line; it means the operation count does not grow with N.
O(N): One Pass
total = 0
for value in values:
total += value
A list of N items causes N loop iterations. Two consecutive scans cost N + N = 2N, which is still O(N) because Big O ignores fixed multipliers and keeps the growth shape.
O(N²): Work for Every Pair
for i in range(n):
for j in range(n):
compare(values[i], values[j])
The outer loop runs N times and starts an N-step inner loop each time, for about N² operations. Comparing only distinct pairs gives N(N-1)/2; its leading growth is still quadratic.
Do not classify code by indentation alone. Consecutive loops add, nested ranges often multiply, and library calls such as sorting contain their own algorithms.
Constraints Turn Big O into a Decision
An O(N³) solution may be completely reasonable at N≤100, as in USACO Daisy Chains. At N=100,000, an O(N²) solution implies roughly ten billion pair checks and is usually unacceptable. Language, constants, operation cost, and time limits still matter, so the usual limits table is a screening heuristic rather than a guarantee.
Use this workflow:
- Define what
Nmeasures. - Identify which work grows with
N. - Add consecutive work and multiply genuinely nested work.
- Keep the dominant growth term.
- Estimate the count at the maximum allowed
N.
Common Misunderstandings
- Big O is an exact running time. It is not; it describes growth.
- Any two loops imply
O(N²). Consecutive loops remain linear. - Multiplying a number by
NcostsO(N). One arithmetic multiplication is fixed work in this model. - A lower Big O automatically makes code better. Correctness, clarity, and actual constraints still matter.
O(N²)is always too slow. It can be ideal whenNis small.
Practice
- What is the Big O of
3N+5operations? - An outer loop runs
Ntimes and its inner body always runs 10 times. What is the complexity? - Compute
N³atN=100. Why can some problems accept it? - What is the complexity of sorting and then scanning once?
- Take an old solution and define its
Nand worst-case order of growth.
One Sentence to Remember
Big O describes how work grows with input size: count the work, read the constraints, and then decide whether the algorithm is fast enough.
Questions or feedback?
Have a question about this article or want to suggest an improvement? Send us an email.
