A hash table is a data structure that stores key-value pairs. It uses a hash function to convert keys into an index where the value is stored, allowing fast data retrieval.
Click to reveal answer
beginner
What is the role of a hash function in a hash table?
A hash function takes a key and returns an integer index. This index tells where to store or find the value in the hash table.
Click to reveal answer
intermediate
Why do collisions happen in a hash table?
Collisions happen when two different keys produce the same index from the hash function. This means both values want to be stored in the same spot.
Click to reveal answer
intermediate
Name one common method to handle collisions in hash tables.
One common method is chaining, where each index stores a list of key-value pairs. If multiple keys map to the same index, they are stored in the list.
Click to reveal answer
intermediate
What qualities make a good hash function?
A good hash function distributes keys evenly across the table, is fast to compute, and minimizes collisions.
Click to reveal answer
What does a hash function output in a hash table?
AA sorted list of keys
BThe original key
CThe value itself
DAn index to store the value
✗ Incorrect
The hash function outputs an index where the value is stored or retrieved.
What is a collision in a hash table?
AWhen a key is missing
BWhen two keys map to the same index
CWhen the table is empty
DWhen the hash function fails
✗ Incorrect
A collision happens when two different keys produce the same index.
Which method stores multiple values at the same index to handle collisions?
AChaining
BLinear probing
CRehashing
DSorting
✗ Incorrect
Chaining stores multiple key-value pairs in a list at the same index.
Why is it important for a hash function to distribute keys evenly?
ATo reduce collisions and improve speed
BTo make the table bigger
CTo sort the keys
DTo store duplicate keys
✗ Incorrect
Even distribution reduces collisions and keeps operations fast.
What type of data does a hash table store?
ASorted arrays
BOnly keys
CKey-value pairs
DOnly values
✗ Incorrect
Hash tables store data as key-value pairs.
Explain how a hash table uses a hash function to store and retrieve data.
Think about how a key becomes a position in the table.
You got /3 concepts.
Describe what a collision is and one way to handle it in a hash table.
Imagine two friends wanting the same seat.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a hash function in a hash table?
easy
A. To convert a key into an index for storing values
B. To sort the keys in ascending order
C. To encrypt the data stored in the table
D. To count the number of elements in the table
Solution
Step 1: Understand the role of a hash function
A hash function takes a key and converts it into an index number that points to a position in the hash table.
Step 2: Identify the purpose in context
This index helps quickly find or store the value associated with the key without searching the entire table.
Final Answer:
To convert a key into an index for storing values -> Option A
Quick Check:
Hash function = key to index [OK]
Hint: Hash function maps keys to indexes fast [OK]
Common Mistakes:
Thinking hash functions sort keys
Confusing hashing with encryption
Assuming hash function counts elements
2. Which of the following is the correct syntax to declare a hash table using unordered_map in C++?
easy
A. hash_map table;
B. unordered_map table;
C. map table;
D. unordered table;
Solution
Step 1: Recall C++ hash table declaration
The standard C++ hash table is declared using unordered_map from the STL.
Step 2: Check each option
unordered_map table; uses unordered_map which is correct syntax. hash_map table; uses hash_map which is non-standard. map table; uses map which is a balanced tree, not a hash table. unordered table; uses unordered which is invalid.
Final Answer:
unordered_map<int, string> table; -> Option B
Quick Check:
unordered_map syntax = correct [OK]
Hint: Use unordered_map for hash tables in C++ [OK]
Common Mistakes:
Using map instead of unordered_map
Using non-standard hash_map
Incorrect container names
3. What will be the output of the following C++ code snippet?