Two chefs, one wedding

Counting Steps

The analogy

Two chefs both cook a perfect dinner for four. Chef A preps each guest's plate separately: 4 guests, 4 rounds of work. Chef B insists on personally introducing every plate to every other plate: 4 plates but 16 comparisons. At a dinner party nobody notices the difference. At a 500-guest wedding, Chef A does 500 units of work โ€” Chef B does 250,000 and the kitchen is on fire. The lesson: do not ask "is my code fast on my laptop today?" โ€” ask "what happens to the step count when the input gets 100 times bigger?" A single loop grows politely with the guest list. A loop INSIDE a loop grows with its square. You now know enough to smell slow code from across the room; Module 4 will give the smell its proper name: Big O.

Visualizer

Two chefs, one wedding

step 1 / 5
n = 8steps (log scale)
Chef A โ€” one pass (n steps)

Chef A preps each plate once: 8 guests, 8 units of work. Double the guests, double the work โ€” a straight, honest line.one loop -> linear

๐Ÿ“– In depth โ€” the full reference

The growth table โ€” steps at different n

shapen = 10n = 1,000n = 1,000,000smell
O(1)111array index, hash lookup
O(log n)~3~10~20halving each step
O(n)101,0001,000,000one loop
O(n log n)~33~10,000~20,000,000sort-shaped
O(nยฒ)1001,000,00010ยนยฒ โ€” daysloop inside a loop

Counting rules

  • Sequential loops ADD (n + n = still O(n)); nested loops over the same data MULTIPLY (n ร— n).
  • A hidden loop counts: `x in list` (Python) and array.includes (JS) are O(n) scans โ€” inside a loop they quietly build O(nยฒ).
  • Halving is the signature of log n: 1,000,000 โ†’ 20 steps. If your algorithm throws half away each round, it's logarithmic.
  • Constants are invisible to growth but real to users โ€” Module 4 formalises when they matter.
# Chef A โ€” one pass: n steps
for guest in guests:
    prep(guest)

# Chef B โ€” all pairs: ~n*n/2 steps. Alarm bells!
for a in guests:
    for b in guests:
        if a != b and a.email == b.email:
            print("duplicate!")

# The escape (Module 2 preview): one pass + a set
seen = set()
for g in guests:
    if g.email in seen: print("duplicate!")
    seen.add(g.email)
Check yourself

A function checks every pair of users for duplicate emails among n users. Roughly how does its work grow?

Practice โ€” write it yourself

has_duplicate(emails) returns True if any email appears twice. Chef A style please: ONE pass with a set โ€” no nested loops.

Python 3 ยท runs in your browser ยท your draft is saved locally
๐Ÿ“ My notessaved in this browser