Hash Collisions: Chaining and Linear Probing
Compare two ways to keep different keys that choose the same bucket.
hash collision, separate chaining, linear probing, hash tableTwo labels, one bucket
A hash table uses a key to choose where to look for its value. Different keys can choose the same bucket: that is a collision at the bucket. A correct table must keep both entries and still distinguish their keys.
For illustration only, use integer key modulo 3: both 1 and 4 choose bucket 1. This is a toy rule, not Python's dictionary layout or hashing algorithm.
Two ways to handle the collision
Separate chaining keeps a collection of entries in each bucket:
bucket 0: empty
bucket 1: (1, A) -> (4, B)
bucket 2: empty
To find key 4, choose bucket 1 and compare keys along that chain. A new value for an existing key replaces its value rather than creating a second key.
Linear probing stores entries in slots. If the chosen slot is occupied by a different key, try the next slot, wrapping around at the end:
slot: 0 1 2
empty (1, A) (4, B)
Key 4 starts at 1 but continues to 2. A search follows the same sequence and may stop at a genuinely never-used empty slot.
Why use them and where they help
Both approaches let a map retain colliding keys. Chaining can store several entries per bucket, while probing uses the table's slots but needs free space or resizing. Probing can form clusters of occupied slots, making later searches longer. The load factor compares the number of entries to the number of buckets or slots; the precise effect depends on the method.
See the reasoning
Follow the example one step at a time
Compare chaining and linear probing for keys 1 and 4. This is a prepared teaching demonstration, not live code execution.
Choose a toy rule
key % 3: 1 → 1 4 → 1
Both keys choose bucket or slot 1; this is not Python's dictionary layout.
Python illustration
def put(buckets, key, value):
chain = buckets[key % len(buckets)]
for index, (old_key, _) in enumerate(chain):
if old_key == key:
chain[index] = (key, value)
return
chain.append((key, value))
buckets = [[] for _ in range(3)]
put(buckets, 1, "A")
put(buckets, 4, "B")
put(buckets, 1, "C")
print(buckets[1])
Prepared output — not live execution:
[(1, 'C'), (4, 'B')]
This small chaining example expects integer keys and a nonempty bucket list. It illustrates insertion and update, not a complete production map. A real implementation also needs lookup, deletion, resizing, and a suitable hash.
Costs and deletion caveats
With appropriate hash distribution and controlled load, lookup and insertion are expected O(1); collisions can make a lookup O(n) in the worst case. Chaining uses space for buckets and entries. Probing requires a rule for a full table. Simply clearing a deleted probing slot can break searches for entries beyond it: implementations can use a special deleted marker or repair the following cluster. A marker must not be mistaken for a slot that has never been used. Python's actual dictionary implementation is not this toy example.
Reflect on the concept
Four fixed questions to reinforce the reasoning. Answers are not saved or scored.
