DSAVisualiseData Structures & Algorithms Studio
Chapter 01 · hash-table

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

Separate ChainingO(1) amortized avg, O(N) worst

Standard collision resolution preserving node identities across rehashes.

Linear ProbingO(1) amortized avg, O(N) worst

Cache-friendly open addressing requiring cluster avoidance and tombstones.

Dynamic ResizingO(N) during resize, O(1) amortized

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?

Separate chaining maintains a linked list of key-value pairs at each bucket; if the key is distinct, a new node is added to the chain.

2. Why does linear probing require soft deletion (tombstones) or cluster rehash on remove?

Linear probing stops searching at the first empty (null) slot. Simply clearing a slot would orphan subsequent keys that collided and probed past it.

3. What is the purpose of the bitwise mask `hash & 0x7fffffff` before the modulo operator in Java hash tables?

In Java, hashCode() returns a signed 32-bit int. Negative integers yield negative modulo results, so masking off the sign bit ensures an index >= 0.

4. When resizing a hash table from capacity M to 2M, what determines the new bucket of an entry?

Rehashing recalculates the compressed index (hash & 0x7fffffff) % newCapacity for every active entry.

5. What is primary clustering in open addressing?

Primary clustering occurs in linear probing because any collision in a block of occupied slots makes that entire block even longer.