Hash Table — Linear Probing
A hash table with linear probing is an open-addressing hash table that stores every key in one array and resolves collisions by scanning forward to the next free slot. With a well-spread hash and few occupied slots (live keys and tombstones alike), insert, get, and delete run in O(1) average time; the tradeoff is clustering as the table fills.
Think of the array as a parking lot with numbered spaces, where the hash function h(k) = k mod capacity gives the space on your permit. If that space is taken, you move forward to the next space in order, wrapping around the end of the lot until you find a free one.
How Linear Probing Works
The table is a fixed array of capacity slots, each in one of three states: empty (never used), occupied (holds a key), or tombstone (a key was deleted here). A tombstone acts like a cone in a vacated space: the slot is reusable, while signaling to later lookups that the probe sequence used to continue past it.
All three operations start at i = hash(k) and probe forward with i = (i + 1) mod capacity, for at most one full lap.
get(k) inspects each slot in order. An occupied slot holding k returns the index. An empty slot stops the search: if k existed, it would have been placed along this run before any empty gap appeared. A tombstone or a non-matching key advances the probe. This is why delete cannot blank a slot: an empty cell marks the end of any probe sequence, and creating an empty gap in the middle of a run makes keys further down unreachable.
delete(k) runs the same probe and, on a match, writes a tombstone instead of empty. The key is removed while later lookups still probe past the slot.
insert(k) tracks spot, the first reusable slot it passes (the first tombstone, or otherwise the empty terminator). It does not stop at the first tombstone; it keeps probing until it hits an empty slot (confirming k is absent) or finds k already in the table (returning without writing, since inserts are idempotent). Only after confirming absence does it write into the remembered spot. Stopping at the first tombstone could insert a duplicate of a key sitting further down the run. If a full lap finds neither a match nor a reusable slot, insert raises an overflow. That overflow belongs to open addressing itself: because the array is the entire storage, a full table has nowhere left to place a key.
In this visualizer, the capacity slider runs from 5 to 11 and keys are limited to 1–99 so slots stay legible on the canvas. Table-full overflow, however, is the data structure's own behavior at any size.
Linear Probing Step by Step
The default table is empty with capacity 7, so h(k) = k mod 7. Each operation below matches the animation beat for beat: a hash-computation beat, one beat per slot probed, and a commit beat.
insert(10): h(10) = 10 mod 7 = 3. Slot 3 is empty, so 10 is absent; place it at slot 3.
insert(20): h(20) = 6. Slot 6 is empty; place 20 at slot 6.
insert(17): h(17) = 3. Slot 3 holds 10, so probe next. Slot 4 is empty; place 17 at slot 4. This is the first collision: two keys map to slot 3, and the second advances to slot 4.
insert(24): h(24) = 3. Slot 3 holds 10, slot 4 holds 17, and slot 5 is empty; place 24 at slot 5. Slots 3, 4, and 5 now form one contiguous cluster. Any absent key hashing to 3 (a miss or an insert of a new key) must walk the entire cluster to reach the empty slot beyond, while a hit stops as soon as it finds its key.
get(17): h(17) = 3. Slot 3 holds 10, so probe next. Slot 4 holds 17: found at index 4 after 2 probes.
delete(10): h(10) = 3. Slot 3 holds 10, matching the target. Mark slot 3 as a tombstone rather than empty.
get(17) again: h(17) = 3. Slot 3 is a tombstone, so keep probing past it. Slot 4 holds 17: found at index 4. Had delete blanked slot 3 instead, this lookup would have stopped at the gap and wrongly reported not-found.
insert(31): h(31) = 31 mod 7 = 3. Slot 3 is a tombstone: remember it as the reuse spot and keep probing. Slot 4 holds 17, slot 5 holds 24, and slot 6 holds 20; the probe wraps to slot 0, which is empty. With 31 confirmed absent, write it into the remembered slot 3. The tombstone is reclaimed only after the probe walks the full run to rule out a duplicate.
In the player, the probe cursor steps slot by slot from h(k), the slot under inspection highlights on each beat, and tombstones render in a muted marker color so you can distinguish them from empty spaces at a glance.
Time and Space Complexity
| Operation | Average (well-spread hash, few occupied slots) | Worst case |
|---|---|---|
insert | O(1) | O(m) |
get | O(1) | O(m) |
delete | O(1) | O(m) |
Space is O(m) for a table of m slots, because the array is the only allocation with no per-entry pointers, which is open addressing's structural advantage over chaining.
The worst case is one full lap of all m slots. Average performance depends on how full the array is. Probes walk past live keys and tombstones alike, so after deletes the live load factor α = n / m understates the cost (a table filled with tombstones holds no keys, yet a miss probes all m slots). Probe counts grow as the occupied fraction approaches 1. Successful and unsuccessful searches have different expected costs, and linear probing's clusters make a single 1 / (1 − α) estimate insufficient for both. The mechanism behind that growth is primary clustering: occupied slots form contiguous runs, a miss or new-key insert hashing anywhere into a run must traverse to its end, and every insert that lands in a run extends it, so larger clusters grow faster than smaller ones. The slots-3-through-5 run in the trace above is a cluster in miniature. Production tables resize (rehashing every live key into a larger array) before α crosses roughly 0.7–0.75 to keep probe chains from merging across the table.
Edge Cases
- Duplicate insert.
insert(k)whenkis already present finds the match during the probe and returns without writing, leaving the existing entry intact with no duplicate. - Full table. A full probe lap with no empty slot, no tombstone, and no match raises overflow. Production implementations resize before reaching this point.
- Delete or get on a missing key. The probe stops at the first empty slot and reports not-found; if no slot is empty, the one-lap cap guarantees termination after m probes.
- Tombstone accumulation. Heavy insert/delete churn leaves the table mostly tombstones with few live keys, and probes still walk past every tombstone, so lookups degrade even at low live load. Resizing also cleans up tombstones, since only live keys get rehashed.
Common Mistakes
- Deleting by blanking the slot. Any key that probed past the deleted slot becomes unreachable because lookups stop at the false gap. Tombstones exist to preserve the probe chain.
- Insert stopping at the first tombstone. Writing there immediately can duplicate a key sitting further down the run. Remember the spot, finish the probe, and write only after confirming absence, as the displayed code does.
- Forgetting the modular wrap. Probing uses
i = (i + 1) mod capacity. Without the modulo, a key hashing near the end of the array walks off the end instead of wrapping to slot 0, asinsert(31)does above. - Skipping tombstones on insert. Lookups probe past tombstones, while inserts reuse them. Treating a tombstone as occupied leaks slots permanently.
- Never resizing. Above roughly 75% load, clusters merge and average probe length climbs steeply toward O(m). Monitoring load factor and doubling the table is required to preserve O(1) average performance.
Keep exploring
A few lessons that share the same technique or lead into the next idea.
Embed this lesson in a blog, docs, or course?
Found a mistake or something unclear?