DSA Tracker

DSA Tracker

Lesson 00

Big-O from scratch

Big-O tells you how the work your code does grows when the input grows. It does not measure seconds, because seconds change with the machine. It counts steps, and asks one question: if the input gets ten times bigger, how much more work is there? Interviewers ask it about every solution, and the input limits in a problem quietly tell you which Big-O your answer needs. Learn this page first and every other lesson reads easier.

The six you will meet

From fastest to slowest

  • O(1)

    constant: the same work for any input size.

    Code shape: A fixed number of steps with no loop over the input.

    Example: Reading arr[i], or looking up a key in a hash map (on average).

  • O(log n)

    logarithmic: the work grows by one step each time the input doubles.

    Code shape: Each step throws away half of what is left.

    Example: Binary search in a sorted array of a million items needs about 20 steps.

  • O(n)

    linear: twice the input, twice the work.

    Code shape: One loop that visits every item once.

    Example: Finding the largest number in an unsorted array.

  • O(n log n)

    a little more than linear.

    Code shape: Split in halves, work on each half, combine; or sort first.

    Example: Merge sort, or the built-in sort in most languages.

  • O(n²)

    quadratic: twice the input, four times the work.

    Code shape: A loop inside a loop, both over the same input.

    Example: Checking every pair of items to find two that sum to a target.

  • O(2ⁿ)

    exponential: one more item doubles the work.

    Code shape: For each item, branch two ways: take it or leave it.

    Example: Listing every subset of a set.

Working it out

Five rules

  1. Drop constants. Two separate passes over the array are 2n steps, and that is still O(n). Big-O is about the shape of growth, not the exact count.
  2. Keep only the biggest term. n² + n is O(n²). When n is a million, the n² part is so much larger that the n part stops mattering.
  3. One after another adds, one inside another multiplies. A loop followed by a second loop is O(n + n) = O(n). A loop inside a loop is O(n × n) = O(n²).
  4. Different inputs get different letters. Looping over an array of n items and then a string of m characters is O(n + m), not O(n). Nesting them is O(n × m).
  5. Space counts too. Space complexity is the extra memory your code uses as the input grows. A hash map that may hold every item is O(n) space; two counters are O(1) space.

Before you code

Read the input limits first

A problem's limits tell you how fast your answer must be. As a rough rule of thumb, a judge runs on the order of a hundred million simple steps per second, so:

Input sizeWhat usually fits
n ≤ 20O(2ⁿ) can pass: trying every subset is fine.
n ≤ 1,000 to 5,000O(n²) usually passes.
n ≤ 100,000 to 1,000,000You need O(n log n) or O(n).
n ≥ 10⁸, or a single huge numberThink O(log n) or O(1): binary search or a formula.

What goes wrong

Common mistakes

  • Missing a hidden loop. Checking whether a list contains a value, finding a value's index, slicing and copying a string all walk the input, so calling one inside a loop is O(n²), not O(n).
  • Building a string by adding to it inside a loop. In many languages each addition copies the whole string so far.
  • Recursion that solves the same smaller problem again and again. Plain recursive Fibonacci is O(2ⁿ); remembering answers (memoization) makes it O(n).
  • Saying "O(n)" without saying what n is. Name it: the array length, the number of nodes, the length of the string.

Check yourself

What is the Big-O?

Decide before you open each answer.

  1. Question 1

    for (const x of arr) total += x;
    Show the answer to question 1

    O(n). One loop, one visit per item.

  2. Question 2

    for (let i = 0; i < n; i++)
      for (let j = 0; j < n; j++)
        count++;
    Show the answer to question 2

    O(n²). The inner loop runs n times for each of the n outer steps.

  3. Question 3

    while (n > 1) n = Math.floor(n / 2);
    Show the answer to question 3

    O(log n). n halves every step, so it reaches 1 after about log₂ n steps.

  4. Question 4

    arr.sort((a, b) => a - b);
    for (const x of arr) seen.add(x);
    Show the answer to question 4

    O(n log n). Sorting is O(n log n); the loop after it adds O(n), which is the smaller term.

  5. Question 5

    return arr[0] + arr[arr.length - 1];
    Show the answer to question 5

    O(1). Two index reads, whatever the length.