3.3.2 · D1Hashing

Foundations — Hash table — structure, open addressing vs chaining

2,952 words13 min readBack to topic

Before you can read the parent note (Hash table — structure), you must be able to read every symbol it uses without pausing. This page builds each one from nothing, in the order they depend on each other.


0. The four arithmetic symbols we lean on

Because this page promises every symbol from zero, here are the plain arithmetic marks the parent note sprinkles everywhere. We use only these four.

Keep this box in mind — every formula below is built from only these four operations plus the modulo we define in §3.


1. The array — the thing underneath it all

Everything starts here, so we start here.

Figure — Hash table — structure, open addressing vs chaining
Figure 1 — A six-box array A B C D E F with indices 0..5 printed underneath. Box 3 is highlighted orange with an arrow reading "jump straight here, no walking past 0,1,2". Takeaway: knowing the index lets you land on a box directly.

We will call the number of boxes the capacity and give it the letter . So a valid index is any whole number from up to .

Recall Why do indices stop at

, not ? Because we start counting at . ::: An array of boxes has indices — that is exactly numbers, and box would be off the end.


2. Key and value — what we are storing

This is why the parent note links Sets and Maps / Dictionaries — a hash table is the machine that makes dictionaries fast. A set is just a map that stores keys with no values (it only remembers whether a key is present).


3. The counting letters: , , and slots

The parent note throws around , , "buckets", and "slots". Here they are, once and clearly.

Figure — Hash table — structure, open addressing vs chaining
Figure 2 — Eight boxes indexed 0..7; three are filled green ("key") and five are empty white. A blue arrow labels the whole row "m = 8 boxes exist (capacity)"; text below reads "n = 3 boxes filled right now → alpha = n/m = 3/8 = 0.375". Takeaway: counts boxes that exist, counts boxes filled now.


4. The modulo operation — the "wrap-around" tool

The parent writes index = hash(key) % m and h(k) = k mod m. Here we finally define what those marks mean. The symbol % in program code, the word mod in maths, and the notation all mean the same single operation — take the remainder. (So when the parent writes hash(key) % m, read it as "hash(key) modulo ".) This is the most important operator on the page, so we build it from zero.

WHY this exact tool and not division? We have a possibly gigantic number (a hash code, maybe billions) but only boxes. We need to fold that huge number down into a valid index — and modulo is precisely the operation whose output is guaranteed to be a legal index. Ordinary division would give a fraction; subtraction would overflow the range. Only "the remainder" is trapped inside by definition. That is why it's the compression step.

Figure — Hash table — structure, open addressing vs chaining
Figure 3 — Five slots 0,1,2,3,4 arranged around a clock face. An orange hand points at slot 2, captioned "12 mod 5 = 2 (wrap around like a clock)"; below in red "22 mod 5 = 2 too → collision!". Takeaway: modulo maps any number onto the dial , and different inputs can point to the same slot.

Notice three different keys () all produced index . That is the whole reason collision resolution exists — and you just derived it with one operator.

Recall What is

? And ? Both are . ::: and — any exact multiple of , including itself, has remainder .


5. The hash function — the address calculator

Now that we have "key", "index", and "mod", we can finally read the star of the show.

The notation (which the parent writes) is read: " is a rule sending each key to one number in the set up to ." The arrow just means "produces". The curly braces mean "the collection of these numbers".


6. The sum symbol and the base- idea

The parent's string hash uses . That wall of symbols scares people off. Disarm it piece by piece — and note it uses only the (multiply) and the power from §0.

Here is the length of the string (how many characters), is the -th character's number (its ASCII code), and is the base raised to a power (repeated multiplication, from §0) — read Polynomial rolling hash & Rabin–Karp for why the powers descend as grows. The one thing you need here is: this formula treats the string as a number written in base , so that letter order changes the result. A plain sum would ignore order and make "ab" and "ba" collide.


7. The load factor — "how full"

Recall If

boxes hold keys, what is ? . ::: Six of the eight boxes are occupied — 75% full.


8. Collision — the crack in the dream

You already saw one in §4 (keys 12, 22, 17 all mapped to index 2). Now name it.


Prerequisite map

Array with indices 0..m-1

Instant jump by index

Key and value pairs

Hash function h

Modulo mod - wrap into range

Sum sigma and base p

index in 0..m-1

Collision - two keys same box

Chaining with linked lists

Open addressing probing

Load factor alpha = n over m

Rehash decision


Equipment checklist

Read each question, answer in your head, then reveal.

What do , , , and each mean?
Multiply (dot and cross are the same), divide, and raise to a power (repeated multiplication).
What is and what is ?
; .
What does mean and why is reaching it fast?
The value in box number 3; fast because the computer computes its memory address directly (random access), no walking.
What is the difference between and ?
= capacity (how many boxes exist); = how many keys are currently stored.
What are the two words the parent uses for one array box?
Bucket and slot — they mean the same thing.
What does a % m in code and in maths compute, and what range do we force it into?
The remainder of ; we force it into (non-negative).
What is under our convention, and why the care?
; because some languages return , which is not a legal index, so we add to fix it.
Why is mod the right tool for turning a huge hash code into an index?
Its output is guaranteed to be a legal index ; it folds any big number into the box range.
What is ?
.
Read aloud and say what it produces.
"h of k"; it produces an index in for the key .
What does the symbol tell you to do?
Add up the expression for .
What is in the string hash and how do you choose it?
The base; a small prime a bit larger than the alphabet size (the parent uses ), chosen to spread results and be cheap to multiply.
Why does the string hash use base instead of a plain character sum?
So letter order matters — a plain sum makes anagrams like "ab" and "ba" collide.
Write the load-factor formula and say what each letter is.
; stored keys, capacity.
Define a collision in symbols.
with — two different keys land in the same box.
Why are collisions unavoidable?
Pigeonhole — more possible keys than boxes, so two keys must share one.