Challenge - 5 Problems
HashMap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this HashMap size check?
Consider this simple HashMap class with put and size methods. What will be printed after the operations?
DSA Javascript
class HashMap { constructor() { this.map = {}; } put(key, value) { this.map[key] = value; } size() { return Object.keys(this.map).length; } } const map = new HashMap(); map.put('a', 1); map.put('b', 2); map.put('a', 3); console.log(map.size());
Attempts:
2 left
💡 Hint
Think about what happens when you add a key that already exists.
✗ Incorrect
The HashMap stores keys uniquely. Adding 'a' twice overwrites the first value, so only two keys exist: 'a' and 'b'.
❓ Predict Output
intermediateWhat is the output after these put and get operations?
Given this HashMap implementation, what will be the output of the get calls?
DSA Javascript
class HashMap { constructor() { this.map = {}; } put(key, value) { this.map[key] = value; } get(key) { return this.map[key] === undefined ? null : this.map[key]; } } const map = new HashMap(); map.put('x', 10); map.put('y', 20); console.log(map.get('x')); console.log(map.get('z'));
Attempts:
2 left
💡 Hint
Check how get handles keys not present in the map.
✗ Incorrect
The get method returns the value if present, else null. 'x' exists with 10, 'z' does not exist, so null is returned.
🔧 Debug
advancedWhy does this HashMap implementation fail to store multiple keys?
This HashMap uses an array of buckets but fails to store multiple keys correctly. Identify the bug.
DSA Javascript
class HashMap { constructor(size = 5) { this.buckets = new Array(size).fill([]); } hash(key) { let hash = 0; for (let char of key) { hash += char.charCodeAt(0); } return hash % this.buckets.length; } put(key, value) { const index = this.hash(key); this.buckets[index].push([key, value]); } get(key) { const index = this.hash(key); for (let [k, v] of this.buckets[index]) { if (k === key) return v; } return null; } } const map = new HashMap(); map.put('a', 1); map.put('b', 2); console.log(map.get('a')); console.log(map.get('b'));
Attempts:
2 left
💡 Hint
Check how the buckets array is initialized.
✗ Incorrect
Using fill([]) assigns the same array reference to all buckets, so pushing to one bucket affects all buckets, causing data corruption.
❓ Predict Output
advancedWhat is the output after removing a key from this HashMap?
This HashMap supports remove operation. What will be printed after these operations?
DSA Javascript
class HashMap { constructor() { this.map = {}; } put(key, value) { this.map[key] = value; } remove(key) { if (this.map.hasOwnProperty(key)) { delete this.map[key]; return true; } return false; } size() { return Object.keys(this.map).length; } } const map = new HashMap(); map.put('one', 1); map.put('two', 2); map.remove('one'); console.log(map.size());
Attempts:
2 left
💡 Hint
Removing a key reduces the size by one if the key exists.
✗ Incorrect
After removing 'one', only 'two' remains, so size is 1.
🧠 Conceptual
expertWhich property is essential for a good hash function in a HashMap?
To ensure efficient storage and retrieval in a HashMap, what property must the hash function have?
Attempts:
2 left
💡 Hint
Think about how collisions affect performance.
✗ Incorrect
A good hash function distributes keys evenly to reduce collisions and maintain fast access times.
