Conflict Resolution in Hash Tables with Open Addressing



1291 views Hash Table Internals



Open Addressing is a common yet interesting way of handling collisions in Hash Tables and instead of using an auxiliary data structure, it leverages empty slots of the hash table to store the collided keys, making the entire approach space efficient.

While inserting a key in the hash table, we pass it through the hash function to get the slot, and if the slot is already occupied, we find the next available slot through probing.

Probing

Probing is the process through which we deterministically find the next available slot in the hash table. The approach and algorithm could vary, but formally it is a function of the key to be placed and the attempt, that spits out a slot.

j = p(k, i)

The probing function uses the key k and an attempt i to spit out an index j. The attempt i and the index j are in the range [0, m-1], where m is the size of the hash table.

Good probing function

A good probing function would generate a deterministic permutation of numbers [0, m - 1] specific to the key k, so that we eventually check and cover the entire hash table for an available slot.

For example: for some key k1, on a hash table of size 8, the probing function might generate a sequence: 5, 7, 2, 1, 0, 6, 4, and 3. This means we first try to put the key in index 5, if that is occupied we check 7, then 2, and so on.

Hash Table Operations

Adding a key

Until we find a free slot in the hash table, we keep invoking the probing function with attempts 0, 1, 2,, etc. Once we find an empty slot, we stop and place the key in that slot.

Key Lookups

For looking up a key, we invoke the probing function with attempt 0 and check the slot. If the slot holds the key we need, we stop and return the value. If not, we continue to probe with attempts 1, 2, etc., and continue to hunt.

We stop the iteration when

  • we find the key,
  • we stumble upon an empty slot during iteration
  • we have checked the complete hash table

Deleting a key

With open addressing, deleting a key from the table has to be a soft delete, because we need a way to differentiate between an empty slot and a deleted key.

If during deletion we empty the slot, then we would be unable to look and reach for a key that had a collision and was placed after the key we deleted.

Limitation of Open addressing

Since we are not having any auxiliary data structure, a major limitation of Open Addressing is that the maximum number of keys we can hold are the same as the number of slots in the hash table.


Arpit Bhayani

Arpit's Newsletter

CS newsletter for the curious engineers

❤️ by 38000+ readers

If you like what you read subscribe you can always subscribe to my newsletter and get the post delivered straight to your inbox. I write essays on various engineering topics and share it through my weekly newsletter.




Other essays that you might like



Be a better engineer

A set of courses designed to make you a better engineer and excel at your career; no-fluff, pure engineering.


Paid Courses

System Design for Beginners

A masterclass that helps early engineers and product managers become great at designing scalable systems.

300+ learners

Details →

System Design Masterclass

A masterclass that helps you become great at designing scalable, fault-tolerant, and highly available systems.

1000+ learners

Details →

Redis Internals

Learn internals of Redis by re-implementing some of the core features in Golang.

98+ learners

Details →

Free Courses

Designing Microservices

A free playlist to help you understand Microservices and their high-level patterns in depth.

823+ learners

Details →

GitHub Outage Dissections

A free playlist to help you learn core engineering from outages that happened at GitHub.

651+ learners

Details →

Hash Table Internals

A free playlist to help you understand the internal workings and construction of Hash Tables.

1027+ learners

Details →

BitTorrent Internals

A free playlist to help you understand the algorithms and strategies that power P2P networks and BitTorrent.

692+ learners

Details →