DrawCode Algorithm Visualizer • Hashing • Easy

Separate Chaining

Tags: Hash Table, Collision, Chaining, Linked List

1. One-Liner

Separate chaining stores collisions in per-bucket chains (lists) so many keys can share a slot without probing the whole table.


2. The Problem It Solves

Open addressing struggles with clustering and delete semantics; chaining keeps insert/delete local to a short list while the table load factor can exceed 1 if lists grow.


3. The Core Idea

Hash to an index; if busy, append to the bucket’s list. Lookup scans only that list—average O(1) if load is bounded and hash spreads keys.


4. How It Works (Step-by-Step)

StepWhat Happens
1. Hashi = h(key) % m
2. InsertPush or update node in bucket i’s list
3. SearchWalk the list at i comparing keys
4. DeleteUnlink node from the list

5. Dry Run Example

m=4, chain insert 5,9,13 (all mod 4 → 1): bucket 1 becomes list 5→9→13; search 9 scans at most three nodes.


6. Key Properties

PropertyValue
Load factorCan be >1 (lists grow)
ClusteringNone in the primary table
CacheLists may hurt locality vs open addressing

7. Where It Is Used

DomainUse
JavaHashMap (bins as trees after threshold)
DatabasesSymbol tables, join hash indexes

8. Interview Tips

Contrast chaining vs open addressing; mention rehash when average chain length grows; Java 8+ treeify on long buckets.


9. Comparison with Other Algorithms

SchemeCollision handling
ChainingLists at slot
Open addressingNext empty slot
CuckooEvict to alternate table

10. Complexity

MetricValue
Average search/insertO(1) with good hash & load
WorstO(n) if all keys collide

Implementation Example (PYTHON)

class HashChain:
    def __init__(self, m=16):
        self.m = m
        self.b = [[] for _ in range(m)]
    def _h(self, k): return hash(k) % self.m
    def put(self, k, v):
        i = self._h(k)
        for j, (kk, _) in enumerate(self.b[i]):
            if kk == k: self.b[i][j] = (k, v); return
        self.b[i].append((k, v))
    def get(self, k):
        i = self._h(k)
        for kk, v in self.b[i]:
            if kk == k: return v
        return None

Interactive Visualizer Workspace

Explore step-by-step interactive animations, memory state tracking, and live multi-language execution in DrawCode.

Launch Interactive Visualizer