Quick answer: Big-O notation describes how the running time (or memory) of an algorithm grows as the input size n grows, ignoring constants. Common classes from fastest to slowest: O(1) constant, O(log n) logarithmic, O(n) linear, O(n log n), O(n²) quadratic, O(2ⁿ) exponential. It helps you predict whether code that works for 100 records will survive 10 million.

Big-O is asked in almost every coding interview, but it is also a daily engineering tool: it explains why a report times out, why an API slows down as users grow, and why adding an index fixes a query. Here it is in plain language.

The growth table

Big-O

Name

n = 1,000

n = 1,000,000

Example

O(1)

Constant

1

1

Hash map lookup, array index

O(log n)

Logarithmic

~10

~20

Binary search, B-tree index lookup

O(n)

Linear

1,000

1,000,000

Loop over a list

O(n log n)

Linearithmic

~10,000

~20,000,000

Efficient sorting (merge sort, Timsort)

O(n²)

Quadratic

1,000,000

10¹²

Nested loops comparing all pairs

O(2ⁿ)

Exponential

astronomical

impossible

Trying every subset

Notice: going from 1,000 to 1,000,000 items, O(log n) barely changes, while O(n²) becomes a million times slower.

Rules for calculating Big-O

  1. Drop constants: O(2n) → O(n).
  2. Keep the dominant term: O(n² + n) → O(n²).
  3. Sequential steps add: loop A then loop B → O(a + b).
  4. Nested steps multiply: loop inside a loop → O(a × b).
  5. Halving each step → log n.
  6. Use different variables for different inputs: O(users × orders), not O(n²).

Examples

// O(n): one pass
function sum(arr) {
  let s = 0;
  for (const x of arr) s += x;
  return s;
}

// O(n²): compare every pair
function hasDuplicateSlow(arr) {
  for (let i = 0; i < arr.length; i++)
    for (let j = i + 1; j < arr.length; j++)
      if (arr[i] === arr[j]) return true;
  return false;
}

// O(n): use a Set (O(1) average lookups)
function hasDuplicateFast(arr) {
  const seen = new Set();
  for (const x of arr) {
    if (seen.has(x)) return true;
    seen.add(x);
  }
  return false;
}

// O(log n): binary search on a sorted array
function binarySearch(a, target) {
  let lo = 0, hi = a.length - 1;
  while (lo <= hi) {
    const mid = (lo + hi) >> 1;
    if (a[mid] === target) return mid;
    a[mid] < target ? lo = mid + 1 : hi = mid - 1;
  }
  return -1;
}

Space complexity

Big-O also measures extra memory. hasDuplicateFast is O(n) time but also O(n) space for the Set – a classic time/space trade-off. Recursion uses stack space: a recursion depth of n means O(n) space.

Common data structure costs (average case)

Structure

Access

Search

Insert

Delete

Array / list

O(1)

O(n)

O(n) (O(1) at end)

O(n)

Hash map / set

–

O(1)

O(1)

O(1)

Balanced BST / sorted map

O(log n)

O(log n)

O(log n)

O(log n)

Heap (priority queue)

min/max O(1)

O(n)

O(log n)

pop O(log n)

Linked list

O(n)

O(n)

O(1) at known node

O(1) at known node

Big-O in real backend work

  • Database without an index: full table scan O(n). With a B-tree index: about O(log n).
  • N+1 queries: loading 100 orders and then querying customers one by one = 101 queries. Use eager loading/joins.
  • Nested loops matching two lists (e.g. reconciling payments): convert one list to a map first – O(n²) becomes O(n).
  • Sorting large exports in application memory: O(n log n) plus memory; let the database sort with an index.
  • Pagination: deep OFFSET pagination scans skipped rows; cursor pagination stays fast.

Best, average and worst case

Quicksort averages O(n log n) but can degrade to O(n²) in the worst case; hash maps are O(1) on average but O(n) in pathological collisions. In interviews, state which case you mean.

Summary

  • Big-O describes growth, not exact time.
  • Drop constants, keep the dominant term, add sequential, multiply nested.
  • Hash maps turn many O(n²) problems into O(n).
  • Indexes, eager loading and cursor pagination are Big-O fixes in real systems.