# Hash Tables
Imagine you're building a phone book app. You have millions of names and phone numbers. How do you find someone's number quickly? Searching through the entire list would take forever. This is where hash tables come in โ one of the most elegant solutions in computer science for fast lookups.
The Problem: Slow Lookups
Let's start with a real scenario. You have a list of contacts:
๐ฑ The Phone Book Challenge
You need to find "Alice" in a list of 1 million contacts.
- Array (unsorted): Check each person one by one โ O(n) time
- Array (sorted): Binary search โ O(log n) time
- What if we could do better? โ O(1) time!
The Solution: Hash Tables
Hash tables solve this by using a clever trick: instead of searching, we calculate where the data should be.
๐ก The Core Idea
Think of a hash table like an apartment building with numbered mailboxes:
1. Take a name: "Alice"
2. Run it through a hash function: hash("Alice") = 42
3. Store Alice's number in mailbox #42
4. To find Alice later: hash("Alice") = 42 โ check mailbox #42
Result: Direct access in O(1) time!
Key insight: We convert the name into a number (the mailbox), then go straight there!
See It in Action
Hash Functions
Three Common Methods
1. Division Method
def hash_division(key, table_size):
return key % table_size # Simple and fast!
Best: Use prime table size
2. Multiplication Method
def hash_multiplication(key, table_size):
A = 0.618 # Golden ratio
return int(table_size * ((key * A) % 1))
Best: Better distribution
3. String Hashing
def hash_string(s, table_size):
hash_val = 0
for char in s:
hash_val = (hash_val * 31 + ord(char)) % table_size
return hash_val
Best: Polynomial rolling hash
Collision Resolution
Method 1: Chaining
Idea: Store colliding elements in a linked list
[0] โ Empty
[1] โ "banana" โ "grape" (collision!)
[2] โ Empty
[3] โ "apple"
Pros: Simple, handles high load factors
Cons: Extra memory for pointers
Method 2: Open Addressing (Linear Probing)
Idea: If slot is full, try next slot
hash("apple") = 3 โ [3] is full
Try [4] โ [4] is full
Try [5] โ [5] is empty, insert here!
Pros: Better cache performance
Cons: Clustering, load factor must be < 1
Load Factor
ฮฑ = n/m (items / table size)
Guidelines:
- ฮฑ < 0.75: Good performance
- ฮฑ > 0.75: Consider rehashing
- Chaining: Can exceed 1.0
- Open Addressing: Must be < 1.0
Rehashing: Double table size, reinsert all elements
The Complete Code
Click Run to execute your code
Real-World Applications
๐ Where Hash Tables Are Used
Hash tables are everywhere in real software:
- Databases: Index lookups for fast queries
- Caching: Store frequently accessed data (Redis, Memcached)
- Compilers: Symbol tables for variable names
- Web browsers: DNS cache, browser history
- Programming languages: Python dicts, Java HashMap, JavaScript objects
Almost every modern application uses hash tables internally!
Interview Tips
๐ก Common Interview Questions
Hash tables appear frequently in coding interviews. Here are patterns to recognize:
- โ Fast lookups: "Find if X exists" โ Use hash table
- โ Counting: "Count frequency of elements" โ Hash table
- โ Pairs: "Find two numbers that sum to X" โ Hash table (Two Sum pattern)
- โ Grouping: "Group items by property" โ Hash table
Key points to mention:
- O(1) average case for operations
- Collision resolution method (chaining or open addressing)
- Load factor management
Summary
What You've Learned
Hash tables are a powerful data structure that provide fast lookups by converting keys into array indices:
- Core concept: Use hash functions to map keys to positions
- Performance: O(1) average case for insert, search, and delete
- Hash functions: Division, multiplication, and string hashing methods
- Collision handling: Chaining (linked lists) or open addressing (probing)
- Load factor: Keep ฮฑ < 0.75 for optimal performance
- Applications: Used in databases, caches, compilers, and more!
What's Next?
Now that you understand how hash tables work internally, let's explore practical implementations! Next up: Hash Maps & Sets โ the data structures you'll use in everyday programming.
Enjoying these tutorials?