Skip to lesson

Open addressing

Open addressing stores all entries directly within the backing array. When a collision occurs, it probes subsequent slots until an open position is found.

Linear probing: finding the next free slot

Open addressing handles collisions by placing the entry in another free slot of the same array, found by probing the next slots.

With open addressing every entry lives in the array itself. When a slot is taken, linear probing tries the next slot, then the next, until one is free. The program below does this.

  1. Hash the key to determine initial bucket index h.
  2. Check slot h: if empty or matching key, finish probe.
  3. If occupied by a different key, advance to (h + 1) % capacity.
  4. Repeat until finding the key, an empty slot, or probing entire table.

Linear probing: insert 12, 17, 22 into 5 slots

Step 1 of 8

Array: -, -, -, -, -.
capacity
5
A table with 5 slots, all empty. We insert 12, 17 and 22. The first slot to try is key % 5.

Example 1 · Linear Probing Map Implementation

LinearProbingDemo.javaJava
public class LinearProbingDemo {    private String[] keys;    private int[] values;    private int capacity;
    public LinearProbingDemo(int cap) {        capacity = cap;        keys = new String[cap];        values = new int[cap];    }
    public void put(String key, int val) {        int idx = (key.hashCode() % capacity + capacity) % capacity;        while (keys[idx] != null && !keys[idx].equals(key)) {            idx = (idx + 1) % capacity;        }        keys[idx] = key;        values[idx] = val;    }
    public Integer get(String key) {        int idx = (key.hashCode() % capacity + capacity) % capacity;        int start = idx;        while (keys[idx] != null) {            if (keys[idx].equals(key)) return values[idx];            idx = (idx + 1) % capacity;            if (idx == start) break;        }        return null;    }
    public static void main(String[] args) {        LinearProbingDemo map = new LinearProbingDemo(5);        map.put("a", 10);        map.put("f", 20); // Collides with 'a' if capacity is 5        System.out.println("a: " + map.get("a"));        System.out.println("f: " + map.get("f"));        System.out.println("z: " + map.get("z"));    }}
Output
a: 10
f: 20
z: null

What this code does

  • Keys 'a' and 'f' map to bucket indices and probe sequentially.
  • get follows the same linear probing path to retrieve values.
  • Unmapped key 'z' encounters null and returns null immediately.

Open addressing avoids linked lists by keeping all entries directly in the table array. This eliminates pointer overhead and maximizes CPU cache locality. When a collision happens at index h, linear probing checks (h + 1) % capacity. It continues checking sequential slots until finding the target key or an empty slot. Lookup follows the identical probe sequence to locate previously inserted keys. Let table capacity be 5. Insert key 12 (12 % 5 = 2). Slot 2 is empty, so store 12 at index 2. Next, insert key 17 (17 % 5 = 2). Slot 2 is occupied, so probe slot 3 and place 17 there. Next, insert key 22 (22 % 5 = 2). Slots 2 and 3 are occupied, so probe slot 4 and store 22 there.

  • Linear probe sequence: h, h + 1, h + 2, ... modulo table capacity.
  • Cache friendly: contiguous array traversal delivers superior CPU cache locality.
  • Capacity constraint: total element count cannot exceed array capacity.

Deleting with tombstones

A tombstone is a marker left in a slot when an entry is deleted, so searches keep probing past it instead of stopping early.

Setting a deleted slot to null would break the probe path of keys stored after it. A tombstone marker keeps searches going. The trace below searches past a deleted slot.

Example 2 · Tombstone Deletion Trace

TombstoneTraceDemo.javaJava
public class TombstoneTraceDemo {    private static final String TOMBSTONE = "<DELETED>";
    public static void main(String[] args) {        String[] table = {"k1", "k2", "k3", null, null};        // Delete "k2" using tombstone        table[1] = TOMBSTONE;
        // Search for "k3" which collided and probed past index 1        int idx = 0; // Suppose initial hash was index 0        boolean found = false;        while (table[idx] != null) {            if (table[idx].equals("k3")) {                found = true;                break;            }            idx++;        }        System.out.println("Found k3: " + found);        System.out.println("Final index probed: " + idx);    }}
Output
Found k3: true
Final index probed: 2

What this code does

  • Tombstone at index 1 allows the while loop to continue probing.
  • If index 1 were set to null, the search would terminate prematurely.
  • The search successfully reaches index 2 and locates key 'k3'.

Simply setting a deleted slot to null breaks the probe search path. Consider our table where keys 12, 17, and 22 occupy slots 2, 3, and 4. If we delete 17 by setting slot 3 to null, searching for 22 breaks. The search hashes to 2, sees 12, probes 3, sees null, and wrongly reports missing! To preserve search paths, replace deleted entries with a sentinel tombstone marker. Searches continue past tombstones, but insertions can safely reuse tombstone slots.

  • Tombstone: a sentinel marker indicating a slot was previously occupied.
  • Search rule: continue probing when encountering a tombstone.
  • Insert rule: record first tombstone seen to reuse slot if key is absent.

Find the bug

Find the deletion bug

Tap the line that breaks the probe path.

Delete the key stored in slot 3.

Round 1 of 3

Primary clustering and probe sequences

Linear probing builds long runs of filled slots called clusters, which make probes longer. The program below counts probes as three keys with the same home slot are inserted.

Example 3 · Measuring Linear Probe Sequence Lengths

ProbeCountDemo.javaJava
public class ProbeCountDemo {    private String[] table = new String[5];
    public int insert(String key) {        int h = (key.hashCode() % table.length + table.length) % table.length;        int probes = 1;        while (table[h] != null) {            h = (h + 1) % table.length;            probes++;        }        table[h] = key;        return probes;    }
    public static void main(String[] args) {        ProbeCountDemo map = new ProbeCountDemo();        System.out.println("Item 1 probes: " + map.insert("a"));        System.out.println("Item 2 probes: " + map.insert("f"));        System.out.println("Item 3 probes: " + map.insert("k"));    }}
Output
Item 1 probes: 1
Item 2 probes: 2
Item 3 probes: 3

What this code does

  • First item finds slot empty immediately in 1 probe.
  • Colliding second item requires 2 probes to reach an empty slot.
  • Third colliding item requires 3 probes, demonstrating clustering effects.

Linear probing tends to form long contiguous blocks of occupied slots. This phenomenon is called primary clustering. Clusters grow wider as new collisions land near existing occupied blocks. Quadratic probing and double hashing use non-linear step sizes to reduce clustering.

  • Primary clustering: adjacent occupied slots grouping together into long chains.
  • Degraded search: long clusters increase average probe sequence lengths.
  • Alternative probing: quadratic probing steps i²; double hashing steps i * hash2.

Next step

Check your understanding

10 quick questions