Browse curriculum

Perfect Hashing

Perfect hashing is a hashing technique that guarantees O(1) worst-case lookup time for a static set of keys by using a two-level hash table structure.

7 KEYS · 7 BUCKETS

LEVEL ONE

0
1
10
2
22
3
37
40
4
52
60
5
70
6

 

Level one: 7 keys into 7 buckets. Collisions are expected and allowed here — the bucket sizes are 0, 1, 1, 2, 2, 1, 0.

Next

Bucket 1 holds 1 key, so its secondary table gets 1² = 1 slots. The square is what makes a collision-free hash likely enough to find by trying.

Keys

Intuition

Imagine organizing a library where every book has a permanently reserved spot. You don't just dump books into sections; you have a specific system (Level 1) to find the shelf, and another specific system (Level 2) to find the exact slot on that shelf. Because the books never change (static set), you can spend time upfront designing this perfect system so you never have to search.

Concept

Perfect Hashing uses a two-level hashing scheme to eliminate collisions entirely:

  • Level 1: Hash N keys into N primary buckets. Some collisions may occur here.
  • Level 2: For each bucket i containing ni keys, create a secondary hash table of size ni2. By the Birthday Paradox, a table of quadratic size allows finding a collision-free hash function with high probability.

How it Works

Construction:

  1. Choose a primary hash function h(x) = ((ax + b) mod p) mod N.
  2. Distribute all keys into primary buckets.
  3. For each bucket i:
    • If it has keys, find a secondary hash function hi(x) = ((aix + bi) mod p) mod ni2 that causes zero collisions for the keys in that bucket.
    • Store the parameters (ai, bi) and the secondary table.

Step-by-Step Breakdown

Watch the visualization:

  • Primary Distribution: Keys are hashed into the main buckets. You'll see some buckets empty, some with multiple keys.
  • Secondary Construction: For buckets with collisions, a secondary table (squared size) is created. The algorithm "rolls" random hash parameters until the keys fit perfectly without overlapping.
  • Lookup: To find a key, we hash it once to find the bucket, then hash it again (using the bucket's specific parameters) to find the exact slot.

When to Use

Applications:

  • Static Dictionaries: Reserved keywords in a compiler.
  • CD-ROM / Read-Only Data: Optimizing access when data doesn't change.
  • Hardware Lookup Tables: Network routing tables where lookup speed is critical.

When NOT to Use

  • Dynamic Data: If you frequently add/remove keys, rebuilding the perfect hash table is too expensive.
  • Memory Constraints: The quadratic size (n2) for secondary tables can be memory-heavy if many keys collide in the primary step (though total space is linear on average).

How to Identify

"Static set", "Worst-case O(1) lookup", "Guaranteed constant time", "Two-level hashing".

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description

Frequently Asked Questions

What is Perfect Hashing?

A technique used for a fixed set of keys where a hash function is chosen to ensure NO collisions occur, allowing for O(1) worst-case lookups.


FKS Hashing (Two-level hashing)?

A common perfect hashing scheme using a primary hash table where each bucket is itself another small hash table with its own function.


When is it used?

Ideal for read-only datasets like reserved keywords in a compiler, dictionary lookups, or static configuration tables.