Mental model
The chaining lesson gave every bucket its own list. Open addressing has no lists at all: there is one array of slots, and each slot holds at most one entry. If a key's home slot is already taken by another key, the table tries other slots in a fixed order, the key's probe sequence, and uses the first free one.
Think of a car park where every car has an assigned space. If someone is already parked there, you do not leave; you drive along a route that is the same every time for your car, and park in the first free space on it. To find your car later you start at your assigned space and follow the same route. If you reach an empty space before you find the car, it is not in the car park: you would have parked there.
That last sentence is the whole contract of open addressing:
- Insert follows the key's probe sequence to the first free slot.
- Lookup follows the same sequence and stops at the key, or at the first empty slot.
The lab starts with 8 slots and uses home = floorMod(key, capacity), like the chaining lab.
Three probe rules
Probe number i (starting at 0) inspects one slot. All three rules start at the home slot and work
modulo the capacity, which is a power of two here.
| Rule | Probe i is at | Key 9, capacity 8 |
|---|---|---|
| Linear probing | home + i |
1, 2, 3, 4, 5, 6, 7, 0 |
| Quadratic probing (triangular numbers) | home + i(i + 1)/2 |
1, 2, 4, 7, 3, 0, 6, 5 |
| Double hashing | home + i · step, step from a second hash |
step 3: 1, 4, 7, 2, 5, 0, 3, 6 |
A probe sequence must eventually visit every slot; otherwise an insert could fail while free slots exist. Each rule above does, for a power-of-two capacity:
- Linear: trivially, one slot at a time.
- Quadratic with triangular numbers: the offsets 0, 1, 3, 6, 10, … are distinct modulo any power
of two for the first
capacityprobes. Plain squares (0, 1, 4, 9, …) do not have this property; with them the usual guarantee is weaker (a prime capacity and a load factor at most 0.5). - Double hashing: the lab uses
step = 2 · floorMod(floorDiv(key, capacity), capacity / 2) + 1. The step is always odd, so it shares no factor with a power of two, and stepping by it visits every slot before repeating. It is computed from the bits above the home slot on purpose: keys with the same home usually get different steps.
In the implementations the next slot is computed incrementally: add 1 (linear), add i (quadratic,
because 1 + 2 + … + i is the triangular number) or add step (double hashing), then mask with
capacity − 1. No multiplication can overflow.
Clustering
Run the first three presets of the lab. They insert the same keys, 1, 9, 17 and 2, and then look up the missing key 33. Keys 1, 9, 17 and 33 all have home slot 1.
- Linear probing stores 1, 9, 17 in slots 1, 2, 3, and then key 2, whose home is slot 2, has to
walk past them to slot 4. One run of occupied slots grew from two different homes. That is
primary clustering: a run attracts more keys, because every key whose home falls anywhere in
the run lands at its end, making it longer still.
get(33)needs 5 probes. - Quadratic probing jumps away from the home slot (1, 2, 4, 7, …), so key 2 finds slot 3 free
after one extra probe, and
get(33)needs 4 probes. Keys with the same home still follow the same sequence, though: secondary clustering. - Double hashing gives 9 and 17 steps 3 and 5, so they leave slot 1 in different directions and
key 2 goes straight into its home slot.
get(33)needs 3 probes. Different keys with the same home follow different sequences, which removes secondary clustering too (unless the step also matches).
Linear probing has one big advantage in real hardware: its probes touch neighbouring slots, often in the same cache line. That is why careful implementations still use it, or variations of it, and keep the load low instead.
How many probes?
Under the standard modelling assumption that keys hash uniformly and independently, Knuth's analysis of linear probing gives, for a table with load factor α = n / capacity:
successful lookup ≈ ½ · (1 + 1 / (1 − α))
unsuccessful lookup ≈ ½ · (1 + 1 / (1 − α)²) (also the cost of inserting a new key)
For an idealised "uniform hashing" scheme, where every probe sequence behaves like a random order of
all slots (double hashing comes close), an unsuccessful lookup costs about 1 / (1 − α) probes.
| α | Linear, successful | Linear, unsuccessful | Uniform hashing, unsuccessful |
|---|---|---|---|
| 0.25 | 1.17 | 1.39 | 1.33 |
| 0.5 | 1.5 | 2.5 | 2 |
| 0.75 | 2.5 | 8.5 | 4 |
| 0.9 | 5.5 | 50.5 | 10 |
Compare that with chaining, where an unsuccessful lookup costs about α comparisons and the table can work happily above α = 1. In open addressing α can never exceed 1, and the cost explodes as α approaches it. So open-addressing tables resize early. The teaching table resizes before an insert that would push the load above 0.5; CPython's dict allows about 2/3.
With the load kept below a constant, every operation is expected O(1), inserts amortized (because of resizing), and O(n) in the worst case, when many keys share a probe path.
Deletion and tombstones
Run the Tombstones on delete preset. Keys 1 and 9 both have home slot 1, so 9 lives in slot 2.
Now remove 1. If slot 1 simply became empty, get(9) would start at slot 1, see an empty slot, and
conclude that 9 is absent, which is wrong. Deleting an entry would make unrelated entries
unreachable.
So removal writes a tombstone (also called a deleted or dummy marker) instead:
- Lookups treat a tombstone as occupied: keep probing.
- Inserts may reuse it, but only after making sure the key is not stored further along.
putremembers the first tombstone it passes, keeps probing until it finds the key (then it updates in place) or an empty slot (then the key is new and goes into the remembered tombstone). In the preset,put(9, 99)passes the tombstone in slot 1 and finds 9 in slot 2: an update, not a second copy of 9.put(17, 17)reaches an empty slot, so it reuses slot 1.
put(key, value):
tomb = none
for each slot s in probe sequence of key:
if s is EMPTY: break # key is not in the table
if s is TOMBSTONE: tomb = tomb or s # reusable, keep looking
else if s.key == key: s.value = value; return
if (size + tombstones + 1) / capacity > 0.5:
resize(); tomb = none; s = first empty slot in the new table
store (key, value) in tomb if there is one, else in s
Tombstones have a cost: they never end a probe sequence, so a table full of tombstones is as slow to
search as a full table, even if it holds almost nothing. That is why the teaching table counts them
in its load check. Run Load factor 0.5 and resize: after three removals the table holds 1 key and 3
tombstones, so the next new key would make (1 + 3 + 1) / 8 = 0.625, and it resizes. A resize
reinserts only live keys, so all tombstones disappear.
For linear probing there is an alternative that needs no tombstones: after emptying a slot, scan the rest of the run and move back any entry that would otherwise become unreachable (often called backward-shift deletion). Quadratic probing and double hashing have no simple equivalent.
Resizing
A key's home depends on the capacity, so a resize must place every live key again, exactly as with chaining. The lab shows the new slot array next to the old one while keys move; each reinsertion probes the new table, which has no tombstones, for the first empty slot. One resize costs O(n), but doubling makes them rare, so n inserts cost O(n) in total: amortized O(1) per insert.
Costs
| Operation | Expected (good hash, bounded load) | Worst case |
|---|---|---|
get, contains |
O(1) | O(n): one long probe path |
put (new key) |
amortized O(1) | O(n) |
put (existing key) |
O(1) | O(n) |
remove |
O(1) (writes a tombstone) | O(n) |
| iterate all entries | O(capacity) | O(capacity) |
| space | O(capacity), no per-entry nodes or pointers |
What real runtimes do
| Strategy | Notes | |
|---|---|---|
Python dict |
open addressing | perturbation probing, dummy markers, grows at about 2/3 |
Python set |
open addressing | its own implementation in setobject.c |
C# Dictionary<K,V>, HashSet<T> |
chaining | chains are next indices inside one entries array |
Java HashMap, HashSet |
chaining | a crowded bin becomes a red-black tree |
Java IdentityHashMap |
linear probing | documented as "a simple linear-probe hash table" |
C++ std::unordered_map |
chaining | the bucket interface and reference stability effectively require it |
CPython's dict deserves a closer look, because its design is explained in long comments inside
Objects/dictobject.c. Since Python 3.6 a dict is compact: a dense array of entries (hash, key,
value) in insertion order, plus a sparse index table whose slots hold small integers pointing into
that array. Only the index table is probed. Its size is a power of two (at least 8), and a slot holds
an entry index, DKIX_EMPTY or DKIX_DUMMY. The dummy is CPython's tombstone: deleting a key sets
its index slot to DKIX_DUMMY and clears the entry.
Probing starts at i = hash & mask and continues with
perturb >>= 5 # PERTURB_SHIFT
i = (i * 5 + perturb + 1) & mask
where perturb starts as the full hash. The first few probes therefore mix in the higher bits of the
hash, which matters because CPython's hashes of small integers are the integers themselves and would
otherwise collide on their low bits. Once perturb has shifted down to zero, the recurrence becomes
i = 5i + 1 mod 2ᵏ, which visits every slot, so the search always terminates. The number of entries
the table can hold before it must grow is USABLE_FRACTION, about two thirds of the index table
size; growing builds a new index table and compacts the entries, which drops deleted ones, just like
the lab's resize.
Open addressing is also the design of several widely used high-performance tables outside the
standard libraries discussed here. Abseil's "Swiss tables" (C++) probe groups of slots using a
separate array of one-byte metadata, and Rust's standard HashMap is a port of that design.
The teaching implementation
The four listings implement the lab's model exactly: three parallel arrays (state, key, value) where
the state is empty, live or tombstone; capacity 8, doubling when 2 · (size + tombstones + 1) > capacity; floorMod homes (Python's % floors already; Java uses Math.floorMod and
Math.floorDiv; C# and C++ use ((k % c) + c) % c); the incremental probe step. Each table counts
the slots it inspects, and every language runs one shared fixture that checks the returned values,
size, capacity, number of tombstones, the final slot layout and the exact probe count, in all three
modes. Each language also replays random put, get and remove sequences against its built-in map.
Common mistakes
- Emptying a slot on delete. Later lookups stop early and lose keys stored past it.
- Inserting into the first tombstone without checking further. The key may already exist further along, and you get two copies of one key.
- Stopping a lookup at a tombstone. A tombstone means "keep going".
- Ignoring tombstones in the load factor. After many removals and inserts, every unsuccessful search walks the whole table.
- A probe sequence that skips slots, such as plain squares with a power-of-two capacity, or an even double-hashing step: inserts can fail while free slots remain.
- Letting the load get close to 1. Probe counts grow like
1 / (1 − α)²with linear probing.
Edge cases
- Lookup on an empty table: the home slot is empty, one probe, not found.
- Wraparound: key 15 in a table of 8 has home 7; if slot 7 is taken it continues at slot 0.
- Removing a missing key: probes to an empty slot, reports it, changes nothing.
putof an existing key never resizes, even when the table is at its load limit.- A new key that will reuse a tombstone does not raise the number of used slots, but the teaching table still checks the load first, so it may resize a little earlier than strictly necessary.
- Negative keys:
floorMod(−1, 8) = 7;floorDiv(−1, 8) = −1, giving double-hashing step 7.
Worked practice
1. Give the first four probe slots of key 25 in an 8-slot table under each rule.
Answer
Home is 25 mod 8 = 1. Linear: 1, 2, 3, 4. Quadratic: offsets 0, 1, 3, 6, so 1, 2, 4, 7. Double
hashing: floorDiv(25, 8) = 3, floorMod(3, 4) = 3, step = 7, so 1, 0, 7, 6.
2. Slots 3, 4, 5 hold keys 3, 11, 19 (linear probing, capacity 8). You remove 11 and then call put(19, 0). What must happen, and what would go wrong with a careless implementation?
Answer
Slot 4 becomes a tombstone. put(19, 0) probes 3 (key 3), 4 (tombstone, remembered), 5 (key 19):
it updates 19 in place. A careless version that inserts into the first tombstone would store a second
19 in slot 4; a later remove(19) would delete only one of them, and get(19) would return the stale
copy.
3. Using Knuth's approximation, how many probes does inserting a new key cost on average with linear probing at α = 0.75, and at α = 0.5?
Answer
½(1 + 1/(1 − 0.75)²) = ½(1 + 16) = 8.5 probes at 0.75, and ½(1 + 4) = 2.5 at 0.5. Halving the spare room more than triples the cost, which is why the teaching table resizes at 0.5.
4. In CPython, a key with hash 33 is looked up in an index table of 8 slots. Which slots are probed first?
Answer
mask = 7, so the first slot is 33 & 7 = 1. Then perturb = 33 >> 5 = 1 and i = (1·5 + 1 + 1) & 7 = 7. Then perturb = 0 and i = (7·5 + 0 + 1) & 7 = 36 & 7 = 4. So 1, 7, 4, and from then on the
plain 5i + 1 recurrence.
5. Why does the lab's double hashing compute the step from floorDiv(key, capacity) rather than from the key's home slot?
Answer
Keys that collide have the same home. A step derived from the home would be the same for all of them, so they would share one probe sequence, the secondary clustering double hashing is meant to avoid. The quotient carries the bits the home threw away, so colliding keys such as 1, 9 and 17 get steps 1, 3 and 5.
Summary
- Open addressing keeps entries in the slot array and resolves collisions by probing a fixed, key-specific sequence of slots; a lookup ends at the key or at an empty slot.
- Linear probing is cache friendly but forms primary clusters; quadratic probing avoids those but not secondary clustering; double hashing avoids both.
- Deletion writes a tombstone; lookups skip it, inserts reuse it only after confirming the key is absent, and the load check counts it.
- Expected probe counts grow sharply as α → 1, so these tables resize early (0.5 here, about 2/3 in CPython); with that, operations are expected O(1).
- CPython's dict and set use open addressing (dict with the perturbation recurrence and
DKIX_DUMMYtombstones); .NET's Dictionary, Java's HashMap and C++'s unordered_map use chaining.