A hash table is a data structure that stores data in key-value pairs. It uses a hash function to convert keys into an index where values are 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 index (a number) that tells where to store or find the value in the hash table. It helps in quick access to data.
Click to reveal answer
intermediate
What is a collision in a hash table?
A collision happens when two different keys produce the same index from the hash function. The hash table must handle collisions to store both values correctly.
Click to reveal answer
intermediate
Name one common method to handle collisions in hash tables.
One common method is chaining, where each index in the hash table holds a list of entries. If multiple keys map to the same index, their values are stored in that list.
Click to reveal answer
intermediate
Why should a hash function distribute keys uniformly?
Uniform distribution helps avoid many keys clustering at the same index, which reduces collisions and keeps data retrieval fast.
Click to reveal answer
What does a hash function do in a hash table?
ASorts the keys alphabetically
BConverts a key into an index for storage
CDeletes duplicate keys
DStores values directly without keys
✗ Incorrect
A hash function converts a key into an index where the value is stored or retrieved.
What is a collision in a hash table?
AWhen two keys map to the same index
BWhen a key is missing
CWhen the table is empty
DWhen values are duplicated
✗ Incorrect
A collision occurs when two different keys produce the same index from the hash function.
Which method stores multiple values at the same index to handle collisions?
ASorting
BIndexing
CHashing
DChaining
✗ Incorrect
Chaining stores multiple values in a list at the same index to handle collisions.
Why is uniform distribution important in a hash function?
ATo store values in order
BTo increase collisions for security
CTo reduce collisions and keep access fast
DTo sort keys alphabetically
✗ Incorrect
Uniform distribution reduces collisions and helps keep data retrieval fast.
What type of data structure is a hash table?
AKey-value store
BStack
CQueue
DLinked list
✗ Incorrect
A hash table stores 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 place to find the value.
You got /3 concepts.
Describe what a collision is in a hash table and one way to handle it.
Imagine two friends trying to sit on the same chair.
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 data
B. To sort the keys in alphabetical 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 a number that can be used as an index in the hash table.
Step 2: Identify the correct purpose
Sorting keys or encrypting data is not the job of a hash function; it only maps keys to indexes.
Final Answer:
To convert a key into an index for storing data -> Option A
Quick Check:
Hash function = key to index [OK]
Hint: Hash functions map keys to indexes, not sort or encrypt [OK]
Common Mistakes:
Thinking hash functions sort keys
Confusing hash functions with encryption
Believing hash functions count elements
2. Which of the following is the correct way to create an empty hash table (object) in JavaScript?
easy
A. const table = [];
B. const table = {};
C. const table = new Map();
D. const table = new HashTable();
Solution
Step 1: Recall JavaScript object syntax
In JavaScript, an empty object is created using curly braces {} which can act as a hash table.
Step 2: Evaluate options
const table = []; creates an array, not a hash table. const table = new Map(); creates a Map, which is a different structure. const table = new HashTable(); is invalid as HashTable is not a built-in class.
Final Answer:
const table = {}; -> Option B
Quick Check:
Empty hash table = {} [OK]
Hint: Use curly braces {} to create an empty object hash table [OK]
B. Cannot use numbers as keys in JavaScript objects
C. Accessing table['cat'] is incorrect; should use table[hash('cat')]
D. Hash function should return a string, not a number
Solution
Step 1: Understand how keys are stored and accessed
The hash function returns a number used as a key in the table object.
Step 2: Check how the table is accessed
The code tries to access table['cat'], but the key stored is a number from hash('cat'), so it should access table[hash('cat')].
Final Answer:
Accessing table['cat'] is incorrect; should use table[hash('cat')] -> Option C
Quick Check:
Access key must match stored key type [OK]
Hint: Access hash table with hashed key, not original key [OK]
Common Mistakes:
Accessing with original key instead of hashed key
Thinking keys must be strings only
Assuming hash function causes error due to key.length
5. Given the hash function below, what is the best way to handle collisions when two keys produce the same hash?
function simpleHash(key) {
let sum = 0;
for (let char of key) {
sum += char.charCodeAt(0);
}
return sum % 5;
}
Choose the best collision handling method.
hard
A. Increase the hash size to avoid collisions
B. Overwrite the existing value with the new one
C. Ignore the new key and keep the old value
D. Use chaining by storing multiple values in a list at the same index
Solution
Step 1: Understand collision in hash tables
When two keys produce the same hash index, a collision occurs and must be handled to store both values.
Step 2: Evaluate collision handling methods
Overwriting loses data, ignoring new keys is incorrect, increasing hash size alone doesn't solve collisions. Chaining stores multiple values in a list at the same index, preserving all data.
Final Answer:
Use chaining by storing multiple values in a list at the same index -> Option D
Quick Check:
Chaining handles collisions by lists [OK]
Hint: Use lists (chaining) to store multiple values at one index [OK]
Common Mistakes:
Overwriting values causing data loss
Ignoring new keys on collision
Thinking increasing hash size removes all collisions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.