Time Complexity

Big-O for lists, dicts, and sets — the interview map

35 min · interactive lesson with runnable code and 4 graded exercises.

What you'll be able to do

  • Read Big-O as “how cost grows with n”
  • Know list, dict, and set operation costs by heart
  • Say amortized, average, and worst case correctly
  • Spot O(n·m) patterns and rewrite them as O(n + m)

Big-O in one minute

How cost grows with n

Big-O describes how runtime grows as the input size n grows. It ignores constants — it's about the shape of the curve. Switch the range and watch O(n²) leave the chart:

List operations

Fast at the end, slow at the front

One insert(0, x) is cheap enough. Doing it in a loop turns an O(n) job into O(n²). See for yourself:

Dict & set operations

Hashing makes lookups O(1)

Why O(1)? The key is hashed to a slot, and Python jumps straight there. The worst case needs pathological collisions — rare in practice:

Choosing a structure

Side by side

Watch the difference scale. Each time n doubles, the list lookup roughly doubles — the set stays flat:

Amortized vs average vs worst

Words interviewers listen for

You can actually watch the amortization: a list over-allocates, so its memory only grows every few appends:

Complexity of common patterns

Analyze, then optimize