CAP7004 · Unit 4: Hashing
Collision resolution: chaining and open addressing
8 practice questions
Key points
- Chaining: lists per slot; load factor may exceed 1
- Open addressing: probe linear, quadratic or double hash
- Linear probing causes primary clustering
- Deleting needs tombstones in open addressing
A collision occurs when two different keys map to the same index.
Separate chaining: each slot holds a list (chain) of entries. With k % 7 the keys [50, 700, 76, 85, 92, 73, 101] give: 0:[700] 1:[50, 85, 92] 2:[] 3:[73, 101] 4:[] 5:[] 6:[76]. The load factor can exceed 1. Search time is O(1 + load factor) on average.
Open addressing: all entries live in the array; on a collision, probe for another slot. Load factor must stay below 1.
- Linear probing: try i, i+1, i+2, ... (mod m). Simple and cache friendly, but causes primary clustering. Same keys with m = 7 give
[700, 50, 85, 92, 73, 101, 76]. - Quadratic probing: try i + 1^2, i + 2^2, ... reduces primary clustering. For keys 10, 20, 30, 40 in a table of 10 they land at indices 0, 1, 4 and 9.
- Double hashing: step size from a second hash,
(h1(k) + j * h2(k)) % m, withh2(k) = p - (k % p)for a prime p; spreads keys best. With m = 11, p = 7, key 19 sits at index 8, and key 30 (also home 8) steps by 7 - 30 % 7 = 5 to index 2.
Deletion in open addressing cannot just empty the slot (it would break probe chains); use a tombstone marker.
Exam pitfall: in linear probing the probe wraps around with % m.
Try two questions
In separate chaining, each table slot stores:
- The load factor
- Exactly one entry
- A list of entries
- A pointer to the table
Show the answer
C. Colliding keys are appended to the slot's chain.
Why must open addressing use tombstones when deleting?
- Tombstones make lookups faster than before
- An empty slot would break the probe chain for later keys
- Python's dict syntax requires it
- Tombstones free memory sooner than clearing
Show the answer
B. Searches stop at an empty slot, so removing an entry could hide keys placed after it.