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

Hashing a show in 8 chapters

Goal: find any record in about one look now about 501 looks on average

1 of 8 Your guess

Before we start

Python's dict, JavaScript's Map and Go's map all find a key fast. Which picture is closest to how you think they do it?

In c, a name's sum means each letter's place in the alphabet, added up: cat is c 3 + a 1 + t 20 = 24.

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

The file: 1,000 records. A look is one record checked.

501looks on average, front to back

Found halfway on average: 501 looks

1,000looks at worst, when it is last

every record checked

10looks halving a sorted file

1,0005002501256332168421

10 cuts, each keeping the half that holds yours, odd halves rounded up

2 of 8 The problem

1956: a file too big to search

You are a records clerk. Older machines kept records on tape: to reach one, you ran the tape past from the very start. Now there is a new kind of memory that jumps straight to any address in one step. Engineers call it random-access memory.

But a jump only helps if you know which address holds the record you want. You don't, so you still search, one record at a time, and the file keeps growing.

If the file is sorted you can halve it: look at the middle record, keep the half that must hold yours, and repeat.

Grow the file and watch the three numbers.

Why 501?

Front to back, the record is as likely to be first as last, so on average you check (1,000 + 1) ÷ 2 = 500.5 records. Half a look isn't a thing, so round up to a whole look: 501.

01234567891011121314150123456789101112131415

looks so far: 0

3 of 8 Try it

Be the clerk

Someone asks for the record on yak. Your 11 records went into the first 11 of 16 boxes, in the order they arrived. Find it.

Tap any box to open it, or open them in order.

c+a+t

The wall: 16 boxes, numbered 0 to 15

01234567891011121314150123456789101112131415

4 of 8 The idea

Work out the box

In 1956 Arnold Dumey, the engineer from before, had an idea: don't search. Work the box out from the name. Follow cat.

Press Play to add them up.

A new record arrives: act

ram01owl2ant34yak56emu7cat8cow9dog10hen11bee12fox131415ram01owl2ant34yak56emu7cat8cow9dog10hen11bee12fox131415

5 of 8 The cost

Two names, one box

Sixteen boxes, and far more possible names. Sooner or later two names give the same box. What then?

Follow act.

Why does sharing have to happen?

There are far more possible names than boxes. Put 17 names into 16 boxes and at least one box gets two, however good the sum is.

A third name arrives: tac = 20 + 1 + 3 = 24, so box 8, under cat and act.

box 8catcat: 1 lookactact: 2 lookstactac: 3 looks

Average for this box: (1 + 2 + 3) ÷ 3 = 2 looks

6 of 8 The fix

How full is too full

A box can hold a stack. To find a record you read down the stack until you reach it, so the deeper it sits, the more looks it costs.

Step 1 of 4

7 of 8 Your guess

Your picture now

Goal met: 9 looks down to 1, and 1.32 on average (measured) while the wall stays under 0.7 full. Which picture is closest now?

Pick one.

HashingWork out where a record has to be, instead of looking for it.123Open boxes one by one9 looksto find one of 11 recordsfrom chapter 3cat3 + 1 + 20 = 2424 ÷ 16 leaves 8: box 81 lookthe same box, every timecatactTwo names,one box: keepboth, in a stack1.32looks on average, if yougrow the wall before 0.7 fullmeasured, from chapter 6chalkbrain.com
HashingWork out where a record has to be,instead of looking for it.1from chapter 3Open boxes one by one9 looksto find one of 11 records2cat3 + 1 + 20 = 2424 ÷ 16 leaves 8: box 81 lookthe same box, every time3measured, from chapter 6catactTwo names,one box: keepboth, in a stack1.32looks on average, if yougrow the wall before 0.7 fullchalkbrain.com

8 of 8 Keep this

Keep this

Work out where a record has to be instead of looking for it. Let a box hold a few, and grow the wall before it passes 0.7 full.

The price: the boxes are in no order. For ranges or "the next largest", reach for a tree.

Read the full lesson