DSA Notes
A complete introduction to hashing — what a hash table is, how hash functions work, why hash tables achieve O(1) average operations, the load factor, the difference between hash maps and hash sets, and real-world applications of hashing in databases, security, and caching.
What Is a Hash Table?
A hash table (also called a hash map or hash set) is a data structure that provides O(1) average time for three fundamental operations: insert, delete, and lookup.
This is dramatically better than:
- Arrays: O(n) for unsorted search, O(1) for indexed access only
- Sorted arrays: O(log n) search, O(n) insert/delete
- Balanced BSTs: O(log n) for all three
The secret is a hash function — a mathematical transformation that converts a key into an array index.
Collisions — When Two Keys Map to Same Index
A collision occurs when two different keys hash to the same index. With any hash function and finite table, collisions are inevitable (by the pigeonhole principle).
Two main strategies to handle collisions:
- Chaining — store a linked list at each index
- Open Addressing — find another empty slot in the table
Basic Hash Table Structure
Load Factor and Rehashing
The load factor α = n/m where n = number of stored items, m = table size.
Rehashing: When α exceeds the threshold, create a new table (usually 2× size) and re-insert all entries:
Python's Built-in Hash Tables
Hash Map vs Other Data Structures
| Operation | Array (unsorted) | Sorted Array | BST (balanced) | Hash Map |
|---|---|---|---|---|
| Search | O(n) | O(log n) | O(log n) | O(1) avg |
| Insert | O(1) append | O(n) shift | O(log n) | O(1) avg |
| Delete | O(n) shift | O(n) shift | O(log n) | O(1) avg |
| Min/Max | O(n) | O(1) | O(log n) | O(n) |
| Range query | O(n) | O(log n + k) | O(log n + k) | O(n) |
| Ordered iteration | O(n log n) | O(n) | O(n) | Not supported |
| Memory | Low | Low | Medium | Medium-High |
Hash maps win for: Lookup-heavy workloads, frequency counting, deduplication BSTs/sorted arrays win for: Range queries, ordered operations, minimum/maximum
Real-World Applications of Hashing
1. Database Indexes (Hash Indexes)
| Query | SELECT * WHERE id = 12345 |
| Hash index: hash(12345) | bucket → O(1) lookup |
| (vs B-Tree index | O(log n) traversal) |
2. Caches (Memoization)
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
if n <= 1: return n
return fib(n-1) + fib(n-2)
# Internally uses a hash map: n → computed fib(n)3. Sets and Deduplication
4. Two Sum and Similar Problems
5. Password Hashing (Cryptographic)
Summary
Hash tables provide the fastest average-case performance for the core dictionary operations:
- O(1) average insert, delete, lookup
- O(n) worst case (when many collisions degrade to linear search)
- Load factor controls the balance between space and time
- Rehashing maintains O(1) amortized performance
Python's dict and set are hash tables. They are the most-used data structures in Python programs and the first tool to reach for when you need fast lookup.
*Next Lesson: Hash Functions*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Hashing — Complete Introduction to Hash Tables and Hash Functions.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, hashing, introduction, hashing — complete introduction to hash tables and hash functions
Related Data Structures & Algorithms Topics