Skip to content
Foundations · 9 min

How to talk about how fast your code is

In a coding interview you will be asked how your solution behaves when the input gets large. The answer is given in a shorthand called Big-O. This lesson explains the shorthand with everyday examples, and gives you a way to work it out for your own code.

The question behind the question

Code that is fine for a hundred log lines may be useless for a hundred million. The interviewer is not asking how many seconds your code takes. That depends on the computer. They are asking: if the input becomes ten times bigger, how much more work does your code do? The same amount? Ten times? A hundred times?

The shorthand

Big-O is a short way to write the answer. The letter n stands for the size of the input: the number of lines, items or requests. O(n) is read "order n" and means the work grows in step with the input. Twice the input, about twice the work.

It is deliberately rough. It ignores small details and keeps only the shape of the growth, because the shape is what decides whether code survives a large input.

The five you will meet

  • O(1), constant. The same work whatever the size. Looking up a word in a dictionary when you already know the page number.
  • O(log n), logarithmic. Each step throws away half of what is left. Finding a name in a phone book by opening it in the middle, then the middle of the right half, and so on. A million names take about twenty steps.
  • O(n), linear. Look at every item once. Reading every line of a file to count the errors.
  • O(n log n). A little worse than linear. This is the cost of sorting. If your solution sorts the input, it is at least this.
  • O(n²), quadratic. For every item, look at every other item. Comparing each person in a room with each other person. Ten times the input means a hundred times the work.
items (n)      O(log n)    O(n)         O(n log n)     O(n²)
1,000          10          1,000        10,000         1,000,000
1,000,000      20          1,000,000    20,000,000     1,000,000,000,000

Read the last row. At a million items, the linear approach does a million steps, which a computer finishes in a blink. The quadratic one does a million million, which will not finish while the interviewer is still in the room.

Working it out for your own code

You do not need mathematics. Look at the loops.

  • One loop over the input is O(n).
  • A loop inside a loop, both over the input, is O(n²).
  • Sorting the input anywhere makes it at least O(n log n).
  • Two loops one after the other are still O(n). Twice the work is the same shape.
  • Halving the problem each step is logarithmic.
# O(n²): for each request, scan all the others looking for the same user
for a in requests:
    for b in requests:
        if a.user == b.user: ...

# O(n): one pass, remembering what has been seen in a dictionary
seen = {}
for r in requests:
    seen[r.user] = seen.get(r.user, 0) + 1
A hidden loopAsking whether a value is in a list makes the computer walk along the list, so doing that inside a loop is a loop inside a loop. Asking the same question of a dictionary or a set takes one step. This single change turns many slow solutions into fast ones.

Memory counts too

The same shorthand describes memory. The dictionary above holds one entry for each different user, so it uses O(n) memory in the worst case. Faster code often uses more memory. Say so: it is a trade, and naming the trade is part of the answer.

Linked exercise · needs a computer · 10 minParse and bucket latency logsRead it here, do it on a computer

What to say out loud

State it before you are asked, in one or two sentences. Use the words of the problem, not just letters. For example: "This reads each log line once, so the time grows in step with the number of lines. It keeps one counter for each endpoint, so memory grows with the number of different endpoints, not the number of lines." Then say what would make it worse, such as needing to sort.