Foundations · lesson 1 · ~15 min

Big-O, felt —
not memorised.

Big-O answers one question: when the input gets 10× bigger, how much more work does the code do? Not seconds, not milliseconds. The shape of the growth. Play with each idea below, then train in the gym until reading complexity off code feels automatic.

8labs to play with
0gym problems
0you've nailed
growth.sh: same machine, 10⁸ ops/sec
input sizen = 10
bar = log-scaled work · time = f(n) ÷ 10⁸ ops/s
01

Growth Lab: watch the curves split

At small n every algorithm looks fast. Drag the size up and see which curves leave the chart. Switch to log scale to see the slower-growing ones again.

x-range
y-scale

At this size…

hover the chart or drag
n16
classoperationstime @10⁸/s
02

Count it: Big-O is just counting steps

Pick a snippet and run it. Every line keeps a hit counter. The innermost line (amber) is the work, and its count as n grows is the complexity.

snippet

READY
n6

The input

work done0
formula—
if n doubles—

Scale it up: the counts trace the curve

Each dot is the real count of the work line for that n. The line is the predicted class scaled by a constant c. Big-O drops c.

03

Simplify: the five rules

Real step counts look like 3n² + 40n + 900. Big-O keeps only the part that wins at scale. Change the coefficients and watch the biggest term take over anyway.

Who wins at scale?

→
n10
RULE 1

Assume the worst case

Searching for something might end on step one. Big-O plans for when it is the last item, or not there at all.

for x in arr: if x == target: return True # best O(1), worst O(n) → O(n)
RULE 2

Drop the constants

Two passes, half a pass, ten passes: they all grow linearly. O(2n) and O(n/2) are both O(n).

for x in arr: lo = min(lo, x) for x in arr: hi = max(hi, x) # 2n → O(n)
RULE 3

Drop the smaller terms

In n² + n, the n becomes noise once n is big. Keep the fastest-growing term.

for i in range(n): ... # n for i in range(n): for j in range(n): ... # n² # n + n² → O(n²)
RULE 4

Count only what grows

A loop that runs a fixed number of times (26 letters, 4 directions) is a constant, even if it is nested.

for i in range(n): for d in range(4): ... # 4n → O(n)
RULE 5

Different inputs get different letters

Two arrays are not both "n". One after the other adds, nested multiplies. You can't simplify a + b any further.

a5
b8
04

Constraints → target complexity

The input limits in a problem statement tell you which complexity the intended solution has. Most judges give about 1 second, and a typical machine does about 10⁸ simple operations per second. Pick an n to see what still fits.

n ≤

Vertical tick = the 10⁸ budget. Constants matter near the edge: “borderline” means it might pass with tight code.

The interview cheat-table

n up toaim forusually means
05

Recursion trees: where the work hides

A divide-and-conquer function that makes a calls on pieces of size n/b and does nd work of its own costs T(n) = a·T(n/b) + nd. Add up the work level by level. Whichever end of the tree is heaviest wins: the root, the leaves, or every level equally.

n = 64

Left: subproblems per level (size inside). Right: total work on that level. Red = heaviest level.

Tip
Not all recursion halves. fib(n-1) + fib(n-2) shrinks by one, so the tree is n levels deep and doubles each level: O(2ⁿ). Run it in lab 02.
06

Amortized: an expensive step, rarely

When a dynamic array (Python list, Java ArrayList, C++ vector) is full, it copies everything into a bigger block. That push costs O(n), but if capacity doubles, the copies are rare enough that the average stays O(1). Grow by a fixed amount instead and the average climbs with n.

EMPTY
Push some values.

Cost of each push

blue = 1 write · red = copy everything
pushes0
total cost0
avg / push0
07

O, Ω and Θ: the formal version

f(n) is O(g(n)) if some constant c makes c·g(n) stay above f(n) for every n past some point n₀. Ω is the same idea from below, and Θ means both at once. Try to find a c that works.

g(n)
c (upper)2
c (lower)0.5

f(n) = n² + 12n + 40

Bounds vs cases. Best, average and worst case describe which input you get. O, Ω and Θ describe how tightly you bound one cost function. Quicksort's worst case is Θ(n²), and its average case is Θ(n log n).
In interviews, “what's the Big-O?” means the tightest upper bound you can justify. That is usually Θ. Saying binary search is O(n³) is technically true and gets you no credit.
08

Cheat sheet

Average case unless a worst case is shown in grey. Built-ins are where hidden costs live, so check the last tab.

09

Big-O Gym

Read the code, type the time and space complexity, and see what trips you up. Answers are checked by meaning, not by spelling: n log n, nlogn and n*log(n) all count. Unsimplified answers like O(2n) are marked almost.

0day streak
0in a row
–accuracy
0/0nailed
RACE0:00

O()
O()

Space means extra memory: don't count the input, but do count the output you build and the recursion stack.

What trips you up

Accuracy by category over your attempts. Red marks your weakest area, and Practice deals more of it.