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

Module 01 · DSA · The toolbox · about 20 min to read

Play it as a journeyTutor comes with a plan

Hashing

Hashing
Hashing · Open board

A row of numbered boxes, a key going in, and the same small sum choosing where it lands every time. Builds on Complexity & Big-O and Arrays & Strings. The same idea drives the hash index in Storage Engines and hash partitioning in Partitioning.

Before you start. You need arrays from Arrays & Strings: a numbered row of slots you can jump straight to. You also need O(1) and O(n) from Complexity & Big-O: work that stays flat no matter how much you store, versus work that grows with it.

What it is

A hash table finds a thing by working out where it has to be, instead of looking for it. Picture a wall of numbered post boxes where your name always works out to box 5. Nobody reads the wall from the left. They do the sum, walk to box 5, and your letter is there.

Who is in trouble

A records clerk in 1956. The new memory can jump to any address in one step, but jumping only helps if you know which address holds the record. So the clerk still searches, checking each record front to back, or halving a sorted file again and again. Either way the search gets slower as the file grows, and the files are growing.

By the end you can
  • Work out which box a key lands in, by hand: ‘cat’ is 3941, on 8 boxes, so box 5.
  • Say why two keys sharing a box is guaranteed, not a bug.
  • Fetch a key back from a shared box, two ways.
  • Read how full a table is and say when it should move to a bigger one.
  • Choose a hash table or a tree for a job, from whether you need order.
Say it in 30 seconds

A hash table runs one small sum on the key and uses the answer as the box number, so a lookup is one jump, not a search, however much is stored. Two keys will sometimes land in one box, so each box keeps the key beside its value and has a rule for a second one: a short stack, or the next free box. Move to a bigger table before it passes about seven-tenths full and a lookup stays at about one look. What you give up is order.

Not in this lesson

Lesson 4 of 96 in DSA

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • 1956: a file too big to search
  • Dumey’s answer: compute the box, don’t search for it
  • Two letters, one box
  • How full is too full

Terms it explains: hash, collision, bucket, load factor, resize, hash function, hash set, hash map, chaining, open addressing, probes, clusters, tombstone, amortized, HashDoS, average O(1) vs worst O(n), Bloom filter, hash join, hash set / map, hash table, load factor α, resize/rehash, SipHash.

Review DSA