Data Structures Β· Visualization

Hash Table: Collision Handling (Chaining)

Hash Table

Separate chaining resolves hash collisions by storing a small linked list (or bucket) of entries at each array slot β€” when two keys hash to the same index, they simply live side by side in that slot's list. Time: Average O(1), worst case O(n) if all keys collide. Space: O(n).

Big-O: Average O(1) per operation; worst case O(n) on collisions.

Hash Table: Collision Handling (Chaining)

Hash Table

Separate chaining resolves hash collisions by storing a small linked list (or bucket) of entries at each array slot β€” when two keys hash to the same index, they simply live side by side in that slot's list.

How Collision Handling (Chaining) Works

  1. Compute the key's hash and reduce it modulo the table size to get a bucket index.
  2. If the bucket is empty, create a new list and insert the entry.
  3. If the bucket already has entries, append (or prepend) the new entry to that bucket's list.
  4. To look up a key, hash to the bucket, then linearly scan that bucket's list comparing keys.
  5. Resize (rehash all entries into a bigger table) when the average chain length grows too long.

Collision Handling (Chaining) Worked Example

Input: Insert keys 'cat' and 'act' which both hash to bucket 3 in a table of size 8

  1. hash('cat') % 8 = 3 β†’ bucket 3 is empty β†’ bucket3 = ['cat']
  2. hash('act') % 8 = 3 β†’ collision β†’ bucket3 = ['cat', 'act']
  3. Lookup 'act': go to bucket 3, scan list, compare 'cat' (no), 'act' (match)

Output: Both keys coexist in bucket 3's chain; lookup costs 2 comparisons worst case

Try your own input
Hash Table: Introduction
Runs each key through a hash function to compute a bucket index, so lookups don’t need to scan every entry.
0
βˆ…
1
βˆ…
2
βˆ…
3
βˆ…
4
βˆ…
5
βˆ…
6
βˆ…
Active Hash map Key
Hash Table: Introduction
Press β–Ά Run to watch it step by step.
Python Β· Hash Tables in Code
# Hash map: key -> value, O(1) average
scores = {}
scores["alice"] = 90     # insert
v = scores.get("bob", 0) # get with default
del scores["alice"]      # delete

if "bob" in scores:      # membership
    print("present")
# example: insert key=cat
Input: Insert keys 'cat' and 'act' which both hash to bucket 3 in a table of size 8

Behind the Scenes Execution

Press β–Ά Run in the visualizer β€” the call stack, registers and memory will replay here in sync with the code, line by line.

Collision Handling (Chaining) Time & Space Complexity

TimeAverage O(1), worst case O(n) if all keys collide
SpaceO(n)

When to Use Collision Handling (Chaining)

  • General-purpose hash map implementations (Java HashMap buckets)
  • Symbol tables in compilers
  • Caching layers

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired β€” free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action β€” visualize the algorithms that operate on these structures, or take a guided course.