Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

DSA the lesson

Complexity & Big‑O a show in 8 chapters

Goal: predict the check at 100,000 people, then make it 100 times faster

1 of 8 Your guess

Before we start

Two programs each take one second on your laptop. Which is better? Programmers have a short way of saying, and interviewers ask for it on every coding question.

Which picture is closest to what that short answer measures?

Pick the closest. You find out which fits at the end.

10,000 people in line51.7 ms, measured100,000 people in line?ms: a millisecond, a thousandth of a second

2 of 8 The problem

The check that slows down

You run a front desk with a long line of people. Every morning the desk's computer hunts for a matching pair, two people with the same name, by checking every person against every other person.

With 10,000 people in line it takes 51.7 ms. The lesson timed this kind of every-person-against-every-person check. Next month the line is ten times longer: 100,000 people. How long then?

Pick one. Chapter 4 works it out.

frontbackMeiSamAnaJoeRaviLuKaiZoeBenIdan = 10: the number of people in the linesteps so far: 0, one tile per check

3 of 8 The idea

Count, don't time

Start with the desk's simplest job: find one name. The line is not sorted, so the receptionist checks faces from the front, one at a time. Today Ida is at the back.

n stands for how many people are in the line. Here n = 10. Each check of one face is one step.

Press Play to check the faces.

Step 1 of 3

each of 10 meets all 1012345678910personchecks so far: 0 × 10 = 0

4 of 8 The cost

Every pair

Back to the morning check. Each person is checked against every person in the line. Start small: 10 people, one row of checks each.

Press Play to check every pair.

Step 1 of 4

sorted A to P · looking for KABCDEFGHIJKLMNOPmiddleleft to search: 16 of 16questions so far: 0, one tile each

5 of 8 Try it

Halve the line

Before fixing the morning check, one more trick. Sort the line by name. To find K, ask "before the middle or after it?" and set aside the half that cannot hold the name.

Press Play to ask.

Step 1 of 3

as they arrivedMeiSamAnaJoeRaviLuAnaKaievery pair: 8 × 8 = 64 checkssorted by nameAnaAnaJoeKaiLuMeiRaviSameach person checks only the next one:7 checks, not 64. The first one finds the Anas.

6 of 8 The fix

Sort first

Now the fix for the morning check. Sort the line by name first. The two people with the same name end up side by side, so each person checks only the one next to them.

Step 1 of 4

7 of 8 Your guess

Your picture now

Goal met: 5.17 s at 100,000 people, predicted by counting, and sorting first brought it to 38.4 ms, about 135 times faster. Which picture is closest now?

Pick one.

Complexity and Big-OCount how the steps grow when the line doubles. Never the seconds.8 people16 peoplethe line doublesO(1)11stays the sameO(log n)34× 1.3O(n)816× 2O(n log n)2464× 2.7O(n²)64256× 4O(2ⁿ)25665,536× 256chalkbrain.com
Complexity and Big-OCount how the steps grow whenthe line doubles. Never the seconds.steps at 8 people → 16 peopleO(1)1 → 1the sameO(log n)3 → 4× 1.3O(n)8 → 16× 2O(n log n)24 → 64× 2.7O(n²)64 → 256× 4O(2ⁿ)256 → 65,536× 256chalkbrain.com

8 of 8 Keep this

Keep this

Don't time it. Count how the steps grow as the line grows, and name the shape. A faster machine changes the seconds, never the shape.

The price: Big-O leaves out the constant. The lesson's sort did 753 times fewer steps at 10,000 and ran about 13 times faster. Pick the shape first, then measure.

Read the full lesson