Data Structures & AlgorithmsVery hardProduct company OA

Probe-sequence coverage for open addressing: how many distinct slots can the offsets i² (quadratic) and i(i+1)/2 (triangular) reach in tables of size m? What is printed?

Probe-sequence coverage for open addressing: how many distinct slots can the offsets i² (quadratic) and i(i+1)/2 (triangular) reach in tables of size m? What is printed?
def reach(m):
    return len({(i * i) % m for i in range(m)})
def reach_tri(m):
    return len({(i * (i + 1) // 2) % m for i in range(m)})
print(reach(16), reach(17), reach_tri(16), reach_tri(12))
  1. A. 16 9 16 12
  2. B. 4 9 8 12
  3. C. 4 17 16 8
  4. D. 4 9 16 8

Correct answer: D. 4 9 16 8

i² mod 16 only takes the values 0, 1, 4, 9 → 4 slots, so an insert can fail although 12 slots are free. For a prime m = 17 it reaches (m + 1)/2 = 9 slots, which is why quadratic probing needs a prime size and load ≤ ½. Triangular offsets are a permutation of all slots exactly when m is a power of two (16 → 16), but not for m = 12 (8). That is the reason power-of-two tables use i(i+1)/2.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic