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))- A. 16 9 16 12
- B. 4 9 8 12
- C. 4 17 16 8
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- An LRU cache of capacity 3 (OrderedDict, oldest first) processes page requests. How many misses, and what is the final order? python from…
- Minimum number of brackets to INSERT to make a string balanced, returned as (total, unmatched ')', unmatched '('). What is printed? python…
- Boyer–Moore majority vote WITHOUT the verification pass, returning (candidate, final counter). What is printed? python def vote(a): cand…
- Inversions counted during merge sort (pairs i < j with a[i] > a[j]). What is printed? python def inv(a): if len(a) <= 1: return a, 0 m =…
- One Hoare partition with the FIRST element (6) as pivot. What is printed (array after the partition, returned index)? python def hoare(a)…
- Counting palindromic substrings (by position) with centre expansion over 2n − 1 centres. What is printed? python def pal(s): c = 0 for…