The Plain Picture#
Delivery charges: Company A charges ₹100 + ₹2 per km. Company B charges ₹10 × km². For a 3 km trip, B is cheaper (₹90 vs ₹106). For a 100 km trip, A charges ₹300 and B charges ₹1,00,000. For big distances, only the shape of the formula matters — the ₹100 base fee and the "×2" become irrelevant compared with "km" versus "km²".
The Real Names#
Big-O notation describes how the number of steps grows as n grows, keeping only the part that dominates for large n:
Drop constant factors: 2n → n; n/2 → n; 100n → n.
Drop lower-order terms: n² + 5n + 20 → n²; n + log n → n.
So 1 + 2n steps is O(n), read "order n" or "big O of n".
The formal definition (worth knowing once): f(n) is O(g(n)) if there are constants c > 0 and n₀ such that f(n) ≤ c · g(n) for every n ≥ n₀. In plain words: beyond some input size, f never grows faster than a constant multiple of g. Big-O is an upper bound on growth.
Under the Hood — the complexity ladder#
Memorise this table. The right-hand columns are rough step counts, and the last column uses ~10⁸ simple steps per second (measured in Ch 10).
| Big-O | Name | Typical example | n = 1,000 | n = 1,000,000 | 10⁶ items at 10⁸ steps/s |
|---|---|---|---|---|---|
| O(1) | constant | arr[i], HashMap.get (average) | 1 | 1 | instant |
| O(log n) | logarithmic | binary search | 10 | 20 | instant |
| O(n) | linear | one loop over the array | 1,000 | 10⁶ | 10 ms |
| O(n log n) | linearithmic | good sorting (Arrays.sort) | 10,000 | 2 × 10⁷ | 0.2 s |
| O(n²) | quadratic | all pairs (nested loops) | 10⁶ | 10¹² | ~3 hours |
| O(n³) | cubic | all triples | 10⁹ | 10¹⁸ | ~300 years |
| O(2ⁿ) | exponential | all subsets | 10³⁰¹ | — | never |
| O(n!) | factorial | all orderings (permutations) | — | — | never |
steps
│ n! 2ⁿ
│ │ ╱
│ │ ╱ n²
│ │ ╱ ╱
│ │╱ ╱
│ ╱│ ╱
│ ╱ │╱ n log n
│ ╱ ╱│ ___───
│ ╱ ╱ │ ___─── n
│ ╱ ╱ ___───────────────────────
│ ╱ ___─── log n
│ ___───────────────────────────────────────────
│ ════════════════════════════════════════════════════ 1
└──────────────────────────────────────────────────────────▶ nPart 0's measurements match the ladder: the pairs solution is O(n²) (doubling n → 4× time), sorting is O(n log n), and the HashSet one is O(n) (doubling n → 2× time).
Gotchas
Big-O is about large n. For n = 10, an O(n²) algorithm with small constants can beat an O(n log n) one. Interview inputs are usually large, so Big-O wins.
"O(2n)" and "O(n + 5)" aren't wrong, but they aren't simplified. Always say O(n).
Don't drop terms for different inputs. O(n + m) where n and m are separate sizes stays O(n + m) (Ch 4).
Check yourself
4 questionsSimplify: 3n² + 10n + 7; n/2 + 100; 5; n + n log n.
Show answerHide answer
O(n²); O(n); O(1); O(n log n).
What does "f(n) is O(g(n))" mean in plain words?
Show answerHide answer
For large enough inputs, f grows no faster than some constant times g.
If an O(n²) program takes 1 second for n = 10,000, roughly how long for n = 40,000?
Show answerHide answer
16 seconds (4× the input → 4² = 16× the time).
Rank from fastest-growing to slowest: n, 2ⁿ, log n, n², n log n, 1.
Show answerHide answer
2ⁿ > n² > n log n > n > log n > 1.