Hash Table Internals - Part 6 - Double Hashing

Hash Table Internals - Part 6 - Double Hashing

Linear and Quadratic probing do a great job at handling collisions in a Hash Table, but they both suffer from Clustered Collision, which degrades performance. So, can we do better?

Double hashing is a technique that minimizes the problem of clustered collisions by using a secondary hash function to find the next available slot.

Double Hashing

Double hashing is an Open Addressing technique to address collisions in a hash table; hence, instead of using an auxiliary data structure to hold the collided keys, it leverages the already available free slots.

The probing function for Double Hashing is defined as

p(k, i) = (h1(k) + i * h2(k)) mod m

Thus, with every attempt we make to find an empty slot, we can leap by any distance in any direction, making all slots equally likely. A sample sequence for a particular key k1 could be

  • primary slot: h1(k1), if that is occupied then
  • attempt 1: h1(k1) + h2(k1), if that is occupied then
  • attempt 2: h1(k1) + 2 * h2(k1), if that is occupied then
  • attempt 3: h1(k1) + 3 * h2(k1), and so on

Linear probing and quadratic traversals take a predictable leap to hunt for an empty slot, while double hashing probing leaps depend on the key and hence reduce the chances of clustering. So, different keys will have different leaps.

Choosing a second hash function

The second hash function is super-critical, as it is aimed at resolving collisions effectively while ensuring minimal clustering. The second hash function should

  1. never return 0
  2. cycle through the entire table (with no particular order)
  3. be fast to compute and almost feel like a random number generator

Advantages of Double Hashing

  1. Uniform spread upon collision
  2. follows no specific offset pattern, the key purely depends on
  3. least prone to the clustering problem

Disadvantage of Double Hashing

Double hashing is not cache-friendly, as it requires us to hop across the hash table to hunt an empty slot. We may be at one extreme of the table and then move to the other one.

Here's the video of my explaining this in-depth ?? do check it out

Thank you so much for reading ?? If you found this helpful, do spread the word about it on social media; it would mean the world to me.

If you liked this short essay, you might also like my courses on

No alt text provided for this image

I teach an interactive course on System Design where you'll learn how to intuitively design scalable systems. The course will help you

  • become a better engineer
  • ace your technical discussions
  • get you acquainted with a spectrum of topics ranging from Storage Engines, High-throughput systems, to super-clever algorithms behind them.

I have compressed my ~10 years of work experience into this course, and aim to accelerate your engineering growth 100x. To date, the course is trusted by 800+ engineers from 11 different countries and here you can find what they say about the course.

Together, we will dissect and build some amazing systems and understand the intricate details. You can find the week-by-week curriculum and topics, testimonials, and other information at https://arpitbhayani.me/masterclass.

要查看或添加评论,请登录

Arpit Bhayani的更多文章

  • How to Find and Ride the Next Tech Wave

    How to Find and Ride the Next Tech Wave

    This edition of the newsletter contains one quick write-up that will help you grow faster in your career a video I…

    6 条评论
  • Engineer or Manager? How to Decide Your Path

    Engineer or Manager? How to Decide Your Path

    This edition of the newsletter contains one quick write-up that will help you grow faster in your career a video I…

    5 条评论
  • One Career Bet Worth Taking

    One Career Bet Worth Taking

    This edition of the newsletter contains one quick write-up that will help you grow faster in your career a video I…

    5 条评论
  • Leave your job with grace and gratitude

    Leave your job with grace and gratitude

    This edition of the newsletter contains one quick write-up that will help you grow faster in your career a video I…

    7 条评论
  • Turn Boring Projects into Opportunities

    Turn Boring Projects into Opportunities

    This edition of the newsletter contains one quick write-up that will help you grow faster in your career a video I…

    1 条评论
  • When is the right time to switch?

    When is the right time to switch?

    This edition of the newsletter contains one quick write-up that will help you grow faster in your career a video I…

    8 条评论
  • Ramping up faster in your new job

    Ramping up faster in your new job

    This edition of the newsletter contains one quick write-up that will help you grow faster in your career a video I…

    4 条评论
  • Back Your Disagreement with Data

    Back Your Disagreement with Data

    This edition of the newsletter contains one quick write-up that will help you grow faster in your career a video I…

    2 条评论
  • Doubt yourself every day

    Doubt yourself every day

    This edition of the newsletter contains one quick write-up that will help you grow faster in your career a video I…

    9 条评论
  • Not everything needs to be dumbed down

    Not everything needs to be dumbed down

    This edition of the newsletter contains one quick write-up that will help you grow faster in your career a video I…

    11 条评论

社区洞察

其他会员也浏览了