← Course home

Lookup and priority · Lesson 27 of 52

Hash tables with linear probing

Trace open-addressing probes, tombstones, and clustering in a table array.

Full lessonAbout 10 min

You will learn to trace a probe sequence through collisions and tombstones and explain clustering and table load effects.

Recommended first: Hash tables with chaining.

On this page 6 sections

The idea

After deleting key 0, lookup of key 4 still returns 2.

Linear probing stores one key in each slot of an array. The hash picks a start slot. If that slot is occupied by a different key, the insert tries the next slot, then the next, wrapping at the end of the array. A LinearProbingHashTable with capacity 4 stores key 0 in slot 0. Key 4 has the same start slot, finds it full, and takes slot 1. Lookup of 4 repeats that probe and returns 2. Deleting 0 does not write an empty slot. It writes a tombstone. Lookup of 4 must skip the tombstone and continue, otherwise it would stop and miss a key that sits further along the same probe. An empty slot still means the key is absent. The table rehashes when the number of live keys would reach half the capacity, which keeps clusters shorter.

A worked trace

StepStateWhat changed
Insertslot = 0Key 0 takes slot 0.
Probeslot = 1Key 4 shares the start slot and takes slot 1.
Tombstonefound = 2Deleting 0 leaves a tombstone. Lookup of 4 still returns 2.

Why it is correct

The probe is a deterministic walk, so insert and lookup that start from the same hash follow the same slots. A tombstone is a slot that used to hold a key. It is not empty, because a later key may have stepped over it. Stopping at a tombstone would hide that later key. Stopping at an empty slot is safe, because an insert would have taken the first empty slot it saw. Rehashing copies every live key into a larger array and drops tombstones. The load factor is the number of live keys divided by the capacity.

A common mistake

An empty slot would hide every key that probed past the deleted one.

What it costs

One probe is constant time when clusters stay short, and linear in the worst case when keys pile into one run. Primary clustering is that pile. A collision does not stay in one slot. It makes the next inserts more likely to collide too. Extra memory is the slot arrays, one per capacity, plus the tombstones that still occupy slots until a rehash. Keep the load factor bounded if you want the expected constant probe. A short probe is Θ(1) expected time, and the slots use Θ(n) extra memory.

Run it

python
from bitsandbytes.hash_tables.linear_probing import LinearProbingHashTabletable = LinearProbingHashTable(capacity=4)table[0] = 1table[4] = 2del table[0]print(table[4])# prints: 2 

Practice and next step

Open LRU cache next. An LRU cache uses a dictionary plus a doubly linked list to drop the stale key.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.hash_tables.linear_probing

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

A collision, then a tombstone

With capacity 4, keys 0 and 4 share a start slot. Deleting 0 leaves a tombstone and lookup of 4 still returns 2.

slot
0

State

  1. 00chosen

Key 0 takes slot 0.

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1Key 0 is deleted after key 4 probed past it. What must the slot hold?
2What is the load factor of a probing table?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.