Hash Table Internals - Part 8 - Why they are doubled upon resize? (2024)

To maintain consistent performance, the hash table has to be resized - be it growing or shrinking. The trigger to resize depends on the load factor, which is defined as the ratio of the number of keys that the hash table holds to the total number of slots.

When should we resize?

The Hash Table is resized when the load factor hits a certain threshold. If we get too aggressive or too lenient, we would not be able to get the optimal efficiency. Hence, we have to find a sweet spot.

We typically grow the hash table when the load factor hits 0.5 and shrink when we hit 0.125.

Why do we always double?

We have heard and seen so many times, that when a Hash Table is required to grow, we always double the underlying array; but why? Can we not just increase it by 1 every time we are trying to insert?

Resizing by 1 every time

Let's take an example: say, we grow the array by 1 every time we insert an element in the Hash Table. Let's compute the time it requires to fill n elements.

Inserting the 1st element is: allocating an array of size 1, and inserting 1 element; so in all O(1) operations.

Inserting the 2nd element is: allocating an array of size 2, copying 1 element from the old array, and then inserting the 2nd element; so in all O(2) operations.

Hence, inserting the nth element is: allocating an array of size n, copying n-1 elements from the old array, and then inserting the nth element; so in all O(n) operations.

Total operations to insert n elements = 1 + 2 + ... + n = (n(n-1))/2 which is O(n^2).

Doubling every time

If we double every time, inserting n elements requires O(n) time, as it is spacing out an expensive resize operation. We would be inserting n/2 elements before resizing the array to 2n.

Note: For a detailed amortized analysis, please refer to the video attached here, where I have explained the reasoning in depth.

Why is a hash table array always a power of 2?

For a power of 2, the MOD and the bitwise AND spit out the same result and given that the bitwise AND is a magnitude faster than the MOD, we get the best performance out of our Hash Tables when we use AND

Recommended by LinkedIn

The Type-Traits Library: Type Checks Rainer Grimm 2 years ago
C# How to create a File System Watcher David N. 6 years ago
Spark Internals: Dynamic Partition Pruning Valdas Gylys 1 year ago

(i % 2^k) == (i & (2^k) - 1)

This is why the length of the underlying array is always a power of 2, making our most-frequent operation efficient.

Shrinking the Hash Table

To ensure we are not wasting space, we trigger the shrink when we do not utilize the underlying array enough. While triggering a shrink, we also need to ensure that we are not aggressive enough that we have to grow immediately after the shrink.

Hence, we shrink the hash table when the load factor hits 1/8 i.e. in a table of length 64 if we are only holding 8 keys, we trigger a shrink and that reduces the length to 32.

Note: To understand why we do it at load factor = 1/8, please refer to the video.

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

Hash Table Internals - Part 8 - Why they are doubled upon resize? (4)

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.

Hash Table Internals - Part 8 - Why they are doubled upon resize? (2024)
Top Articles
What Are Your Strengths? |15 GREATEST Strengths With Sample Answers
Set the default storage space for OneDrive users - SharePoint in Microsoft 365
Foxy Roxxie Coomer
Diario Las Americas Rentas Hialeah
123 Movies Black Adam
Garrison Blacksmith Bench
Best Transmission Service Margate
Umn Pay Calendar
Craigslist Phoenix Cars By Owner Only
Rls Elizabeth Nj
Ohiohealth Esource Employee Login
Camstreams Download
Synq3 Reviews
Nj Scratch Off Remaining Prizes
Pac Man Deviantart
Dirt Removal in Burnet, TX ~ Instant Upfront Pricing
Inter-Tech IM-2 Expander/SAMA IM01 Pro
Hollywood Bowl Section H
Sni 35 Wiring Diagram
We Discovered the Best Snow Cone Makers for Carnival-Worthy Desserts
Fsga Golf
Mc Donald's Bruck - Fast-Food-Restaurant
The EyeDoctors Optometrists, 1835 NW Topeka Blvd, Topeka, KS 66608, US - MapQuest
Purdue 247 Football
Boston Dynamics’ new humanoid moves like no robot you’ve ever seen
Rubber Ducks Akron Score
Cain Toyota Vehicles
Crossword Help - Find Missing Letters & Solve Clues
Silky Jet Water Flosser
Jcp Meevo Com
15 Primewire Alternatives for Viewing Free Streams (2024)
Rek Funerals
The Fabelmans Showtimes Near Baton Rouge
Weather Underground Durham
CohhCarnage - Twitch Streamer Profile & Bio - TopTwitchStreamers
Worthington Industries Red Jacket
Uno Fall 2023 Calendar
Davita Salary
Ravens 24X7 Forum
A Grade Ahead Reviews the Book vs. The Movie: Cloudy with a Chance of Meatballs - A Grade Ahead Blog
Jambus - Definition, Beispiele, Merkmale, Wirkung
Nsu Occupational Therapy Prerequisites
Robot or human?
Craigslist Hamilton Al
Acadis Portal Missouri
Me Tv Quizzes
Ursula Creed Datasheet
Alston – Travel guide at Wikivoyage
Yale College Confidential 2027
Windy Bee Favor
Secondary Math 2 Module 3 Answers
Fetllife Com
Latest Posts
Article information

Author: Ray Christiansen

Last Updated:

Views: 6486

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Ray Christiansen

Birthday: 1998-05-04

Address: Apt. 814 34339 Sauer Islands, Hirtheville, GA 02446-8771

Phone: +337636892828

Job: Lead Hospitality Designer

Hobby: Urban exploration, Tai chi, Lockpicking, Fashion, Gunsmithing, Pottery, Geocaching

Introduction: My name is Ray Christiansen, I am a fair, good, cute, gentle, vast, glamorous, excited person who loves writing and wants to share my knowledge and understanding with you.