A HashMap 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
Why do we use 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 quickly in an array, making data access 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 special ways to handle collisions so data is not lost.
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 searching a value in a well-implemented HashMap?
The average time complexity is O(1), meaning it takes constant time to find a value using its key.
Click to reveal answer
What does a hash function do in a HashMap?
ASorts the keys alphabetically
BConverts a key into an index
CDeletes duplicate keys
DStores values directly
✗ 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 a key is missing
CWhen two keys produce the same index
DWhen the HashMap is empty
✗ Incorrect
A collision happens when two different keys produce the same index from the hash function.
Which method stores multiple key-value pairs at the same index to handle collisions?
ASorting
BLinear probing
CRehashing
DChaining
✗ Incorrect
Chaining stores multiple key-value pairs in a list at the same index to handle collisions.
What is the average time complexity to find a value in a HashMap?
AO(1)
BO(log n)
CO(n)
DO(n log n)
✗ Incorrect
In a well-implemented HashMap, searching takes constant time O(1) on average.
Which of these is NOT a typical operation of a HashMap?
ASort all keys
BDelete key-value pair
CFind value by key
DInsert key-value pair
✗ Incorrect
HashMaps do not sort keys; they store and retrieve key-value pairs efficiently.
Explain how a HashMap stores and retrieves data using a hash function and how collisions are handled.
Think about how you find a book in a library using a code and what happens if two books have the same code.
You got /4 concepts.
Describe the average time complexity of searching in a HashMap and why it is efficient.
Imagine looking up a phone number by name in a well-organized phone book.
You got /4 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 key-value pairs for fast data retrieval
C. To perform mathematical calculations
D. To store data sequentially like an array
Solution
Step 1: Understand HashMap functionality
A HashMap stores data as key-value pairs, allowing quick access by key.
Step 2: Compare with other data structures
Unlike arrays or lists, HashMaps do not store data sequentially or sorted but focus on fast lookup.
Final Answer:
To store key-value pairs for fast data retrieval -> Option B
Quick Check:
HashMap = key-value fast access [OK]
Hint: HashMap = key-value pairs for quick lookup [OK]
Common Mistakes:
Thinking HashMap stores data in order
Confusing HashMap with arrays
Assuming HashMap is for calculations
2. Which of the following is the correct way to declare a simple HashMap class property in TypeScript?
easy
A. private map: {[key: string]: any} = {};
B. map = new Map();
C. let map = [];
D. const map: Map = {};
Solution
Step 1: Identify TypeScript syntax for object property
Declaring a private property with type {[key: string]: any} initializes an object for key-value storage.
Step 2: Check other options for correctness
map = new Map(); uses built-in Map, not a plain object; C is an array; D has wrong Map type usage.
Final Answer:
private map: {[key: string]: any} = {}; -> Option A
Hint: Missing keys return null due to || null fallback [OK]
Common Mistakes:
Expecting undefined instead of null for missing keys
Confusing set and get method effects
Assuming runtime error on missing key
4. Identify the bug in this simple HashMap implementation and choose the correct fix:
class BuggyHashMap {
private map: {[key: string]: number} = {};
set(key: string, value: number) {
this.map[key] = value;
}
get(key: string): number {
return this.map[key];
}
}
const h = new BuggyHashMap();
h.set('x', 10);
console.log(h.get('y'));
medium
A. map should be an array instead of object
B. set method should check if key exists before setting
C. get should return number | null and handle missing keys
D. No bug, code works fine
Solution
Step 1: Analyze get method return type
get returns number but missing keys return undefined, causing type mismatch.
Step 2: Fix get method to handle missing keys
Change return type to number | null and return null if key missing to avoid undefined.
Final Answer:
get should return number | null and handle missing keys -> Option C
Quick Check:
Handle missing keys in get method [OK]
Hint: Always handle missing keys in get method return type [OK]
Common Mistakes:
Ignoring undefined return for missing keys
Changing map to array incorrectly
Thinking set method needs key existence check
5. You want to implement a HashMap from scratch in TypeScript that handles collisions using chaining (array of entries). Which approach correctly adds a new key-value pair to the map?
hard
A. Store keys in an array and values in a separate array without hashing
B. Compute hash, overwrite bucket with new entry ignoring existing keys
C. Use a single array for all entries without hashing
D. Compute hash, check bucket array; if key exists update value, else push new entry
Solution
Step 1: Understand collision handling by chaining
Chaining stores multiple entries in a bucket array; new entries are added or updated if key exists.
Step 2: Analyze options for correct collision handling
Compute hash, check bucket array; if key exists update value, else push new entry correctly checks bucket, updates existing key or adds new entry; others ignore collisions or do not hash.
Final Answer:
Compute hash, check bucket array; if key exists update value, else push new entry -> Option D
Quick Check:
Chaining = bucket array with update or add [OK]
Hint: Check bucket for key, update or add entry to handle collisions [OK]
Common Mistakes:
Overwriting bucket ignoring existing keys
Not using hashing for bucket selection
Storing keys and values separately without hashing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.