A HashMap is a data structure that stores 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
Why do we use a hash function in a HashMap?
A hash function converts a key into an index in an array. This helps quickly find where the value for that key is stored, making lookups fast.
Click to reveal answer
intermediate
What is a collision in a HashMap?
A collision happens when two different keys produce the same index from the hash function. We need a way to handle collisions to store both values correctly.
Click to reveal answer
intermediate
Name one common method to handle collisions in a HashMap.
One common method is chaining, where each array index holds a list of key-value pairs. If multiple keys map to the same index, they are stored in that list.
Click to reveal answer
intermediate
What is the average time complexity for get and put operations in a well-implemented HashMap?
The average time complexity is O(1), meaning it takes constant time to get or put a value, thanks to direct indexing using the hash function.
Click to reveal answer
What does a hash function do in a HashMap?
ASorts the keys alphabetically
BStores the value directly
CConverts a key into an index
DDeletes duplicate keys
✗ Incorrect
A hash function converts a key into an index to store or find the value quickly.
What is a collision in a HashMap?
AWhen two keys have the same value
BWhen two keys map to the same index
CWhen a key is missing
DWhen the HashMap is empty
✗ Incorrect
A collision happens when two different keys produce the same index from the hash function.
Which method can handle collisions by storing multiple key-value pairs at the same index?
AHashing
BSorting
CBinary Search
DChaining
✗ Incorrect
Chaining stores multiple key-value pairs in a list at the same index to handle collisions.
What is the average time complexity for searching a key in a good HashMap?
AO(1)
BO(log n)
CO(n log n)
DO(n)
✗ Incorrect
A well-implemented HashMap provides average O(1) time for searching keys.
In a HashMap, what happens if two keys produce the same index and chaining is not used?
AOne value overwrites the other
BBoth values are stored separately
CThe HashMap resizes automatically
DThe keys are merged
✗ Incorrect
Without collision handling like chaining, one value overwrites the other at the same index.
Explain how a HashMap stores and retrieves data using a hash function and handles collisions.
Think about how keys become positions and what happens if two keys want the same spot.
You got /4 concepts.
Describe the advantages of using a HashMap compared to a simple list for storing key-value pairs.
Focus on speed and how data is found quickly.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a HashMap in data structures?
easy
A. To store key-value pairs for fast data retrieval
B. To store data in a sorted order
C. To perform mathematical calculations
D. To store data in a sequential list
Solution
Step 1: Understand what a HashMap stores
A HashMap stores data as key-value pairs, allowing quick access to values using keys.
Step 2: Identify the main purpose
The main purpose is to retrieve data quickly by keys, not to sort or calculate.
Final Answer:
To store key-value pairs for fast data retrieval -> Option A
Quick Check:
HashMap = fast key-value access [OK]
Hint: Remember: HashMap = key-value pairs for quick lookup [OK]
Common Mistakes:
Confusing HashMap with sorted data structures
Thinking HashMap stores data sequentially
Assuming HashMap is for calculations
2. Which of the following is the correct way to define a simple HashMap class constructor in Python?
easy
A. def __init__(self): self.map = set()
B. def __init__(self): self.map = []
C. def __init__(self): self.map = {}
D. def __init__(self): self.map = ()
Solution
Step 1: Identify the data structure for storing key-value pairs
A HashMap stores key-value pairs, so a dictionary {} is appropriate.
Step 2: Check each option's data type
def __init__(self): self.map = {} uses {} which is a dictionary. Others use list, set, or tuple which are not suitable for key-value mapping.
Final Answer:
def __init__(self): self.map = {} -> Option C
Quick Check:
HashMap uses dictionary = {} [OK]
Hint: Use {} to store key-value pairs in Python [OK]
Common Mistakes:
Using list or tuple instead of dictionary
Confusing set with dictionary
Not initializing the map attribute
3. What will be the output of the following code?
class HashMap:
def __init__(self):
self.map = {}
def put(self, key, value):
self.map[key] = value
def get(self, key):
return self.map.get(key, None)
h = HashMap()
h.put('a', 1)
h.put('b', 2)
print(h.get('a'))
print(h.get('c'))
medium
A. 1\nNone
B. 1\n0
C. None\nNone
D. Error
Solution
Step 1: Trace the put method calls
Keys 'a' and 'b' are added with values 1 and 2 respectively.
Step 2: Trace the get method calls
Getting 'a' returns 1. Getting 'c' returns None because 'c' is not in the map and default is None.
Hint: get() returns None if key not found by default [OK]
Common Mistakes:
Expecting 0 instead of None for missing keys
Confusing print output format
Assuming error on missing key
4. Find the error in this HashMap implementation snippet:
class HashMap:
def __init__(self):
self.map = []
def put(self, key, value):
self.map[key] = value
medium
A. Missing return statement in put method
B. No error, code is correct
C. Incorrect method name put instead of add
D. Using list instead of dictionary for self.map
Solution
Step 1: Check the data structure used for self.map
self.map is initialized as a list, but lists do not support key-based assignment.
Step 2: Understand why list causes error
Using self.map[key] = value will cause TypeError because list indices must be integers, not keys.
Final Answer:
Using list instead of dictionary for self.map -> Option D
Quick Check:
List can't use keys as indices [OK]
Hint: Use dictionary {} for key-value storage, not list [] [OK]
Common Mistakes:
Thinking put method needs return
Confusing method names
Ignoring data type of self.map
5. You want to implement a HashMap that handles collisions by chaining (using lists). Which of the following code snippets correctly initializes the internal storage for chaining?
hard
A. self.map = {}
B. self.map = [[] for _ in range(size)]
C. self.map = [None] * size
D. self.map = [0] * size
Solution
Step 1: Understand chaining collision handling
Chaining uses a list of lists where each bucket holds a list of key-value pairs.
Step 2: Identify correct initialization
self.map = [[] for _ in range(size)] creates a list of empty lists, one for each bucket, suitable for chaining.
Step 3: Why other options are incorrect
self.map = [None] * size creates a list of None, which needs extra checks. self.map = {} is a dictionary, not a list of buckets. self.map = [0] * size creates a list of zeros, not suitable for chaining.
Final Answer:
self.map = [[] for _ in range(size)] -> Option B
Quick Check:
Chaining needs list of lists = [[] for _ in range(size)] [OK]
Hint: Use list comprehension to create buckets for chaining [OK]
Common Mistakes:
Using None or 0 instead of empty lists for buckets
Confusing dictionary with list of buckets
Not initializing buckets properly for chaining
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.