Module 0 · Lesson 1 · ~25 min

Big-O — the napkin you sketch in interviews

Big-O isn't math for math's sake. It's the back-of-the-napkin question every senior engineer asks instinctively: how does the work grow as the input grows? Get this in your bones and the rest of the course gets twice as easy.

The metaphor first: the line at the DMV

Picture two clerks at the DMV.

Clerk A processes one person at a time. Five people in line? Five units of work. Fifty? Fifty units. The work scales proportionally with the line. That's O(n).

Clerk B insists on cross-checking each person against every other person already seen, "just in case." With one person, one check. With two, three checks. With ten, forty-five checks. With a hundred, almost five thousand checks. That's O(n²) — work scales with the square of the line.

Both clerks finish a line of one in the same blink. Both finish a line of five about the same. But around 50 people, Clerk B starts visibly drowning. At 5,000, Clerk A is on lunch break and Clerk B has hit the heat death of the universe.

The metaphor

Big-O is "how does this clerk feel when the line gets long?" Constants — coffee breaks, a slow day — wash out. The shape of the curve wins.

The curves you have to know on sight

Below is the same chart you'll mentally draw in every interview. Notice how some curves stay nearly flat, and others go vertical so fast they vanish off the top of the page.

NotationNameReal-world exampleVibe
O(1)constantLooking up a value in a Python dict by key.Free.
O(log n)logarithmicBinary search in a sorted array. Finding a name in a phone book.Cheap, even at scale.
O(n)linearReading every email in your inbox once.Fair — you have to look at everything.
O(n log n)linearithmicSorting (mergesort, quicksort, Timsort).The "best general sort" curve.
O(n²)quadraticNested loops over the same list. Checking every pair.Tolerable for small n. Suspicious in interviews.
O(2ⁿ)exponentialNaive recursion over subsets. "Try every combination."Tractable up to n ≈ 25. After that — pain.
O(n!)factorialGenerating all permutations of a list.Up to n ≈ 11. Beyond that, lifetime-of-the-sun.
Interview reality

The interviewer is rarely looking for "the optimal solution." They're looking for "can you tell me, out loud, that your solution is O(n²) and that there exists an O(n) solution if we accept O(n) extra space?" Conversational fluency in trade-offs is the actual signal.

How to compute Big-O — practically

Three rules cover 95% of cases.

1. Drop constants and lower-order terms

If your algorithm does 3n + 7 operations, that's still O(n). The reason isn't laziness — it's that constants and lower-order terms get swamped by the dominant term as n grows. 3n + 7 at n = 1,000,000 is overwhelmingly about the n.

If you do n² + n operations, that's O(n²). The + n stops mattering by n = 100.

2. Sequential code adds, nested code multiplies

for x in arr:    # O(n)
    print(x)
for y in arr:    # O(n)
    print(y)
# Total: O(n) + O(n) = O(n)
for (const x of arr) console.log(x);   // O(n)
for (const y of arr) console.log(y);   // O(n)
// Total: O(n) + O(n) = O(n)
for (const x of arr) console.log(x);   // O(n)
for (const y of arr) console.log(y);   // O(n)
// Total: O(n) + O(n) = O(n)
for x in arr:        # O(n)
    for y in arr:    # O(n)
        print(x, y)
# Total: O(n) × O(n) = O(n²)
for (const x of arr)        // O(n)
  for (const y of arr)      // O(n)
    console.log(x, y);
// Total: O(n) × O(n) = O(n²)
for (const x of arr)        // O(n)
  for (const y of arr)      // O(n)
    console.log(x, y);
// Total: O(n) × O(n) = O(n²)

3. Recursion is "how many calls × work per call"

If a function calls itself twice, halving the input each time, you're looking at a tree of log₂ n levels with 2^level nodes. The total node count is 2^log₂(n) = n. That's O(n) calls, often with O(1) work each → O(n). We'll do this rigorously in lesson 3.

Two inputs, two letters

When an algorithm consumes two independent inputs, use two letters. Don't say "O(n²)" when you mean "O(n + m)" or "O(n × m)" — interviewers notice.

def merge(a, b):
    # walks through a once, then b once
    # O(n + m), not O(n²)
function merge(a, b) {
  // walks through a once, then b once
  // O(n + m), not O(n²)
}
function merge(a: number[], b: number[]): number[] {
  // walks through a once, then b once
  // O(n + m), not O(n²)
  return [];
}
def cross_join(a, b):
    # every element of a paired with every element of b
    # O(n × m)
function crossJoin(a, b) {
  // every element of a paired with every element of b
  // O(n × m)
}
function crossJoin(a: number[], b: number[]): [number, number][] {
  // every element of a paired with every element of b
  // O(n × m)
  return [];
}

Average, worst, best — and amortized

Big-O usually means worst case. But two other phrases come up constantly:

Vocabulary trap

If an interviewer asks "is hash-map lookup O(1)?" the strong answer is: "Average case yes, worst case O(n) under bad collisions or adversarial inputs — Python and Java both randomize their hashes specifically to defend against this." That sentence will impress more than any whiteboarded solution.

Space too — not just time

Big-O describes any resource. The two we care about:

A solution that builds a hash map of every element you've seen is O(n) space. A solution that uses two pointers and walks in place is O(1) space. Both might be O(n) time. Senior interviews care which you used.

The Big-O cheat sentence

Practice this until you can say it without thinking, in your own voice:

Say it out loud

"My solution is O(X) time and O(Y) space. The X comes from (loop / sort / recursion), and the Y comes from (hash map / call stack / output). We could trade time for space by (memoizing / caching / preprocessing) if needed."

That sentence — said calmly, with the right blanks filled in — is what FAANG L5/L6 candidates sound like. Not the brilliant solution. The fluent description.

Try it: classify these by sight

Open the code runner. Read each function. Without running it, write out the time complexity in the comment line. Then run it for n = 100, 1000, 10000 and see if the actual time matches your prediction. Save your reasoning to the 📓 notebook — these will be your reference card.

Answers (try first, then peek)

f1: O(n) — single loop. f2: O(n²) — nested loops. f3: O(log n) — i doubles each step, so the loop runs about log₂(n) times. f4: O(2ⁿ) — call tree branches by 2 at each level, depth n.

What to put in your notebook

Hit the 📓 button and capture, in your own words: