Hash Tables
Hash tables map keys to values by computing an integer hash code, compressing it to an array index, and resolving collisions. Under a uniform hashing assumption with load factor bounded below 1 (or resize threshold typically 0.5–0.75), search, insertion, and deletion operate in amortized O(1) time.
Key Invariants & Rules
- Every key has a deterministic bucket index = (hash(key) & 0x7fffffff) % capacity.
- Separate chaining stores colliding entries in a linked chain per bucket slot.
- Linear probing searches consecutive slots modulo capacity until an empty cell or matching key is found.
- Resizing doubles capacity and rehashes all reachable keys in original index order.
- Soft deletion / tombstoning in open addressing ensures probe chains are not broken prematurely.
Must-Know Algorithms
Standard collision resolution preserving node identities across rehashes.
Cache-friendly open addressing requiring cluster avoidance and tombstones.
Doubling capacity keeps the load factor bounded, preventing cluster degradation.
Learning Path (3 Lessons)
Self-Check & Concept Review
1. In separate chaining with table size M, what happens when a new key collides with an existing bucket?
2. Why does linear probing require soft deletion (tombstones) or cluster rehash on remove?
3. What is the purpose of the bitwise mask `hash & 0x7fffffff` before the modulo operator in Java hash tables?
4. When resizing a hash table from capacity M to 2M, what determines the new bucket of an entry?
5. What is primary clustering in open addressing?