Data Structures & AlgorithmsVery hardProduct company OA

Open addressing with linear probing (size 7, h(k) = k mod 7). Deletion leaves a tombstone 'X'; insert reuses the first tombstone; find stops only at an EMPTY slot and returns (found, probes). What is printed?

Open addressing with linear probing (size 7, h(k) = k mod 7). Deletion leaves a tombstone 'X'; insert reuses the first tombstone; find stops only at an EMPTY slot and returns (found, probes). What is printed?
M = 7
table = [None] * M
DEL = "X"
def ins(k):
    i = k % M
    while table[i] not in (None, DEL): i = (i + 1) % M
    table[i] = k
def find(k):
    i, probes = k % M, 0
    while True:
        probes += 1
        if table[i] is None: return False, probes
        if table[i] == k: return True, probes
        i = (i + 1) % M
for k in [10, 17, 24, 5, 3]: ins(k)
table[table.index(17)] = DEL
ins(31)
print(table, find(24), find(38))
  1. A. [3, None, None, 10, 'X', 24, 5] (True, 3) (False, 2)
  2. B. [3, None, None, 10, 31, 24, 5] (False, 2) (False, 6)
  3. C. [3, 31, None, 10, 'X', 24, 5] (True, 3) (False, 3)
  4. D. [3, None, None, 10, 31, 24, 5] (True, 3) (False, 6)

Correct answer: D. [3, None, None, 10, 31, 24, 5] (True, 3) (False, 6)

10→3, 17→4, 24→5 (3, 4 taken), 5→6, 3→ 3,4,5,6 taken → 0. Deleting 17 must leave a tombstone: emptying slot 4 would break the probe chain for 24. 31 hashes to 3 and reuses the tombstone at 4. find(24) probes 3, 4, 5 → found in 3. find(38) (38 mod 7 = 3) walks 3, 4, 5, 6, 0 and stops at the empty slot 1: 6 probes. Tombstones keep searches correct but lengthen unsuccessful ones until the table is rebuilt.

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

Exams that ask this topic