Section 5 · 7 min read
Hashing
Hash functions, collision resolution by chaining and open addressing, probe sequences, expected search costs and the clustering effects GATE asks about.
On this page
The idea
A hash table stores keys in an array of slots. A hash function maps each key to a slot index in . When the function spreads keys evenly, insert, search and delete take expected time. Two keys with the same slot form a collision, and the table needs a rule to resolve it.
The load factor is . With chaining can exceed 1; with open addressing always.
Hash functions
- Division method: . Choose prime and not close to a power of 2. If , only the low bits of the key are used, which wastes information.
- Multiplication method: for a constant . The choice of is not critical, so is fine.
- Universal hashing: pick at random from a family where any two distinct keys collide with probability at most . This gives expected per operation for any input, not just random input.
Simple uniform hashing assumes each key is equally likely to go to any slot, independently of other keys. Expected-cost results below rely on it.
Separate chaining
Each slot holds a linked list of the keys that hash there.
insert(k): put k at head of list T[h(k)] // O(1)
search(k): scan list T[h(k)] // O(1 + length)
delete(k): remove node from T[h(k)] // O(1) with doubly linked list and node pointer
Under simple uniform hashing:
- Expected chain length is .
- Unsuccessful search examines keys on average, time including the hash.
- Successful search examines keys on average, also .
- Worst case: all keys in one chain, search.
The probability that a given slot is empty after inserting keys is , so the expected number of empty slots is . The expected number of colliding pairs is .
Open addressing
All keys live in the table itself. Insertion probes the slots until it finds an empty one. Search follows the same sequence and stops at the key or at an empty slot.
Probe sequences
| Method | Distinct sequences | Problem | |
|---|---|---|---|
| Linear | primary clustering | ||
| Quadratic | secondary clustering, may not visit all slots | ||
| Double hashing | needs coprime with |
Primary clustering: in linear probing, occupied slots form long runs. A key hashing anywhere into a run lands at its end, so a run of length grows with probability per insertion. Big runs grow faster.
Secondary clustering: in quadratic probing, two keys with the same home slot follow identical probe sequences. Keys with different home slots do not interfere as much, so the effect is milder.
Double hashing makes the step size depend on the key, so keys with the same home slot usually take different paths. must never be 0; a common choice is with , and prime makes every step coprime to .
With , and prime, the first quadratic probes are distinct, so insertion always succeeds while . Beyond that, quadratic probing can fail to find an empty slot even when one exists.
Expected probes under uniform hashing
| Search | Uniform hashing | Linear probing (approx.) |
|---|---|---|
| Unsuccessful / insert | ||
| Successful |
The uniform-hashing column is an upper bound that double hashing approaches. As all costs blow up, which is why tables are resized (usually doubled, then all keys rehashed) once passes a threshold such as 0.5 or 0.75. Doubling keeps the amortised insert cost .
Deletion
Clearing a slot to empty breaks search: a later key that probed past this slot during its insertion will now be reported missing. The fix is a tombstone (deleted marker): search skips over it, insertion may reuse it. Tombstones lengthen searches, so tables with many deletions are rebuilt periodically, or chaining is preferred.
Worked examples
Worked example 1: Linear vs quadratic probing
Insert into an empty table of size 11 with .
Home slots: , , , , , .
Linear probing ():
- 20 goes to 9 (1 probe).
- 31: 9 taken, goes to 10 (2 probes).
- 42: 9, 10 taken, wraps to 0 (3 probes).
- 9: 9, 10, 0 taken, goes to 1 (4 probes).
- 53: 9, 10, 0, 1 taken, goes to 2 (5 probes).
- 14: home slot 3 is free (1 probe).
Final table: slot 0 = 42, 1 = 9, 2 = 53, 3 = 14, 9 = 20, 10 = 31. Total 16 probes. Notice that 14 now sits at the end of a run of six, so any future key hashing to 9, 10, 0, 1, 2 or 3 will land in slot 4.
Quadratic probing ():
- 20 goes to 9.
- 31: probes 9, then .
- 42: probes 9, 10, then .
- 9: probes 9, 10, 2, then .
- 53: probes 9, 10, 2, 7, then .
- 14: home slot 3 is now taken, .
Final: slot 2 = 42, 3 = 53, 4 = 14, 7 = 9, 9 = 20, 10 = 31. The keys sharing home slot 9 still followed one identical sequence (secondary clustering), but the occupied slots are no longer one long run.
Worked example 2: Expected probes at
Uniform hashing, unsuccessful search: probes. Successful search: probes. For linear probing the approximations give probes unsuccessful and successful. The gap between 5 and 13 is the cost of primary clustering.
Worked example 3: Where will the next key land?
A table of size 10 uses linear probing. Slots 1, 2, 3 and 6 are occupied. A new key's hash is uniform over 0..9. Find the probability that it lands in slot 4.
It lands in 4 if it hashes to 1, 2, 3 (then slides to the end of the run) or to 4 itself. That is 4 values out of 10, so probability 0.4. Slot 7 receives hashes 6 and 7 (probability 0.2), and slot 0 receives only hash 0 (0.1). Slots just after a run are the most likely next targets.
Worked example 4: Chaining statistics
1000 keys go into 250 slots with simple uniform hashing. Then . An unsuccessful search examines 4 keys on average. A successful search examines about keys. The expected number of empty slots is .
Common traps
- Read the probe function carefully: some questions use , others start at or use with specific constants.
- Count probes, not collisions. A key placed in its home slot used 1 probe and 0 collisions.
- Wrap-around: after slot comes slot 0.
- Load factor in chaining can exceed 1; open addressing cannot hold more than keys.
- The worst-case search for any hashing scheme is . Expected is not a worst-case claim.
- Do not delete in open addressing by emptying a slot.
- Quadratic probing with a non-prime can cycle through only a few slots.
Quick revision
- ; chaining search expected.
- Open addressing, uniform hashing: unsuccessful , successful .
- Linear probing: primary clustering. Quadratic: secondary clustering. Double hashing: neither.
- Next-slot probability under linear probing = (run length before the slot + 1) / .
- Use tombstones for deletion in open addressing.
- Division method: prefer prime away from powers of 2.