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
How to Improve Your Credit Score Before a Divorce and Why It’s Important - Divorce Financial Solutions
Gwei to ETH - How to Calculate and Convert Gwei to Ether
Red wine, berries, dark chocolate and tea: A recipe to reduce dementia risk
MyChart - Baptist Health
فیلم پیشنهاد بی شرمانه دوبله فارسی نماشا بدون سانسور
Noaa 7 Day Tropical Outlook
Eternal Sunshine Of The Spotless Mind Parents Guide
Globle Answer March 1 2023
Academic calendar 2023 - 2024 - student.uva.nl
One Barred From Bars Daily Themed Crossword
Governor Brown Signs Legislation Supporting California Legislative Women's Caucus Priorities
Becu Turbotax Discount Code
[H] Wasteland 3, Cuphead, Resident Evil 7, Cloudpunk, Firewatch, Heavy Rain [W] Offers, Skins
Kiddle Encyclopedia
Stocktwits Cycc
Anjaam Pathiraa Tamil Dubbed Tamilyogi
Katherine Grant Wilkes County Ga
Varsity Competition Results 2022
Is Mulch Bad for Dogs? | Great Pet Care
103 Care manager assistant jobs in UK
How Old is Reyna: All Valorant Agent's Age, Name, & More - Unigamesity
Www Silverdaddies.com
Jps Occupational Health Clinic
Kuchnia chińska w Warszawie: 8 miejsc, które warto znać
Aultman.mysecurebill
Half Inning In Which The Home Team Bats Crossword
¿Cuándo se regalan flores amarillas y por qué se realiza este ritual en septiembre?
Move Relearner Infinite Fusion
Hartland Liquidation Oconomowoc
Paul Mccombs Nashville Tn
Ferguson Showroom West Chester Pa
Huff Lakjer Funeral Home
Joliet Herald News Obituary
Sra Memorialcare
Uncovering The Mystery Behind Crazyjamjam Fanfix Leaked
Carly Hart Playboy
The Cure Average Setlist
Publix Super Markets | LinkedIn
Bbwchan Blueberry
Best Online Bingo Sites - Play For Fun or Real Money
Cake Bfb Asset
Alcon National Driving Center Inc
24 Hours Body Massage Near Me
shemale philly | Discover
Dr Yoel Rojas Google Reviews
Take Me Home.org
Full Cast Of Red
phoenix for sale by owner "puppies" - craigslist
Cyberpunk 2077 Update 2.110 Patch Notes: Enhancements, Fixes, and Exciting Additions
8664602315
Jasgotgass2
Hillsborough County Florida Recorder Of Deeds
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.