All chapters[•]Data StructuresChapter 07
Data Structures · Chapter 7

Unsorted Maps and HashingData Structures · Chapter 7

An array finds item number 500,000 at once, because the position is the key. This chapter keeps that speed for keys that are words, names or order numbers: a hash function turns the key into a position, and the table jumps straight to it, without keeping the keys in order. Two keys sometimes land in the same slot, so we meet the two ways to handle a collision, chains and probing, what makes deleting tricky, and the rule that keeps the table fast as it fills: rebuild it bigger before it gets crowded.

Instructors: Boxi Yu · Yintong Huo · Python and Java versions (switch at the top) · Every program here has been run, and every output shown is the real output. · All chapters

Put keys in a table

Type a key or pick one from the list and press Put: the hash code is shown, then its remainder modulo the number of slots, then the key drops into that slot of a seven-slot table; when the slot is taken the key is appended to the chain of that slot and a counter shows how many keys were compared. Press Get to see a lookup walk the chain.

Why this matters

about 90–120 minutes (4 videos of 8–10 minutes plus the exercises).

Jump instead of search

A dictionary, a set, a cache, a symbol table, a database index and a "have I seen this before?" check are all a map from keys to values. A hash table answers get and put in about one step, however many keys it holds, while a sorted structure of chapter 6 needs about log n.

A good hash and a good table are a pair

The speed is an average: it needs a hash function that spreads the keys and a table that is not overcrowded. A bad hash function turns the table into one long list, and a table that never grows slows down with every key; this chapter shows both, with real numbers.

Equal keys must hash equally

Hashing is a contract between your key class and the table. Break it, or change a key after it has been stored, and the map does not crash: it quietly loses data. This is the most common bug in code that uses hash maps.

5 parts

Four videos with their notes, then the summary and practice. Read them in order.

01

Hash functions

A hash function works in two steps: a hash code turns any key into a whole number, and compression squeezes that number into a table index between 0 and m − 1, usually with a remainder. Two keys can share a slot, so what matters is that real keys spread out evenly. Two rules keep a map correct: equal keys have equal hash codes, and a key must not change while it is in the table.

hash codecompressionmodulocollisionpolynomial hashuniform spreadequals and hashCodemutable keys
Open →
02

Separate chaining

When two keys land in the same slot, let every slot hold a chain, a list of the entries that hash there. A lookup is one hash, one remainder and a walk along one short chain; with a good hash the chain is about as long as the load factor, so the expected cost is constant. In the worst case every key is in one chain and a lookup costs n.

separate chainingbucketchainload factorexpected O(1)worst case O(n)remove from a chain
Open →
03

Open addressing

Chaining needs a list for every slot. Open addressing needs nothing but the table: every entry lives in the array, and when the slot you want is taken you probe another slot, following a fixed sequence that depends on the key, so a lookup can follow the same sequence. Linear, quadratic and double hashing differ in the sequence. Deleting must leave a tombstone, not an empty slot, or the probe sequences of other keys break.

open addressingprobe sequencelinear probingclusteringquadratic probingdouble hashingtombstoneload factor
Open →
04

Load factor and rehashing

Every put makes the table fuller and a fuller table is slower. The remedy is the one the dynamic array uses: when the load factor passes a threshold, build a bigger table and re-insert every entry (rehashing). Entries must be re-indexed, because the index depends on the table size. Since the table grows by a factor, not by a constant, the rebuilds cost O(1) per put on average.

load factorthresholdrehashre-indexgeometric growthamortized O(1)presizingHashMapdict
Open →
05

Summary and practice

A one-page map of the chapter, five self-check questions, ten exercises with solutions, and a way to test claims an AI makes about complexity.

self-checkexercises
Open →

How to use these notes

Watch · Predict · Step through · Practise

STEP 1

Watch

Each part starts with its video, 8–10 minutes, in Python or Java.

STEP 2

Predict

Answer the amber Predict box before you open the answer.

STEP 3

Step through

The interactive demos are being added; each one is described where it will appear.

STEP 4

Practise

Finish with the self-check and the exercises, with templates in your language.