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 need a hash function in a HashMap?
A hash function converts a key into an index number. This index tells where to store or find the value in the underlying array, making lookups fast.
Click to reveal answer
intermediate
What is collision in a HashMap?
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 index holds a list of key-value pairs. If multiple keys map to the same index, they are stored in this 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?
AConverts a key into an index
BSorts the keys alphabetically
CStores values directly
DRemoves duplicate keys
✗ Incorrect
A hash function converts a key into an index to store or find values quickly.
What is a collision in a HashMap?
AWhen two keys produce the same index
BWhen two keys have the same value
CWhen a key is missing
DWhen the HashMap is empty
✗ Incorrect
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?
ASorting
BBinary Search
CHashing
DChaining
✗ Incorrect
Chaining stores multiple key-value pairs in a list at the same index to handle collisions.
What is the average time complexity of retrieving a value from a HashMap?
AO(n)
BO(log n)
CO(1)
DO(n log n)
✗ Incorrect
Retrieving a value from a HashMap is on average O(1) due to direct indexing.
In a simple HashMap implementation, what data structure is often used to store values at each index?
AArray
BLinked List
CStack
DQueue
✗ Incorrect
Linked lists are often used at each index to handle collisions by chaining.
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 steps to implement a basic HashMap from scratch in JavaScript.
Start with an array and a way to convert keys to indexes.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of a HashMap in data structures?
easy
A. To store data in a sorted order
B. To store data as a sequence of elements
C. To store data in a last-in-first-out order
D. To store key-value pairs for fast data retrieval
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: Compare with other data structures
Unlike lists or stacks, HashMaps focus on fast lookup, not order or sequence.
Final Answer:
To store key-value pairs for fast data retrieval -> Option D
Quick Check:
HashMap = key-value fast access [OK]
Hint: HashMaps store pairs for quick lookup by key [OK]
Common Mistakes:
Confusing HashMap with list or stack
Thinking HashMap stores data in order
Assuming HashMap stores only values without keys
2. Which of the following is the correct way to define a simple HashMap class constructor in JavaScript?
easy
A. class HashMap { constructor() { this.map = {}; } }
B. class HashMap { constructor() { map = []; } }
C. function HashMap() { this.map = new Map(); }
D. class HashMap { constructor() { this.map = null; } }
Solution
Step 1: Check class syntax and property initialization
class HashMap { constructor() { this.map = {}; } } correctly defines a class with a constructor initializing an object property this.map.
Step 2: Identify errors in other options
class HashMap { constructor() { map = []; } } misses this., so map is not a property. function HashMap() { this.map = new Map(); } uses function syntax, not class. class HashMap { constructor() { this.map = null; } } initializes this.map to null, which is not useful for storing pairs.
Final Answer:
class HashMap { constructor() { this.map = {}; } } -> Option A
Quick Check:
Proper class constructor uses this.property = value [OK]
Hint: Use this.property inside constructor for class fields [OK]
Common Mistakes:
Forgetting 'this.' before property name
Mixing function and class syntax
Initializing property to null instead of object
3. Consider this simple HashMap implementation snippet:
First, key 'a' is set to 1, then 'b' to 2, then 'a' is updated to 3, overwriting previous value.
Step 2: Check the get operation
Calling get('a') returns the current value for 'a', which is 3.
Final Answer:
3 -> Option A
Quick Check:
Last set value for 'a' = 3 [OK]
Hint: Last set value for a key overwrites previous [OK]
Common Mistakes:
Assuming first set value remains
Confusing keys and values
Expecting null if key was set before
4. The following HashMap method is intended to remove a key-value pair but has a bug:
remove(key) {
delete this.map.key;
}
What is the correct fix?
medium
A. Change to delete this.map->key;
B. Change to this.map.delete(key);
C. Change to delete this.map[key];
D. Change to this.map.remove(key);
Solution
Step 1: Understand property access in JavaScript objects
Using dot notation with this.map.key looks for property literally named 'key', not the variable key.
Step 2: Use bracket notation for dynamic keys
Bracket notation this.map[key] accesses the property named by the variable key. So delete this.map[key]; correctly deletes the key-value pair.
Final Answer:
Change to delete this.map[key]; -> Option C
Quick Check:
Use brackets for variable keys [OK]
Hint: Use brackets [] to delete dynamic keys [OK]
Common Mistakes:
Using dot notation with variable keys
Using non-existent methods like remove()
Using wrong delete syntax
5. You want to implement a keys() method in your HashMap class that returns all keys stored. Which of these implementations correctly returns an array of keys?
hard
A. keys() { return Object.values(this.map); }
B. keys() { return Object.keys(this.map); }
C. keys() { return this.map.keys(); }
D. keys() { return this.map.getKeys(); }
Solution
Step 1: Identify how to get keys from a plain object
JavaScript's Object.keys() returns an array of keys from an object.
Step 2: Check other options for correctness
keys() { return this.map.keys(); } assumes this.map is a Map object, but here it's a plain object, so this.map.keys() is undefined. keys() { return Object.values(this.map); } returns values, not keys. keys() { return this.map.getKeys(); } uses a non-existent method.
Final Answer:
keys() { return Object.keys(this.map); } -> Option B
Quick Check:
Object.keys() returns keys array [OK]
Hint: Use Object.keys() to get keys from plain objects [OK]
Common Mistakes:
Using Map methods on plain objects
Returning values instead of keys
Calling non-existent methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.