Module 0 · Lesson 1 · ~25 min
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.
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.
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.
| Notation | Name | Real-world example | Vibe |
|---|---|---|---|
O(1) | constant | Looking up a value in a Python dict by key. | Free. |
O(log n) | logarithmic | Binary search in a sorted array. Finding a name in a phone book. | Cheap, even at scale. |
O(n) | linear | Reading every email in your inbox once. | Fair — you have to look at everything. |
O(n log n) | linearithmic | Sorting (mergesort, quicksort, Timsort). | The "best general sort" curve. |
O(n²) | quadratic | Nested loops over the same list. Checking every pair. | Tolerable for small n. Suspicious in interviews. |
O(2ⁿ) | exponential | Naive recursion over subsets. "Try every combination." | Tractable up to n ≈ 25. After that — pain. |
O(n!) | factorial | Generating all permutations of a list. | Up to n ≈ 11. Beyond that, lifetime-of-the-sun. |
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.
Three rules cover 95% of cases.
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.
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²)
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.
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 [];
}
Big-O usually means worst case. But two other phrases come up constantly:
O(n log n), worst O(n²).list.append is amortized O(1): it's free 99% of the time and O(n) on the rare occasion the list has to grow its underlying array. We'll see why next lesson.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.
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.
Practice this until you can say it without thinking, in your own voice:
"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.
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.
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.
Hit the 📓 button and capture, in your own words:
f1–f4 above.