Skip to lesson content
navan✳THE BUILD LABDSA
0/66 complete
BUILD LAB / DSA/ Hashing and identity
EXPERIMENT 15 / 66

A missing key needs a trail.

Keep a probing chain searchable after a deletion.

BY NAVAN SAMI◷ 10 MIN READSTARTERSTRUCTURE

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.

THE THING THAT MUST STAY TRUE

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.

the_strategy.txt
PROBE from the hashed position
STOP only at a never-used empty slot or a matching key
SKIP deleted markers

03 Make it concrete

The implementation uses Python 3.11+ with only the standard library. Run it locally, or download the independent DSA workbench.

hash_sets.py
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))
↳ EXPECTED OUTPUT
True False
  1. 1

    Keys 1 and 6 start at the same slot. Linear probing places the second key in a later slot.

  2. 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. 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

TIME

Expected O(1) at controlled load; O(n) worst case

SPACE

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

A SMALL MISSION

Explain and demonstrate why replacing the deleted slot with EMPTY would hide the colliding key.

Reveal one possible solution +
one_possible_solution.py
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

QUICK SIGNAL CHECK

Why use a tombstone?

ANOTHER PATTERN IN YOUR TOOLKIT.

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.

Built with curiosity. By Navan Sami. ↗Patterns over panic. One step at a time.