Hash Tables / Hash Maps Flashcards
(50 cards)
What is a hash table?
A data structure that maps keys to values using a hash function.
What is a hash map?
Another name for a hash table, often used interchangeably.
What is a hash function?
A function that converts a key into an index in the hash table.
What is the goal of a good hash function?
To distribute keys uniformly and minimize collisions.
What is a collision in a hash table?
When two keys hash to the same index.
What are common collision resolution techniques?
Chaining and open addressing.
What is chaining?
A method where each table index points to a linked list of entries that hash to that index.
What is open addressing?
A collision resolution method that finds another open slot in the table when a collision occurs.
What are the types of open addressing?
Linear probing, quadratic probing, and double hashing.
What is linear probing?
A technique where you check the next slot until an empty one is found.
What is quadratic probing?
A technique where you check slots at increasing quadratic intervals.
What is double hashing?
Using a second hash function to determine the step size when resolving collisions.
What is the average-case time complexity for search, insert, and delete in a hash table?
O(1)
What is the worst-case time complexity for hash table operations?
O(n), if many collisions occur.
What is the load factor of a hash table?
The ratio of the number of entries to the number of buckets.
Why is the load factor important?
High load factors increase the chance of collisions and degrade performance.
What is rehashing?
The process of resizing the hash table and re-inserting all elements.
When should you rehash a hash table?
When the load factor exceeds a certain threshold.
What is a key in a hash map?
An identifier used to retrieve its associated value.
What is a value in a hash map?
The data associated with a key.
Can hash maps store duplicate keys?
No, each key must be unique.
Can hash maps store duplicate values?
Yes, values can be duplicated.
What happens if you insert a key that already exists?
Its value is updated.
What is a hash collision attack?
An attack that exploits poor hash functions by causing many collisions.