1. University MCA (AI/ML) Semester 2
  2. CAP7004
  3. Unit 4

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, with h2(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

Question 1 · Tier 1, Recall

In separate chaining, each table slot stores:

  1. The load factor
  2. Exactly one entry
  3. A list of entries
  4. A pointer to the table
Show the answer

C. Colliding keys are appended to the slot's chain.

Question 2 · Tier 3, Apply

Why must open addressing use tombstones when deleting?

  1. Tombstones make lookups faster than before
  2. An empty slot would break the probe chain for later keys
  3. Python's dict syntax requires it
  4. 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.