Unit 5 · Lesson 418 minAcademic review pending

When a room is full, walk down the hall

Open addressing allows no extra list — every slot holds at most one key, and a collision means probing forward until an empty slot appears.

Choose explanation

After this lesson

You should be able to

  • Insert into a hash table using linear probing.
  • Search using the same probe sequence insertion used.
  • Explain why an empty slot, not a full table, ends a search.

Try it before you read

Try it live

hash.ctable size 11
0
1
2
3
4
5
6
7
8
9
10
01

No extra room — take the next one down the hall

A hostel with strictly single-occupancy rooms cannot add a cot when a room is taken. Instead, the warden's rule is: try your assigned room; if it is occupied, check the next room down the hall; keep walking until an empty one turns up. Open addressing does exactly this — every slot in the table holds at most one key, and a collision is resolved by probing forward through the table itself, not by growing a list.

Probe forward, wrapping with the same modulo arithmetic as Unit I's circular queue
void insert(int table[], int key)
{
    int index = key % TABLE_SIZE;

    while (table[index] != EMPTY) {       /* linear probing */
        index = (index + 1) % TABLE_SIZE; /* Unit I's circular queue math */
    }
    table[index] = key;
}
02

Searching must retrace the same steps

Because a key may not sit at its computed slot at all, searching cannot simply check one slot and give up — it must probe the exact same sequence insertion would have used, checking each slot until the key is found or, critically, until an empty slot appears.

An empty slot is the honest end of the trail: if the key you want had ever been inserted, its probe sequence would have stopped at that same empty slot rather than continuing past it — so finding an empty slot proves the key was never placed. Stopping only when the whole table is full, instead, would force every failed search to check every single slot, which defeats the entire purpose of hashing.

Stop on a match, or on the empty slot that proves it was never there
int search(int table[], int key)
{
    int index = key % TABLE_SIZE;
    int start = index;

    while (table[index] != EMPTY) {
        if (table[index] == key) return index;   /* found */
        index = (index + 1) % TABLE_SIZE;
        if (index == start) break;                /* full loop, not found */
    }
    return NOT_FOUND;   /* hit EMPTY before finding it — never inserted */
}

Try it yourself

Using the widget below in open-addressing mode, insert 5, 16, and 27 in that order — the same keys that all collided in the chaining lesson — then search for 27 and describe its probe path.

Need a hint?

5 lands directly. 16 must probe past 5. 27 must probe past both.

Check the worked solution

5 hashes to slot 5 and lands there directly. 16 also hashes to 5, finds it occupied, and probes to slot 6. 27 also hashes to 5, finds 5 occupied, probes to 6, finds that occupied too, and lands at slot 7. Searching for 27 must therefore check slots 5, 6, and 7 in that exact order — the same three-step path insertion took, which is precisely why search must retrace insertion's steps rather than jumping straight to slot 7.

Quick check

Why must a search under open addressing stop at the first empty slot it meets, rather than continuing to search the whole table?

Select an answer to check your thinking.

Why this lesson exists

Syllabus mapping

Collision Resolution by Open Addressing

Maps to course outcomes CO1, CO2, CO4.