Overview & Concept
A Hash Table (Hash Map) is an associative data structure providing average-case O(1) dictionary operations: insertion, search, and deletion. It maps arbitrary keys to bucket indices using a deterministic hash function: index = hash(key) % capacity. Collisions (where two keys hash to the same bucket) are resolved via Separate Chaining (linked buckets) or Open Addressing (linear/quadratic probing).
How It Works
Deterministic Hashing: Keys are converted into pseudo-random integers via uniform hashing functions.
Modulo Index Mapping: The hash integer is mapped into the bucket array using index = hash % capacity.
Collision Resolution: Separate chaining appends colliding key-value pairs into a bucket linked list.
Dynamic Rehashing: When the load factor (n / capacity) exceeds a threshold (typically 0.75), capacity doubles and all keys are rehashed.
Step-by-Step Execution
Asymptotic Complexity Analysis
| Metric | Best Case | Average Case | Worst Case | Space Complexity |
|---|---|---|---|---|
| Complexity Bound | O(1) direct lookup | O(1) uniform hash | O(n) hash collision | O(n) |
| Explanation | Optimal input arrangement minimizing operations. | Expected time for uniformly distributed randomized inputs. | Adversarial degenerate conditions requiring full traversal. | Auxiliary memory allocated beyond original input size. |
Pseudocode & Invariants
// Hash Table Separate Chaining
operation set(key, value):
bucket = hash(key) % capacity
for node in table[bucket]:
if node.key == key: node.val = value; return
table[bucket].append({key, value})
operation get(key):
bucket = hash(key) % capacity
for node in table[bucket]:
if node.key == key: return node.val
return null- Hash Determinism Invariant: If key1 == key2, then hash(key1) == hash(key2) strictly.
- Bucket Containment Invariant: Any key K is stored exclusively in the bucket identified by hash(K) % capacity.
Advantages & Disadvantages
Key Advantages
- •Average O(1) constant time for insert, lookup, and delete operations.
- •Versatile: supports keys of any hashable type (strings, numbers, objects).
- •Industry standard for database indexing, caching, and key-value lookups.
Trade-offs & Limitations
- •Worst-case O(n) performance under severe hash collisions or adversarial hash flooding.
- •Unordered: does not maintain sorted order or key ordering.
- •Memory overhead from bucket arrays and collision pointers.
Real-World Applications
Database Indexing & Caches
Redis key-value store, Memcached, and relational database hash join operators.
Compiler Symbol Tables
Fast identifier lookup during lexing and syntax analysis.
Object / Dictionary Runtimes
JavaScript V8 objects, Python dictionaries, and Java HashMaps.
Decision Guide: When to Use
Ideal Scenarios
- ✓When fast key-value lookups and insertions are the primary requirement.
- ✓When ordering of keys is unimportant.
Consider Alternatives When
- ✕When sorted iteration or range queries (min, max, keys between A and B) are needed (use BST or B-Tree).
Explore Related Algorithms
Compare trade-offs and complexity across complementary tools in AlgoViz.