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?- A. [3, 4]
- B. [3, 8, 2]
- C. [3, 8, 5, 2]
- 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
- Problem: Exponential (galloping) search: double the bound until a[hi] ≥ target, then binary search in (hi/2, hi]. Count the comparisons of…
- Problem: Count unique quadruplets (as value multisets) summing to target with sort + two nested loops + two pointers. python def four(a…
- Problem: For each element, the nearest smaller value to its LEFT (−1 if none), with a monotonic stack. python def leftsmaller(a): st, out…
- Problem: Report the first negative number in every window of size k (0 if none), using a queue of negative indices. python from…
- Problem: Reorder a linked list L0 → L1 → … → Ln into L0 → Ln → L1 → Ln−1 → … in place (find middle, reverse second half, merge). python…
- Problem: From a sorted linked list, delete EVERY value that appears more than once (keep only distinct values). python class N: def…