Describe how running time grows with input size, ignoring constants.
Use it when you must compare methods by how they scale, or say whether a method will fit when the input grows. Signal words: how does the work grow, ignore the constant factor, looks fast on a small input.
Start count the steps at two sizes, n and 2n, with a counter on the basic step.
Step divide the larger count by the smaller one. About 1 means constant, about 2 linear, a little over 2 n log n, about 4 quadratic, about 8 cubic.
Stop keep the fastest-growing term of the formula, drop its constant, and write O of that term.
It does not say which method is faster at a given n. A method with a big constant can be slower at every size you will ever see, and a ratio near 2 needs a third size.
Cost: measuring costs two runs. The formula lets you skip the runs: keep the leading term and drop the rest.
In this lesson you learn to describe how work grows with the input size. You measure the work at two sizes, read the growth from the ratio, and keep only the term that wins at scale.
You work on a monitoring system. Every minute, a batch of events goes through one handler. Today a batch has 1,000 events, and in six months it will have 100,000.
Three handlers are ready. Each counts its steps. The budget is 100,000,000 steps for one batch. The team tested all three on 10 events, and the pair handler looked cheapest.
Your team lead says: Pick the handler that will still fit the budget at 100,000 events, and tell me why.
So the task is this: from step counts, say how each handler grows, and choose the one that scales.
A walker takes 3 steps for every metre and needs 40 steps to get ready. A sprinter starts fast, but each extra metre costs more than the one before.
Over 10 metres the sprinter wins. Over 10 kilometres the walker wins by a huge margin. What matters is how the effort grows with the distance, not who is ahead at the start.
Like a steady walker and a tiring sprinter: the sprinter wins over ten metres, but over ten kilometres the walker wins by a huge margin.
Before you write code, read the growth by hand. Take the step counts of one handler at two sizes, and see how many times bigger the work got.
Solve it by hand: Handler A: read the growth from two counts. The hand-solve sheet loads in your browser.
The three handlers, as code:
Each count comes from running the handler with a counter on its basic step.
| Events n | Handler A | Handler B | Handler C |
|---|---|---|---|
| 10 | 70 | 45 | 40 |
| 100 | 340 | 4,950 | 700 |
| 1,000 | 3,040 | 499,500 | 10,000 |
| 2,000 | 6,040 | 1,999,000 | 22,000 |
At 10 events, B and C are cheaper than A. At 1,000 events, B costs 499,500 steps, which is 164 times more than A. The order flipped.
Start with a correct version. Pick the handler with the fewest steps on the test size, which was 10 events.
def pick_by_test(counts_at_test_size):
best = 0
for i in range(len(counts_at_test_size)): # compare the three counts
if counts_at_test_size[i] < counts_at_test_size[best]:
best = i
return bestAt 10 events the counts are 70, 45 and 40, so it picks handler C. That is a good pick here, but it says nothing about handler A against handler B at scale.
Now check the real size.
A batch will have 100,000 events, and the budget is 100,000,000 steps.
Here is the count, from running the three handlers' formulas:
Handler B costs about 50 times the budget. The test on 10 events had hidden it.
Predict the cost
Q A batch has 100,000 events and the budget is 100,000,000 steps. Handler B compares each event with every later one. About how many times over the budget is it?
Answer: C
Handler B makes 4,999,950,000 steps. That is about 50 times the budget of 100,000,000.
A single small test cannot tell you how a count grows. Next you will measure at two sizes.
Can you tell the growth without computing a formula? Measure the work at n and at 2n, and divide.
Look at the counts of each handler at 1,000 and 2,000 events:
| Handler | Steps at 1,000 | Steps at 2,000 | Ratio |
|---|---|---|---|
| A | 3,040 | 6,040 | 1.99 |
| B | 499,500 | 1,999,000 | 4.00 |
| C | 10,000 | 22,000 | 2.20 |
Doubling the events about doubles A, quadruples B, and multiplies C by a little more than 2. The ratio is the growth. The constants 3 and 40 and the other small terms barely change it.
Race: Steps of three handlers as the batch grows. The operation counter loads in your browser.
The race counts the steps of the three handlers by running them. At 6,000 events, A makes 18,040 steps, C makes 78,000, and B makes 17,997,000.
Counted steps are exact. Time gives a second view. The table comes from timing Python loops on one laptop, taking the best of 7 runs. The times depend on the machine, so yours will differ.
| Loop | Size n | Time in milliseconds | Ratio to the size before |
|---|---|---|---|
| Pair loop | 2,000 | 37.8 | |
| Pair loop | 4,000 | 151.7 | 4.0 |
| Pair loop | 8,000 | 583.1 | 3.8 |
| Linear loop | 500,000 | 7.9 | |
| Linear loop | 1,000,000 | 15.9 | 2.0 |
| Linear loop | 2,000,000 | 32.4 | 2.0 |
Doubling n multiplied the pair loop's time by about 4, and the linear loop's time by about 2. The timed ratios follow the counted ones, with noise on top.
Doubling the input: the linear handler about doubles, the pair handler quadruples, and the halving handler grows a little over two times.
Exercise · Predict the output: What does doubling do to the pair loop?. This code counts the checks of a pair loop at n = 6 and at n = 12, then divides in hundredths. What does it print?
counts = []
for n in (6, 12):
steps = 0
for a in range(n):
for b in range(a + 1, n):
steps += 1
counts.append(steps)
print(counts[0], counts[1], counts[1] * 100 // counts[0])How much of the work comes from the biggest term? A handler makes 3n² + 5n + 20 steps. The player works out the share of the n² term at n = 100 and asks you to predict.
Dry run: Predict each step: how big is the biggest term. The step-by-step player loads in your browser.
Reading without the player? Cover the last column and predict it row by row.
| Step | Value at n = 100 |
|---|---|
| The n² term, 3 × n × n | 30,000 |
| The total, 30,000 + 500 + 20 | 30,520 |
| Share of the n² term, in whole percent | 98 |
The n² term makes 98 percent of the steps at n = 100.
Run the share on more sizes, from the same formula 3n² + 5n + 20:
| Events n | Share of the n² term |
|---|---|
| 1 | 10% |
| 10 | 81% |
| 100 | 98% |
| 1,000 | 99% |
As n grows, the lower terms and the constants fade. That is why a growth description keeps only the biggest term.
Here is the rule that gives every growth label in this lesson. Both templates use it.
Compare how the work grows when n doubles, and keep only the fastest-growing term without its constant.
This is what Big-O does. Big-O describes how the work grows with the input size n, and it drops constants and lower terms. The count 3n + 40 is O(n), and n(n-1)/2 is O(n²).
Strictly, Big-O is an upper bound. The work is O(g(n)) when, from some size on, it never exceeds a fixed constant times g(n). That is why the constant can be dropped: the definition already allows any constant multiple.
Lower terms drop for the same reason. For n of at least 40, 3n + 40 is at most 4n, so it is O(n). The bound is also loose: 3n + 40 is O(n²) too, which is true but says little.
When people name the Big-O of code, they mean the tightest bound: the work also grows at least that fast. Its formal name is Theta: 3n + 40 is Θ(n). This course writes O for that tight bound, as interviews do.
Use it when you must compare methods by how they scale, or say whether a method will fit when the input grows. Signal words: how does the work grow, ignore the constant factor, looks fast on a small input.
Start: count the steps at two sizes, n and 2n, with a counter on the basic step.
Step: divide the larger count by the smaller one. About 1 means constant, about 2 linear, a little over 2 n log n, about 4 quadratic, about 8 cubic.
Stop: keep the fastest-growing term of the formula, drop its constant, and write O of that term.
It does not say which method is faster at a given n. A method with a big constant can be slower at every size you will ever see, and a ratio near 2 needs a third size.
Cost: measuring costs two runs. The formula lets you skip the runs: keep the leading term and drop the rest.
The template takes the steps at n and at 2n, and returns the label. The ratio is in hundredths, so everything stays in whole numbers.
def growth_label(steps_before, steps_after):
# [1] Measure how many times bigger the work got when n doubled, in hundredths.
ratio = steps_after * 100 // steps_before
# [2] Barely any more work: the count does not depend on n.
if ratio < 130:
return "constant"
# [3] About twice the work: linear.
if ratio < 210:
return "linear"
# [4] A little over twice the work: n log n.
if ratio < 300:
return "n log n"
# [5] About four times the work: quadratic.
if ratio < 600:
return "quadratic"
# [6] Eight times or more: cubic or worse.
return "cubic or worse"public String growthLabel(long stepsBefore, long stepsAfter) {
// [1] Measure how many times bigger the work got when n doubled, in hundredths.
long ratio = stepsAfter * 100 / stepsBefore;
// [2] Barely any more work: the count does not depend on n.
if (ratio < 130) {
return "constant";
}
// [3] About twice the work: linear.
if (ratio < 210) {
return "linear";
}
// [4] A little over twice the work: n log n.
if (ratio < 300) {
return "n log n";
}
// [5] About four times the work: quadratic.
if (ratio < 600) {
return "quadratic";
}
// [6] Eight times or more: cubic or worse.
return "cubic or worse";
}string growthLabel(long long stepsBefore, long long stepsAfter) {
// [1] Measure how many times bigger the work got when n doubled, in hundredths.
long long ratio = stepsAfter * 100 / stepsBefore;
// [2] Barely any more work: the count does not depend on n.
if (ratio < 130) {
return "constant";
}
// [3] About twice the work: linear.
if (ratio < 210) {
return "linear";
}
// [4] A little over twice the work: n log n.
if (ratio < 300) {
return "n log n";
}
// [5] About four times the work: quadratic.
if (ratio < 600) {
return "quadratic";
}
// [6] Eight times or more: cubic or worse.
return "cubic or worse";
}function growthLabel(stepsBefore, stepsAfter) {
// [1] Measure how many times bigger the work got when n doubled, in hundredths.
const ratio = Math.floor((stepsAfter * 100) / stepsBefore);
// [2] Barely any more work: the count does not depend on n.
if (ratio < 130) {
return "constant";
}
// [3] About twice the work: linear.
if (ratio < 210) {
return "linear";
}
// [4] A little over twice the work: n log n.
if (ratio < 300) {
return "n log n";
}
// [5] About four times the work: quadratic.
if (ratio < 600) {
return "quadratic";
}
// [6] Eight times or more: cubic or worse.
return "cubic or worse";
}Rebuild the template: put its lines back in order. Loads in your browser.
Exercise · Find the bug: Every handler is called constant. This function should name the growth from the steps at n and at 2n. For a handler that doubles its work it answers "constant". Which line is wrong?
def growth_label(steps_before, steps_after):
ratio = steps_after // steps_before
if ratio < 130:
return "constant"
if ratio < 210:
return "linear"
if ratio < 300:
return "n log n"
if ratio < 600:
return "quadratic"
return "cubic or worse"The template computes the share of the n² term in the work 3n² + 5n + 20, as a whole percent. It shows how the lower terms fade.
def leading_share(n):
# [1] The biggest term: 3 times n squared.
lead = 3 * n * n
# [2] The whole work: the biggest term plus the lower terms.
total = lead + 5 * n + 20
# [3] The share of the biggest term, in whole percent.
share = lead * 100 // total
# [4] Return the share.
return sharepublic int leadingShare(int n) {
// [1] The biggest term: 3 times n squared.
long lead = 3L * n * n;
// [2] The whole work: the biggest term plus the lower terms.
long total = lead + 5L * n + 20;
// [3] The share of the biggest term, in whole percent.
long share = lead * 100 / total;
// [4] Return the share.
return (int) share;
}int leadingShare(int n) {
// [1] The biggest term: 3 times n squared.
long long lead = 3LL * n * n;
// [2] The whole work: the biggest term plus the lower terms.
long long total = lead + 5LL * n + 20;
// [3] The share of the biggest term, in whole percent.
long long share = lead * 100 / total;
// [4] Return the share.
return (int)share;
}function leadingShare(n) {
// [1] The biggest term: 3 times n squared.
const lead = 3 * n * n;
// [2] The whole work: the biggest term plus the lower terms.
const total = lead + 5 * n + 20;
// [3] The share of the biggest term, in whole percent.
const share = Math.floor((lead * 100) / total);
// [4] Return the share.
return share;
}Rebuild the template: put its lines back in order. Loads in your browser.
The biggest term of 3n squared plus 5n plus 20 makes 98 percent of the steps at n of 100, so Big-O keeps only that term.
You can name the Big-O of any handler in four steps.
Then test a third size if the ratio sits near a boundary.
Exercise · Break it: Find the counts this label misses. This version names the growth only when the count is exactly the same, exactly double or exactly four times. Find two counts where it gives the wrong label.
def growth_label(steps_before, steps_after):
if steps_after == steps_before:
return "constant"
if steps_after == 2 * steps_before:
return "linear"
if steps_after == 4 * steps_before:
return "quadratic"
return "cubic or worse"A fourth handler makes 2n² + 100n + 5 steps. Counted at n = 1,000 it makes 2,100,005 steps, and at n = 2,000 it makes 8,200,005.
Template 1 divides: 8,200,005 × 100 / 2,100,005 is 390, so the label is quadratic. The biggest term, 2n², makes 95 percent of the steps at n = 1,000. Handler D is O(n²).
Real data brings edge cases.
With n = 1, handler B makes 0 steps and handler A makes 43. Small inputs flip the order, because setup and constants dominate. Handler B is cheaper than A for every n below 14, and costlier from 14 events on (91 steps against 82).
For steps_before = 3040, steps_after = 6040 the ratio is 1.99, which is linear. A handler that makes n × ceil(log2 n) steps has the ratio 2.20 at 1,000 events, but 2.08 at 10,000,000 events, and Template 1 calls that linear. When the ratio sits between 2.0 and 2.2, measure a third size and read the formula too.
Two linear handlers make 3n + 40 and 30n + 400 steps. At n = 1000000 they make 3,000,040 and 30,000,400 steps. Both are O(n), yet one is ten times slower. Big-O does not rank them.
For n = 1000000, the work 3n² + 5n + 20 is 3,000,005,000,020 steps, which is far above the largest 32-bit integer, 2,147,483,647. The template uses 64-bit integers, so the share is exact: 99 percent.
This version runs the three handlers on 10 events and picks the one with the fewest steps.
Counterexample: at 10 events handler B makes 45 steps and handler A makes 70, so it picks B. At 1,000 events B makes 499,500 steps and A makes 3,040. One size cannot show growth. Compare two sizes.
This version says n²/1000 is tiny, so it drops it and keeps 1000n.
Counterexample: at n = 100,000 the two terms are 10,000,000 and 100,000,000, and the drop looks safe. At n = 10,000,000 they are 100,000,000,000 and 10,000,000,000, and n²/1000 is ten times bigger. Drop only terms that grow more slowly.
A term that only looks small: n squared over 1000 is below 1000n at 100,000, but ten times above it at 10,000,000.
This version says a loop that divides by 2 and a loop that divides by 10 are different classes.
Counterexample: the passes are 9 against 3 for 1,000, 19 against 6 for 1,000,000, and 29 against 9 for 1,000,000,000. The ratio stays near 3, so they differ by a constant. A quadratic against a linear count would have a ratio that grows with n. The base is dropped.
This version says O(n) is always faster than O(n²).
Counterexample: at 10 events the pair handler makes 45 steps and the linear handler makes 70. Big-O is about how the count grows. It does not say which count is smaller at a given size.
Two methods with the same Big-O can differ by a factor that matters. Handler A makes 3n + 40 steps, and a linear handler with 30n + 400 steps is ten times slower. Both are O(n).
Big-O cannot choose between them. For that, count the steps and time the real code, because the constants decide.
Your team lead brings three product changes. Each one asks you to name a growth.
A new handler sorts the 100,000 events and compares neighbours. It makes about n × 17 = 1,700,000 steps, which is O(n log n). Comparing every pair makes 4,999,950,000 steps, which is O(n²).
A handler makes 10 steps for each of 1,000 events: 10,000 steps. The inner loop is fixed, so the count is 10n and the handler is O(n). Two nested loops are not always n².
For each of n events, a handler halves a copy of n until 1. It makes n × floor(log2 n) steps: 9,000 for 1,000 events and 384 for 64. That is O(n log n).
Explain it back: write the idea in your own words and get feedback.
In two or three sentences, explain to a teammate what Big-O keeps and what it drops, and how you read growth from a ratio. Then say what Big-O does not tell you. The AI tutor checks your answer against the invariant and the key points of this lesson.
Here is how you could explain it in an interview, now that you have measured it.
Big-O describes how the work grows with the input size. I keep the fastest-growing term and drop its constant and the lower terms.
I measure at n and at 2n. About double means linear, about four times means quadratic, and a little over double means n log n.
Big-O ranks growth, not speed. At a small n a quadratic method can win, and two linear methods with different constants have the same Big-O.
Big-O describes how the work of an algorithm grows with the input size n. I keep the fastest-growing term and drop its constant factor and every lower term. So 3n plus 40 is O of n, and n times n minus 1, over 2, is O of n squared.
To read growth, I count the steps at n and at 2n. If the work about doubles, it is linear. If it about quadruples, it is quadratic. A little over double is n log n, and I test a third size when the ratio is near a boundary.
A small test can mislead. At 10 events a pair loop made 45 steps against 70 for a linear handler, but at 1,000 events it made 499,500 against 3,040.
Logarithm bases are dropped, because they differ by a constant. Big-O ranks how things scale, not which is faster at one size.
Checkpoint · Big-O as growth
Q1/10 What does Big-O describe?
Answer: B
Big-O describes growth with the input size n. It hides constants, so it is not a time.
Q2/10 A handler makes 3n + 40 steps. What is its Big-O?
Answer: C
Keep the fastest-growing term, 3n, and drop its constant. The handler is O(n).
Q3/10 A handler makes n(n-1)/2 steps. What is its Big-O?
Answer: B
The leading term is n squared over 2. Drop the constant one half, and the handler is O(n squared).
Q4/10 You count the steps of a method at n = 1,000 and at n = 2,000, and the second count is almost exactly twice the first. What is the growth?
Answer: B
Doubling the input about doubles a linear count. Handler A went from 3,040 to 6,040 steps.
Q5/10 A method makes 499,500 steps at n = 1,000. About how many does it make at n = 2,000, if its count is quadratic?
Answer: B
A quadratic count about quadruples when n doubles. The pair handler made 1,999,000 steps at 2,000.
Q6/10 At 10 events a pair handler makes 45 steps and a linear handler makes 70. Which statement is right?
Answer: B
Lower terms and constants dominate at small n. At 14 events the counts are 91 and 82.
Q7/10 A loop divides by 2 each time, and another divides by 10. For a billion items they make 29 and 9 passes. How do their Big-O classes compare?
Answer: B
The ratio is about 3.2 and stays near 3 at every size. The base of a logarithm is dropped.
Q8/10 Handler X makes 3n steps, and handler Y makes 30n steps. Which is true?
Answer: C
Constants are dropped from the Big-O, but they still change the real time.
Q9/10 The work is n squared over 1000 plus 1000n. Which term decides the growth for very large n?
Answer: B
At n = 100,000 they are 10,000,000 and 100,000,000. At n = 10,000,000 they are 100,000,000,000 and 10,000,000,000. The faster-growing term wins.
Q10/10 A handler's ratio at doubling is 2.08. What should you do before you call it linear?
Answer: C
At 10,000,000 events an n log n count has the ratio 2.08. A third size and the formula tell the two apart.
Ten questions on this lesson. Pass with 80% or more to be sure the idea has landed before the next lesson.
| Step | Problem | Product line | Level | What it practises |
|---|---|---|---|---|
| 1 | Which log grows faster | Search | Easy | Two logarithm bases differ only by a constant |
| 2 | Rank the growth of the handlers | Observe | Easy | Order by growth, and ignore constant factors |
| 3 | Predict the slowest endpoint | Platform | Medium | The biggest term wins, then its constant |
Review cards from this lesson.
Five cards from this lesson join your spaced-review deck. The first one comes back tomorrow.
Step through the idea on your own input, then build it in the lab. Runs in your browser. Step through it.
A few questions, then explain the idea back in your own words. Your tutor marks it. Quiz and explain back.