A missing key needs a trail.
Keep a probing chain searchable after a deletion.
01 Model the idea
Open addressing stores entries directly in a table and probes alternative slots after collisions. An unused empty slot can terminate an unsuccessful search. A deleted slot cannot do that, because another key might have been displaced beyond it. A tombstone marks a deleted position while preserving the search trail.
A deleted slot never prematurely terminates a valid probe chain.
02 State the strategy
Read the steps independently of syntax. Explain what each step preserves before translating it into code.
PROBE from the hashed position
STOP only at a never-used empty slot or a matching key
SKIP deleted markers03 Make it concrete
The implementation uses Python 3.11+ with only the standard library. Run it locally, or download the independent DSA workbench.
EMPTY, DELETED = object(), object()
table = [EMPTY] * 5
def put(key):
first_deleted = None
for offset in range(len(table)):
index = (key % len(table) + offset) % len(table)
value = table[index]
if value is EMPTY:
table[first_deleted if first_deleted is not None else index] = key
return
if value is DELETED:
if first_deleted is None: first_deleted = index
elif value == key: return
if first_deleted is not None:
table[first_deleted] = key; return
raise OverflowError("Table full")
def contains(key):
for offset in range(len(table)):
value = table[(key % len(table) + offset) % len(table)]
if value is EMPTY: return False
if value is not DELETED and value == key: return True
return False
put(1); put(6)
table[1] = DELETED
print(contains(6), contains(9))True False
- 1
Keys 1 and 6 start at the same slot. Linear probing places the second key in a later slot.
- 2
The deleted marker at index 1 does not stop the search for 6. A truly unused EMPTY slot can prove absence along this insertion policy.
- 3
Insertion remembers the first tombstone but continues searching for an existing equal key. Only after absence is established can that deleted position be reused. A full production set also controls load and rebuilds when tombstones accumulate.
04 Count the cost
Expected O(1) at controlled load; O(n) worst case
O(m) table slots
Bounds refer to the shown implementation and exclude printed output unless stated. n is input size; V and E are graph vertices and edges.
05 Your next move
Explain and demonstrate why replacing the deleted slot with EMPTY would hide the colliding key.
Reveal one possible solution +
EMPTY, DELETED = object(), object()
table = [EMPTY, DELETED, 6, EMPTY, EMPTY]
def contains(key):
for offset in range(len(table)):
value = table[(key % len(table) + offset) % len(table)]
if value is EMPTY: return False
if value is not DELETED and value == key: return True
return False
print(contains(6))
table[1] = EMPTY
print(contains(6))06 Check your reasoning
Why use a tombstone?
Understand it. Then own it.
Can you explain the invariant, trace an edge case, and defend the cost model? Log the experiment when the reasoning makes sense.