3.3.10 · D1Hashing

Foundations — Applications — frequency counting, two-sum problem, caching (LRU)

1,697 words8 min readBack to topic

Before you can read the parent note on Applications, every symbol on that page must mean something concrete to you. Below we build them one at a time — each from the picture it stands for.


0. What is a "map" / "hash map" at all?

The picture: a wall of labelled drawers. The label on the drawer is the key; whatever is inside is the value.

Figure — Applications — frequency counting, two-sum problem, caching (LRU)

The picture: a little machine. You feed in the word "banana", out pops the number 3, so you open drawer 3 immediately. That "compute-then-jump" is why lookup does not depend on how many drawers exist. For the machine's inner workings see Hash Functions; for what happens when two keys land on the same drawer see Collision Resolution (Chaining vs Open Addressing).


1. The symbols , , ,

The parent note is soaked in these. They describe how work grows as the input grows — nothing more.

Here is just a name for how big the input is (how many items in the list).

Figure — Applications — frequency counting, two-sum problem, caching (LRU)

2. The summation symbol

The cost formulas use . If that squiggle scares you, here is all it means.

The picture: a tally. Loop the counter ; each time drop one unit of cost into a bucket. When the loop ends, read the bucket.


3. The choose symbol

Brute-force two-sum costs . What is that?

The picture: a handshake room. people, everyone shakes everyone else's hand once. Count the handshakes → that's .


4. Index notation nums[i], i ≠ j, and enumerate

Figure — Applications — frequency counting, two-sum problem, caching (LRU)

5. The complement

The picture: a seesaw that must balance at height target. If one side sits at x, the other side must sit at c = target − x. Two-sum's whole cleverness is: instead of searching for a matching pair, search for this single required number c in the map of things seen so far.


6. Sentinels, prev, next — the linked-list vocabulary

The LRU section needs a Doubly Linked List. Here is its alphabet.

The picture: a train. Two dummy locomotives (sentinels) bookend the carriages so you never fall off either end. To yank a carriage out you only need to re-link its two neighbours — that is the "pointer surgery" the parent shows.

Figure — Applications — frequency counting, two-sum problem, caching (LRU)

7. Putting the alphabet together

Hash function turns key into slot

Hash map key to value

Big-O growth shapes

Cost proofs O of n

Summation adds cost per item

n choose 2 counts pairs

Why brute force is O of n squared

Frequency counting

Two-sum with complement

Complement target minus x

Index and enumerate

LRU cache

Doubly linked list prev and next

Sentinels head and tail


Equipment checklist

Cover the right side; can you answer before revealing?

What does promise?
The effort does not grow with input size — flat, one drawer-open, on average.
What is a hash function's job?
Turn a key into a slot number so you jump straight to it instead of scanning.
What does evaluate to?
— one flat unit of work repeated times.
Why is equal to ?
It equals ; the term dominates for large .
What is the complement in two-sum?
, the partner value so that .
Why must in two-sum?
You need two different positions; one element can't pair with itself.
What extra info does a doubly linked list store vs a singly linked one?
Each node keeps prev (its predecessor), enabling unlink without searching.
What is a sentinel node for?
A dummy end-guard so real nodes always have neighbours on both sides — no empty-list special cases.
Which end of the LRU list is the eviction victim?
The node at tail.prev — the least-recently-used, just before the tail sentinel.