Explain the concept of Hash Tables and their collision resolution techniques.
Instruction: Discuss the structure of Hash Tables, how they function for data storage and retrieval, and explain at least two methods used to resolve collisions within them.
Updated
Official answer
A hash table maps a key's hash to a bucket or probe sequence and stores key-value entries. Different keys can collide; a hash is not a unique identity. Lookup must compare keys according to the table's equality rule, and equal keys must have compatible hashes.
Two common collision strategies are separate chaining, where a bucket holds several entries, and open addressing, where colliding entries occupy other slots along a probe sequence. Chaining can use lists or other bucket structures. Open addressing needs a search procedure consistent with insertion and deletion; simply clearing a deleted slot can break a probe chain, so implementations use tombstones or a suitable repair strategy.
With suitable hashing and controlled load, operations can have expected O(1) cost; pathological collisions can make them O(n). Resizing also costs work, commonly accounted for amortized over updates. Keep hashing/equality costs in the model for long or complex keys.
Choose a load policy and probe strategy from the workload and security requirements. Open addressing can improve locality but is not universally more memory-efficient or faster. Test colliding keys, repeated updates, deletion, resizing and missing-key lookup, rather than assuming collisions are exceptional.