What Is Big O Notation? Explained With Examples

Big O notation describes how an algorithm's running time or memory use grows as input size increases. Learn O(1), O(n), O(log n), and O(n^2) with examples.

Published September 22, 2026

Big O notation describes how an algorithm's running time or memory grows as the input size n grows. It ignores constants and focuses on the dominant term, such as O(n) or O(n²).

The concept

O(1) is constant, O(log n) grows very slowly (binary search), O(n) grows in step with input (a loop), O(n log n) is efficient sorting, and O(n²) usually means nested loops.

Big O describes the worst case unless stated, and tells you how an algorithm scales, not its exact speed on one machine.

Growth when n = 1,000

O(1) 1 step
O(log n) ~10 steps
O(n) 1,000 steps
O(n log n) ~10,000 steps
O(n²) 1,000,000 steps

Practical examples

O(1)

const first = arr[0]

O(n)

for (const x of arr) sum += x

O(n²)

for (const a of arr) for (const b of arr) pairs.push([a, b])

O(log n)

function bs(a, t) {
  let lo = 0, hi = a.length - 1
  while (lo <= hi) {
    const m = (lo + hi) >> 1
    if (a[m] === t) return m
    a[m] < t ? lo = m + 1 : hi = m - 1
  }
  return -1
}

Common mistakes

  • Counting constants: O(2n) is just O(n).
  • Forgetting hidden costs such as includes() inside a loop.
  • Optimising before measuring.

Key takeaways

  • Big O = growth rate.
  • Drop constants and lower terms.
  • Use hash sets to turn O(n²) lookups into O(n).

FAQ

What about space complexity?

Same notation, but for memory used.

Is lower Big O always faster?

Not for small n; constants and cache effects can dominate.

More General articles