What is a hash table, and how does it work?
What is a hash table, and how does it work?
A hash table is a data structure that provides fast insertion, deletion, and lookup operations. It uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found. In essence, it transforms a key into a location in the array, allowing for efficient data retrieval.
Explanation:
- A hash table stores key-value pairs.
- It uses a hash function to map keys to indices in an array.
- Ideally, different keys map to different indices to allow O(1) time complexity for operations.
Key Talking Points:
- Data Structure: Stores data in key-value pairs.
- Hash Function: Computes an index from a key.
- Efficiency: Provides average O(1) time complexity for lookup, insertion, and deletion.
- Handling Collisions: Uses techniques like chaining or open addressing to handle cases where multiple keys hash to the same index.
NOTES:
Reference Table:
| Aspect | Hash Table | Array |
|---|---|---|
| Access | O(1) average | O(1) |
| Insertion | O(1) average | O(n) when resizing |
| Search | O(1) average | O(n) in unsorted, O(log n) in sorted |
| Order | Unordered | Ordered or Unordered |
| Space Complexity | O(n) | O(n) |
Pseudocode:
# Simple hash table implementation in Python using chaining for collision resolution
class HashTable:
def __init__(self, size):
self.size = size
self.table = [[] for _ in range(size)]
def hash_function(self, key):
return hash(key) % self.size
def insert(self, key, value):
index = self.hash_function(key)
self.table[index].append((key, value))
def search(self, key):
index = self.hash_function(key)
for pair in self.table[index]:
if pair[0] == key:
return pair[1]
return None
# Example usage
hash_table = HashTable(10)
hash_table.insert("apple", 1)
print(hash_table.search("apple")) # Output: 1
Follow-Up Questions and Answers:
-
What are hash collisions, and how can they be resolved?
- Answer: Hash collisions occur when two keys hash to the same index. They can be resolved using:
- Chaining: Store multiple elements in the same index using a linked list or another data structure.
- Open Addressing: Find another open slot in the array using techniques like linear probing or quadratic probing.
- Answer: Hash collisions occur when two keys hash to the same index. They can be resolved using:
-
What qualities are important for a good hash function?
- Answer: A good hash function should:
- Minimize collisions by evenly distributing keys across the hash table.
- Be fast to compute.
- Be deterministic, meaning the same key should always hash to the same index.
- Answer: A good hash function should:
-
When might you prefer a hash table over a binary search tree?
- Answer: You might prefer a hash table when you need:
- Faster average time complexity for lookups, insertions, and deletions.
- An unordered collection where order does not matter.
- Simple key-value pair storage without the need for sorted data.
- Answer: You might prefer a hash table when you need:
These elements provide a comprehensive understanding and preparation for discussing hash tables in an interview setting, especially for a FAANG company.