3.3.2 · D3Hashing

Worked examples — Hash table — structure, open addressing vs chaining

2,552 words12 min readBack to topic

The scenario matrix

Every problem in this topic is one of these cells. We will hit each one with a worked example.

# Case class What makes it special Example
C1 No collision every key lands in an empty slot Ex 1
C2 Chaining collision multiple keys share a bucket Ex 2
C3 Linear-probe collision array-internal sliding Ex 3
C4 Quadratic probing jumps , may skip slots Ex 4
C5 Double hashing second hash sets the step Ex 5
C6 Degenerate: all-collide worst case, every strategy Ex 6
C7 Deletion + tombstone the severed-chain trap Ex 7
C8 Limiting load factor , probe blow-up, rehash Ex 8
C9 Word problem real strings via polynomial hash Ex 9
C10 Exam twist quadratic probing fails to insert Ex 10

Before we start, one symbol we reuse: means "the remainder when is divided by ." Example: — how many left over after taking out fives? , remainder . That remainder is always in , exactly the legal slot range. That is why is the compression step: it forces any integer into a valid index.

Figure 1 below shows this compression visually: five big keys funnelling down to their five slots in a table of size . Follow a coral arrow from a key to the slot its remainder picks — that arrow is the hash function.


C1 — The easy case: no collisions


C2 — Chaining collision


C3 — Linear probing


C4 — Quadratic probing


C5 — Double hashing


C6 — Degenerate: everything collides (all four strategies)


C7 — Deletion and the tombstone trap


C8 — Limiting load factor: probes blow up


C9 — Word problem: hashing real strings


C10 — Exam twist: quadratic probing fails to insert


Recall Self-test across the matrix

Which cell does each belong to? Insert 3,11,5 into , all distinct slots ::: C1 (no collision) Bucket 4 becomes [4,10,16] ::: C2 (chaining collision) Blanking a slot severs a probe chain ::: C7 (tombstone) probes ::: C8 (limiting load factor) "ab"→slot 11, "ba"→slot 2, ::: C9 (word problem) Quadratic reaches only in ::: C10 (exam twist)