Home
DSA Course
Hashing
Hash Map (Hash Table)
Hash Map (Hash Table)
A high-performance data structure that maps keys to values for fast lookup, insertion, and deletion.
Ready
See the Logic in Motion
Stop memorizing code. Unlock the full interactive visualizer to master the logic step-by-step.Intuition
Imagine a giant library where every book has a unique ID number. Instead of scanning every shelf to find a book, you use a magic formula (Hash Function) on the ID to get the exact aisle and shelf number immediately. If two books end up on the same shelf (Collision), you just stack them or place one next to the other.
Concept
Hash Function: Converts a given key (string, number, object) into an integer index valid for the array size.
Collision Handling: Strategies to handle cases where two keys hash to the same index.
- Chaining: Store colliding elements in a linked list or dynamic array at that index.
- Open Addressing (Linear Probing): If the slot is full, try the next slot (index+1) until an empty one is found.
How it Works
Insert (Put):
- Compute hash
h = hash(key). - Map to index
i = h % capacity. - If slot
iis empty, store the Key-Value pair. - If occupied, resolve collision (chain or probe).
Search (Get):
- Compute hash and finding index
i. - Check slot
i. If key matches, return value. - If not, traverse chain or probe sequence to find it.
Step-by-Step Breakdown
Watch the visualization:
- Input: You provide a Key and Value.
- Hashing: The system computes the index (highlighted/animated).
- Placement: The item is placed in the corresponding bucket.
- Collision: Observe how Chaining creates a list vs Linear Probing finding the next spot.
When to Use
Applications:
- Caching (e.g., in-memory caches like Redis).
- Database indexing.
- Counting frequencies of items.
- Implementing Sets (Hash Set).
When NOT to Use
- Sorted Data: Hash Maps do not maintain order. Use TreeMap/BST if order matters.
- Worst-case guarantees: Poor hash functions or attacks can degrade performance to O(N).
How to Identify
"Fast lookup by ID", "Count occurrences", "Find duplicates", "Store relations".
Stop Guessing, Start Mastering.
Build the FAANG intuition. Master this pattern with optimized implementations, visual dry runs, and our curated collection of high-yield problems.
