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))- A. [3, None, None, 10, 'X', 24, 5] (True, 3) (False, 2)
- B. [3, None, None, 10, 31, 24, 5] (False, 2) (False, 6)
- C. [3, 31, None, 10, 'X', 24, 5] (True, 3) (False, 3)
- 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.
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
- Largest rectangle under a histogram using a stack of (start index, height). What is printed? python def rect(h): st, best = [], 0 for i, x…
- Rain water trapped between bars, computed with two pointers. What is printed? python def water(h): l, r, lm, rm, w = 0, len(h) - 1, 0, 0…
- Next greater element in a CIRCULAR array (scan the array twice, pushing indices only in the first pass). What is printed? python def…
- Towers stand in a row. Two towers can see each other if every tower strictly between them is shorter than both. The function counts such…
- A ticket number may lose exactly k digits; the remaining digits must keep their order and form the smallest possible number (leading zeros…
- Count subarrays whose sum is divisible by 3 using prefix sums and a remainder map. The second version imitates C's % operator (sign…