Data Structures & AlgorithmsHardAmazon · Microsoft OA

Problem: Double hashing with h1(k) = k mod 11 and h2(k) = 7 − (k mod 7); probe i is (h1 + i·h2) mod 11. Print the slots probed for key 58 in a table where slots 3, 5 and 8 are occupied.

Problem: Double hashing with h1(k) = k mod 11 and h2(k) = 7 − (k mod 7); probe i is (h1 + i·h2) mod 11. Print the slots probed for key 58 in a table where slots 3, 5 and 8 are occupied.
occupied = {3, 5, 8}
k = 58; h1 = k % 11; h2 = 7 - k % 7; probes = []
for i in range(11):
    s = (h1 + i * h2) % 11; probes.append(s)
    if s not in occupied: break
print(probes)
What is printed?
  1. A. [3, 4]
  2. B. [3, 8, 2]
  3. C. [3, 8, 5, 2]
  4. D. [3, 5, 7]

Correct answer: B. [3, 8, 2]

h1(58) = 3 (occupied), h2(58) = 7 − 2 = 5. Probes: 3, 8 (occupied), 13 mod 11 = 2 (free). The step size depends on the key, so keys colliding at slot 3 follow different paths, avoiding clustering.

More Coding Interview Problems questions

Exams that ask this topic