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 lookup.
Click to reveal answer
beginner
What does a hash function do in a hash table?
A hash function takes a key and converts it into an integer index. This index tells where to store or find the value in the hash table.
Click to reveal answer
intermediate
Why do hash tables sometimes have collisions?
Collisions happen 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 two common ways to handle collisions in hash tables.
1. Chaining: Store multiple values in a list at the same index. 2. Open addressing: Find another empty spot in the table using a probing method.
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 table store?
AOnly values
BOnly keys
CKey-value pairs
DSorted list of values
✗ Incorrect
Hash tables store data as key-value pairs for fast lookup.
What is the main purpose of a hash function?
ASort the keys
BRemove duplicates
CStore values directly
DConvert keys to indexes
✗ Incorrect
Hash functions convert keys into indexes to find where values are stored.
What is a collision in a hash table?
ATwo keys map to the same index
BA key is missing
CValue is duplicated
DTable is empty
✗ Incorrect
Collision happens when different keys produce the same index.
Which method stores multiple values at the same index to handle collisions?
AHashing
BChaining
CSorting
DOpen addressing
✗ Incorrect
Chaining uses a list at each index to store multiple values.
Which is NOT a quality of a good hash function?
AAlways returns zero
BFast computation
CMinimizes collisions
DEven distribution
✗ Incorrect
Always returning zero causes all keys to collide, which is bad.
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 two ways to handle it.
Imagine two friends trying to sit in 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 sort the keys in ascending order
B. To count the number of elements in the table
C. To encrypt the stored data for security
D. To convert a key into an index for storing values
Solution
Step 1: Understand the role of a hash function
A hash function takes a key and converts it into a number called an index.
Step 2: Connect index to storage in hash table
This index tells where to store or find the value in the hash table.
Final Answer:
To convert a key into an index for storing values -> Option D
Quick Check:
Hash function = key to index [OK]
Hint: Hash function maps keys to storage spots [OK]
Common Mistakes:
Thinking hash function sorts keys
Confusing hash function with encryption
Believing hash function counts elements
2. Which of the following is the correct way to declare a hash table (Map) in TypeScript?
easy
A. const map = new map();
B. const map = Map;
C. const map = new Map();
D. const map = new Map(); string, number;
Solution
Step 1: Recall TypeScript Map syntax
In TypeScript, a Map is created with new Map().
Step 2: Check each option's syntax
const map = new Map(); uses correct syntax: new Map(). Others have syntax errors or wrong usage.
Final Answer:
const map = new Map(); -> Option C
Quick Check:
Map declaration = new Map<Key, Value>() [OK]
Hint: Use new Map() to declare [OK]
Common Mistakes:
Omitting 'new' keyword
Using lowercase 'map' instead of 'Map'
Incorrect generic syntax
3. What will be the output of the following TypeScript code?
Setting a key that already exists updates its value.
Step 2: Trace the code
First 'apple' is set to 5, then updated to 10. So map.get('apple') returns 10.
Final Answer:
10 -> Option A
Quick Check:
Updated value = 10 [OK]
Hint: Last set value for key is stored [OK]
Common Mistakes:
Assuming first value stays
Thinking get returns undefined if key repeated
Confusing Map with object property behavior
4. Identify the error in the following TypeScript code using a hash table:
const map = new Map();
map.set(1, 'one');
map.set(2, 'two');
console.log(map.get('1'));
medium
A. Missing parentheses in map.get call
B. Using string '1' instead of number 1 as key
C. Map keys must be strings only
D. Cannot mix number and string values in Map
Solution
Step 1: Check Map key type
Map is declared with number keys, so keys must be numbers.
Step 2: Analyze map.get argument
map.get('1') uses string '1', which does not match number 1 key, so returns undefined.
Final Answer:
Using string '1' instead of number 1 as key -> Option B
Quick Check:
Key type mismatch causes undefined [OK]
Hint: Match key types exactly when using get() [OK]
Common Mistakes:
Assuming '1' and 1 are same keys
Thinking Map keys must be strings
Ignoring type mismatch errors
5. You want to design a hash function for strings that minimizes collisions for English words. Which approach is best?
hard
A. Multiply each character code by a prime number and sum, then modulo table size
B. Use the length of the string as the hash value
C. Sum the ASCII codes of all characters and take modulo table size
D. Use the first character's ASCII code only
Solution
Step 1: Understand collision minimization
Good hash functions spread keys evenly to reduce collisions.
Step 2: Evaluate each approach
Sum the ASCII codes of all characters and take modulo table size sums codes but can cause many collisions (e.g., 'abc' and 'cab'). Use the length of the string as the hash value uses length only, very poor. Use the first character's ASCII code only uses first char only, also poor. Multiply each character code by a prime number and sum, then modulo table size uses weighted sum with primes, better distribution.
Final Answer:
Multiply each character code by a prime number and sum, then modulo table size -> Option A
Quick Check:
Weighted sum with primes = better hash [OK]
Hint: Use prime multipliers for better hash spread [OK]
Common Mistakes:
Using simple sums causing collisions
Ignoring character positions
Using string length as hash
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.