Big-O Notation

RoadmapsDSA

Overview

Big-O notation describes the upper bound on an algorithm's growth rate as the input size approaches infinity. It strips away constants and lower-order terms, capturing only the dominant factor that determines how the algorithm scales. O(f(n)) means there exist constants c and n0 such that T(n) <= c * f(n) for all n >= n0.

Exact operation counts like 3n^2 + 7n + 4 are precise but overwhelming to compare. Big-O compresses them to their essential scaling behavior — O(n^2) — so you can instantly decide whether an algorithm is feasible for a given input size without drowning in arithmetic.

Where used: Every technical interview: candidates must state and justify the Big-O of their solution before it is considered complete, System design discussions: estimating whether a proposed architecture handles expected load (e.g., O(n) indexing vs. O(log n) B-tree lookups for millions of records)

Why learn this

Aha moment

# T(n) = 5n^2 + 1000n + 999999
# At n = 10:    5*100 + 10000 + 999999 = 1,010,499  (n^2 is 0.05% of total!)
# At n = 10000: 5*100000000 + 10000000 + 999999 = 510,999,999  (n^2 is 97.8%)
# At n = 1000000: n^2 term is 99.99998% of the total

Prediction: The constant 999999 dominates for small n, so all three terms matter.

Common guess: The lower-order terms always matter because they add to the total.

At n = 10, the constant term is 99% of the work and the n^2 term is negligible. But at n = 1,000,000, the n^2 term is 99.99998% of the work and everything else vanishes. THIS is why Big-O keeps only the highest-order term — it predicts the reality at scale, where performance actually matters.

Common mistakes

Glossary

upper bound
The maximum limit on how much time or memory an algorithm can use.
constant factor
A fixed multiplier that affects actual speed but not the overall scaling category.
recursive call stack
The memory structure used by a computer to keep track of a function calling itself.

Recall questions

Understanding checks

A developer says: 'My algorithm runs in 0.5 seconds so it must be O(1).' Why is this reasoning flawed?

Big-O is not a measure of absolute time — it describes how time GROWS with input size. A 0.5-second run on one input tells you nothing about the growth rate. The algorithm might be O(n^3) on a small input that happened to be fast.

Confusing measured time with asymptotic growth rate is one of the most dangerous misconceptions because it makes engineers believe their solution scales when it does not.

You have two sorting algorithms: Algorithm X has T(n) = 1000n*log(n) and Algorithm Y has T(n) = 2n^2. For n = 10, Y is faster. For n = 1,000,000, which is faster and why does Big-O predict this?

For n = 1,000,000, Algorithm X (O(n log n)) is vastly faster. X does about 20 billion operations vs. Y's 2 trillion. Big-O predicted this because n*log(n) grows strictly slower than n^2 — the crossover is inevitable and the gap widens forever.

This demonstrates exactly why Big-O drops constants: 1000 is a huge coefficient, but it cannot prevent n*log(n) from eventually beating n^2. The growth rate always wins at scale.

Practice tasks

Simplify operation counts to Big-O

For each of the following T(n) expressions, identify the dominant term and write the Big-O: (a) 7n + 3, (b) 4n^2 + 99n + 12, (c) 2^n + n^3, (d) 500, (e) n*log(n) + n. Justify each by explaining which terms were dropped and why.

Continue learning

Previous: Counting Operations

Next: Common Complexities

Next: Amortized Analysis

Return to DSA Roadmap