COMPUTER SCIENCE & TECHNOLOGY • COMPUTER SCIENCE
Hash Tables & Collision Resolution
Insert the same key sequence with chaining, linear probing or quadratic probing and compare collisions, probe paths and table occupancy.
Collisions are part of hashing
Different keys can share a home index. Collision strategies decide where those keys go next, changing clustering, probe counts and the structure of the table.
LEARN WITH THE SIMULATION
Understand Hash Tables & Collision Resolution by experimenting.
A hash table trades ordered structure for fast direct indexing: a hash function maps each key to a home bucket, and a collision-resolution policy decides what happens when multiple keys want the same location. This laboratory inserts the same key sequence with chaining, linear probing or quadratic probing while exposing home bucket, actual placement, collisions, total probes and load factor. The premium challenge turns those metrics into a cost question rather than a placement animation.
BUILD THE IDEA
What you should understand first
- Map an integer key to a home bucket with a modular hash function.
- Distinguish a hash collision from the extra probes required to resolve it.
- Compare separate chaining with linear and quadratic open addressing on the same key sequence.
- Explain why load factor and key order influence probe cost even when the hash function is unchanged.
TRY IT YOURSELF
Why can two collision strategies store the same keys but require very different numbers of probes?
- Use one table size and key sequence with linear probing, then step through every insertion.
- Record which keys collide, where they are finally placed and the total probe count.
- Reset with quadratic probing while keeping the table and keys identical.
- Compare placement clusters and total probes, then repeat with a larger table size.
The home bucket is fixed by the hash function, but collision resolution changes which later buckets are inspected. Linear probing tends to create contiguous primary clusters, while quadratic probing spreads subsequent probes differently. More free space often reduces pressure, but exact cost still depends on the key sequence.
WHY IT WORKS 01
Home bucket and final placement are different concepts
The hash function determines a key's home bucket before the collision strategy is considered. If that bucket is already occupied, open addressing searches additional slots while chaining keeps multiple entries associated with the same bucket. Separating home from placement helps explain why changing the collision strategy changes probe cost without changing the hash value itself.
WHY IT WORKS 02
Probe count is a more useful cost signal than collision count alone
A collision tells that the first desired location was unavailable, but one collision can trigger several additional slot inspections. Linear probing may walk through a long occupied cluster, while quadratic probing follows a widening offset pattern. Total probes therefore captures more of the actual insertion work than simply counting how many keys initially collided.
WHY IT WORKS 03
Load factor changes how much room a strategy has to maneuver
For open addressing, load factor is stored entries divided by available slots. As the table fills, the chance of encountering occupied candidate slots rises and probe sequences can lengthen sharply. Chaining can conceptually exceed a load factor of one because multiple keys can share a bucket, but longer chains still increase lookup cost in a fuller implementation.
DO NOT MIX THESE UP
Common misconception
A hash function does not guarantee unique indices, and a collision is not itself a failure. Chaining and open addressing resolve collisions using different storage rules, so their load-factor constraints differ. A low load factor also does not guarantee zero collisions because distinct keys can still share the same home bucket.
PRACTICE CHALLENGE
Can you keep the same key sequence while meeting a probe-count target by changing only table size or collision strategy?
- Choose a target maximum probe count.
- Run the full insertion sequence with the current strategy and record total probes.
- Change only the collision strategy or table size, keeping keys in the same order.
- Explain why the winning configuration changed probe cost using home buckets, clustering and load factor.
Reveal the reasoning
Probe cost falls when the collision strategy reaches free positions with fewer inspections. Increasing table size usually lowers occupancy pressure, while changing from linear to quadratic probing changes the sequence of candidate slots after a collision. The same hash values can therefore lead to different total probe counts even though every key begins from the same home bucket.
MODEL BOUNDARIES
What this simulation assumes
Every simulation simplifies reality or a mathematical system. These are the assumptions and limits to keep in mind while interpreting the results.
Model assumptions
- Keys are integers and the home bucket is the nonnegative key modulo table size.
- Separate chaining stores colliding keys in insertion order within each bucket.
- Open addressing uses either h+i or h+i² probing and starts from an empty fixed-size table.
Model limitations
- Deletion, resizing, tombstones, duplicate-key replacement and production hash mixing are omitted.
- Quadratic probing may fail to visit every slot for some table sizes even when unused slots remain.
CONNECT IT
Where this matters
- Dictionaries and maps
- Database indexing concepts
- Caches and symbol tables
- Understanding collision-resolution tradeoffs
WORDS THAT MATTER
Key vocabulary
- Hash function
- Function mapping a key to a fixed-range hash value or home bucket.
- Collision
- Event where two different keys map to the same home bucket.
- Probe
- Inspection of a candidate bucket or slot during lookup or insertion.
- Load factor
- Ratio of stored entries to table slots, especially important for open addressing.
- Primary clustering
- Growth of contiguous occupied regions that can make linear probing increasingly expensive.
NEXT STEP