The Magic of Hash Tables, A Quick Deep Dive into O(1) (2024)

Ahh yes, the Hash Table, a Computer Science classic. Often our first thought as software developers when facing an unfamiliar problem is:

“hmmm, can I use a Hash Table to solve this problem?”

Which I mean, is the correct thought! Hash Tables not only make our logic simple, but they also are extremely quick with constant time accessing speeds!

The Magic of Hash Tables, A Quick Deep Dive into O(1) (3)

As I wrote the simple Map<String, Integer> my_map = new Map<String, Integer>(); I grew curious about how many lines of code were running underneath-the-hood to make this super convenient data structure work. Which…. led me into a climatic journey in discovering the in’s and out’s of our friend, the Hash Table. Here is a digest of the findings!

Ok ok, I’ll keep this part short and sweet I swear… I won’t doubt your intelligence. There are two parts:

  1. A Hash Table is simply a block of memory (an array) that contains ‘buckets’ which hold the data we want to map too. Phew, easy right? We call them ‘buckets’ instead of indexes because, in fact, in some implementations, the index contains a Linked List (for collisions).
  2. A Hash Table is also abstractly-addressable, meaning that instead of using an outright offset like: Data = (Memory Location of Array) + (array Offset)we substitute the offset for a user-defined variable and hash it into a unique*** integer.
  • **In a perfect world the value is unique
The Magic of Hash Tables, A Quick Deep Dive into O(1) (4)
The Magic of Hash Tables, A Quick Deep Dive into O(1) (5)

So you might ask yourself, okay Hashing algorithm, easy! The end goal is that we want to take the supplied key and convert it to an Integer. In our example:

Map<String, Integer> my_map = new Map<String, Integer>();

We would want to convert our key of type String into an Integer so that we can use it as a substitute for our Array_Offset value. That process it conducted by a hashing algorithm, which will yield a large number back, so we use modulo arithmetic with the current size of the array to prevent an ‘Array out of bounds’ error.

Neat right?! Okay, but now you might ask yourself:

“A Hash Table’s key can be any object! Not just a string, how do we convert any Object into an Integer Array Offset?”

The beauty of a Hash Table is that it can work with any object. How? Well, the only universal trait amongst various objects is that they are all originally made of bits! So with our hash function, we will use bit-wise operations to randomize the object before converting it to an Integer.

There is no ‘standard hashing function’ that is used throughout all OOP languages, but the C++ implementation of unordered_map uses an algorithm called ‘MurmurHash’. Below is a simple example of how a 32-bit object can be hashed into a 32-bit Integer.

The Magic of Hash Tables, A Quick Deep Dive into O(1) (6)

In the above example:

  1. We take a 32-bit Object, and spit it into 4 groups of 8-bit blocks.
  2. With each individual group, we perform an AND instruction with a random static variable Var1
  3. Bit shift the result to the right, or a ROL Instruction
The Magic of Hash Tables, A Quick Deep Dive into O(1) (7)

4. Repeat steps 2 and 3 with a different variable, Var2

5. Combine each individual 8-bit block back into a 32-bit Object

6. Read the result as an Integer.

After hashing the key into an indexable integer, we are ready to insert a value at that memory location (the corresponding bucket).

Remember, the hash table itself is a block of memory, so after a certain amount of inserts, the block of memory will have to recreate itself. This process is relatively expensive — as the data structure will have to allocate a new block of memory, rehash the keys, and copy the values into the new index.

What determines when a hash table is full? A ‘Load Factor Value’.

The Magic of Hash Tables, A Quick Deep Dive into O(1) (8)

Load Factor is defined as “The number of entries in the table (n)” Divided by “The Size of the Hash Table (k)”

There’s a check that happens before the insertion of a new element into a hash table. If the load factor is exceeded on the next insert, then the Hash Table will allocate a new block of memory, rehash the keys, and copy all the data back into the hash table.

We want to have some wiggle room in the hash table to avoid collisions, this usually means having the array size greater than the number of entries at all times.

The Magic of Hash Tables, A Quick Deep Dive into O(1) (9)

Tada! Magic right?

Overall, the purpose of this article was to explore how the insertion and search time complexity of a standard Hash Table is achieved in constant time. There are plenty of questions that were left unanswered, such as “how to handle collisions with a hash table optimally?” Haha… I’ll let stack overflow answer some of those unanswered questions you may have. I hope however that you can confidently see how constant time-complexity is achieved with hash tables. Below are five key takeaways:

  • Hashing functions use bit-wise operations on the Key Value
  • The C++ standard library Hashing function is called “Murmurhash
  • The hashing algorithms used for hash tables are not cryptographic algorithms and prioritize speed rather than security.
  • A hash table has to rehash it’s values once the load factor is met.
  • The standard hashing threshold is 3/4, so a hash table should usually have about 25% memory than storing all elements in an array.
The Magic of Hash Tables, A Quick Deep Dive into O(1) (2024)
Top Articles
6 Authentic Ways to Trace IP Address from Email:
Resetting a transition to retirement strategy
WALB Locker Room Report Week 5 2024
Cold Air Intake - High-flow, Roto-mold Tube - TOYOTA TACOMA V6-4.0
Team 1 Elite Club Invite
From Algeria to Uzbekistan-These Are the Top Baby Names Around the World
Craigslist Vermillion South Dakota
Optimal Perks Rs3
Costco in Hawthorne (14501 Hindry Ave)
Imbigswoo
[PDF] INFORMATION BROCHURE - Free Download PDF
The Blind Showtimes Near Showcase Cinemas Springdale
Ladyva Is She Married
ATV Blue Book - Values & Used Prices
Insidekp.kp.org Hrconnect
Love In The Air Ep 9 Eng Sub Dailymotion
Minecraft Jar Google Drive
Michael Shaara Books In Order - Books In Order
Tamilrockers Movies 2023 Download
SF bay area cars & trucks "chevrolet 50" - craigslist
Nordstrom Rack Glendale Photos
Ubg98.Github.io Unblocked
Hyvee Workday
Jenna Ortega’s Height, Age, Net Worth & Biography
Nz Herald Obituary Notices
Kabob-House-Spokane Photos
Craigslist List Albuquerque: Your Ultimate Guide to Buying, Selling, and Finding Everything - First Republic Craigslist
Relaxed Sneak Animations
Unreasonable Zen Riddle Crossword
3 Ways to Drive Employee Engagement with Recognition Programs | UKG
Kuttymovies. Com
Khatrimmaza
Puerto Rico Pictures and Facts
Myhrconnect Kp
Watchdocumentaries Gun Mayhem 2
Pickle Juiced 1234
Indiana Wesleyan Transcripts
Western Gold Gateway
How Much Is Mink V3
Reborn Rich Ep 12 Eng Sub
Scanning the Airwaves
Rochester Ny Missed Connections
301 Priest Dr, KILLEEN, TX 76541 - HAR.com
Alpha Labs Male Enhancement – Complete Reviews And Guide
Divinity: Original Sin II - How to Use the Conjurer Class
Mitchell Kronish Obituary
Satucket Lectionary
Portal Pacjenta LUX MED
Skyward Cahokia
Kjccc Sports
Wvu Workday
Epower Raley's
Latest Posts
Article information

Author: Patricia Veum II

Last Updated:

Views: 6157

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Patricia Veum II

Birthday: 1994-12-16

Address: 2064 Little Summit, Goldieton, MS 97651-0862

Phone: +6873952696715

Job: Principal Officer

Hobby: Rafting, Cabaret, Candle making, Jigsaw puzzles, Inline skating, Magic, Graffiti

Introduction: My name is Patricia Veum II, I am a vast, combative, smiling, famous, inexpensive, zealous, sparkling person who loves writing and wants to share my knowledge and understanding with you.