Quadratic Probing for Conflict Resolution in Hash Tables



256 views Hash Table Internals



Linear probing is a popular way to handle Hash Table collisions, but is that the only way? Definitely not.

Challenges with linear Probing

Linear Probing suffers from one significant challenge, and that is clustered collision. With a poor hash function, it is very much possible that some slots are preferred more over others, and upon collision, they are placed next to each other.

Hence, we would see clusters of collided keys forming across the entire hash table, and this is called Clustered Collision. Linear Probing suffers from this the most.

Quadratic Probing

Quadratic Probing determines the next slots as per an arbitrary probing function

p(k, i) = h(k) + c1 * i + c2 * i^2

For attempt 0, we get the primary slot h(k) and post that for each attempt we move quadratically through the hash table to hunt for the next available slot.

Because of the quadratic jump, we do not form clustered collisions, instead, we cover a good chunk of the hash table. A simple quadratic sequence would be

h(k) + 1
h(k) + 4
h(k) + 9
h(k) + 16
.
.

Where h(k) is the primary slot we got by passing the key through the hash function and 1, 4, 9, and 16 are the quadratic offsets.

Properties of Quadratic Probing

Reducing clustered collisions

Quadratic Probing reduces the clustered collisions by distributing collided slots quadratically across the hash table and utilizing the entire hash table space.

Good Locality of Reference

Quadratic Probing has a good locality of reference. When we access a particular slot of the hash table, we are also bringing in neighboring slots to the CPU cache. Upon collisions, when we access a few next quadratic slots, we need not fetch it from the RAM.

The locality of reference is not as high as Linear Probing, but it is decent enough when we observe fewer collisions per slot.


Arpit Bhayani

Arpit's Newsletter

CS newsletter for the curious engineers

❤️ by 17000+ 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


Implementing Hash Maps with Hash Tables

425 views 15 likes 2022-08-03

Maps and Dictionaries are amazing, but how are they implemented? In this 11th video of this Hash Table Internals series...

Implementing Hash Sets with Hash Tables

259 views 6 likes 2022-08-01

Sets are amazing, but how are they implemented? In this 10th video of this Hash Table Internals series, we take an in-d...

Implementing Resize of a Hash Table

257 views 9 likes 2022-07-29

So, the Hash Table needs to be resized in order to maintain consistent performance, but how exactly? In this 9th Video ...

Why are Hash Tables always doubled?

770 views 19 likes 2022-07-27

Why are the underlying arrays of the hash tables always a power of 2? When we trigger a resize why are Hash Tables alway...


Be a better engineer

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


System Design Masterclass

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

Enrolled by 700+ learners

Details →

Designing Microservices

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

Enrolled by 17+ learners

Details →

GitHub Outage Dissections

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

Enrolled by 67+ learners

Details →

Hash Table Internals

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

Enrolled by 25+ learners

Details →

BitTorrent Internals

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

Enrolled by 42+ learners

Details →

Topics I talk about

Being a passionate engineer, I love to talk about a wide range of topics, but these are my personal favourites.




Arpit's Newsletter read by 17000+ engineers

🔥 Thrice a week, in your inbox, an essay about system design, distributed systems, microservices, programming languages internals, or a deep dive on some super-clever algorithm, or just a few tips on building highly scalable distributed systems.



  • v12.4.4
  • © Arpit Bhayani, 2022

Powered by this tech stack.