Course map

Topic 003 / Core

Time and space complexity

Chapter 1.1 · Programming, Data Structures, and Complexity

What and why

Topic 2 picked structures by their typical cost; this topic gives the vocabulary to state that cost precisely and to predict when a choice that works today stops working as data grows. Time complexity describes how an operation's cost grows with input size n; space complexity does the same for memory. Both describe growth, not a single measurement — a function twice as slow on today's data can be a thousand times slower next year. Builds on: → 2 (data structures).

Diagram

Growth of operation count as n grows (illustrative — operation counts, not time):

Complexity n = 10 n = 1,000 n = 1,000,000
O(1) 1 1 1
O(log n) ~3 ~10 ~20
O(n) 10 1,000 1,000,000
O(n log n) ~33 ~10,000 ~20,000,000
O(n²) 100 1,000,000 1,000,000,000,000

How it works

  1. Identify the input whose size matters (the number of rows, requests, or elements) and call it n.
  2. Count how the dominant operation's cost grows as n grows; drop constants and lower-order terms — O(3n + 7) is O(n).
  3. Report time and space separately — a structure can trade one for the other, as the hash map in topic 2 does.
  4. State whether the figure is worst-case or average-case; a hash map is O(1) average but O(n) worst case under heavy collisions.
  5. Multiply the per-call cost by how often the code actually runs in production — an O(n) loop run 10,000 times a second is a bigger problem than O(n²) code that runs once a day on a small input.

Practical example

(illustrative). A request handler deduplicates a list of 10,000 item IDs in its body. A nested-loop check compares every item to every other item: 10,000 × 10,000 = 100,000,000 comparisons — an O(n²) operation. Rewriting it to insert each ID into a hash set and check membership before inserting does roughly 10,000 operations — an O(n) operation, ten thousand times fewer comparisons on this input. Both versions pass every test written against a 50-item sample; only the growth rate predicts which one survives in production.

When to use it

  • Use when: choosing between algorithms or structures before writing the code, and when deciding what to optimize first on a hot path.
  • Avoid when: the code runs rarely on small, bounded input — rewriting a once-a-day O(n²) report over 200 rows buys nothing.
  • Failure scenarios: an algorithm correct and fast at the size used in testing becomes the bottleneck silently as real data grows past that size; optimizing the wrong function because its Big-O "looks bad" when it's never actually called on large input.
  • Simpler alternative: profile actual production load (→ 103) before rewriting for Big-O — sometimes the "worse" algorithm wins at realistic n because its constant factor is smaller.

Practice

(numeric — drills with an answer key; 15–20 min, no lab services). Classify five short code snippets (a single loop, a nested loop over the same list, a hash-set membership check, a binary search, a sort followed by a scan) by time and space complexity. Then, given an illustrative traffic figure for each snippet's input size, rank the five by which would exhaust a fixed time budget first. Done when every classification matches the answer key and the predicted bottleneck order is correct.

Key takeaway

Big-O measures growth, not speed on one input; multiply it by real call frequency before deciding what to fix. Done when you can: classify an unfamiliar snippet's time and space complexity and state, in one sentence, at what input size it becomes a problem. Used next in → 64 (index lookup cost), → 100 (latency and throughput), → 101 (queueing and Little's Law).

References

None — this topic makes no external claims.

Source directory

Search the guide