Design HashMap - Problem
Design Your Own HashMap from Scratch!
Your mission is to build a fully functional HashMap data structure without using any built-in hash table libraries. This is a fundamental challenge that tests your understanding of how hash tables work under the hood.
You need to implement the MyHashMap class with these operations:
Example Usage:
Your mission is to build a fully functional HashMap data structure without using any built-in hash table libraries. This is a fundamental challenge that tests your understanding of how hash tables work under the hood.
You need to implement the MyHashMap class with these operations:
MyHashMap()- Initialize an empty hash mapput(int key, int value)- Insert or update a key-value pairget(int key)- Return the value for a key, or -1 if not foundremove(int key)- Delete a key and its value if it exists
Example Usage:
MyHashMap hashMap = new MyHashMap();
hashMap.put(1, 1); // Map is now [[1,1]]
hashMap.put(2, 2); // Map is now [[1,1], [2,2]]
hashMap.get(1); // Returns 1
hashMap.get(3); // Returns -1 (not found)
hashMap.put(2, 1); // Map is now [[1,1], [2,1]] (updated)
hashMap.get(2); // Returns 1
hashMap.remove(2); // Map is now [[1,1]]
hashMap.get(2); // Returns -1 (removed) Input & Output
basic_operations.py โ Python
$
Input:
["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
โบ
Output:
[null, null, null, 1, -1, null, 1, null, -1]
๐ก Note:
Initialize empty hashmap, insert (1,1) and (2,2), get key 1 returns 1, get key 3 returns -1 (not found), update key 2 to value 1, get key 2 returns 1, remove key 2, get key 2 returns -1 (removed)
collision_handling.py โ Python
$
Input:
["MyHashMap", "put", "put", "put", "get", "get", "get"]
[[], [1, 10], [1001, 20], [2001, 30], [1], [1001], [2001]]
โบ
Output:
[null, null, null, null, 10, 20, 30]
๐ก Note:
Keys 1, 1001, 2001 all hash to same bucket (assuming bucket size 1000), but chaining handles collisions correctly
edge_cases.py โ Python
$
Input:
["MyHashMap", "remove", "get", "put", "put", "put", "get", "remove", "get", "remove", "get"]
[[], [1], [1], [0, 0], [1, 1], [0, 5], [0], [0], [0], [1], [1]]
โบ
Output:
[null, null, -1, null, null, null, 5, null, -1, null, -1]
๐ก Note:
Remove from empty map, get from empty map, insert with key 0, update key 0, remove and get operations on existing keys
Visualization
Tap to expand
Understanding the Visualization
1
Hash Function Magic
Transform any key into a bucket index using modulo: hash(key) = key % bucket_count
2
Direct Access
Use the hash value to jump directly to the correct bucket, no searching needed
3
Handle Collisions
When multiple keys map to same bucket, chain them together in a list
4
Maintain Performance
With good hash distribution, each bucket stays small, keeping operations fast
Key Takeaway
๐ฏ Key Insight: The hash function acts like a perfect librarian - it instantly knows exactly where to look for any book (key), transforming an O(n) search into O(1) direct access!
Time & Space Complexity
Time Complexity
O(1)
Hash function gives direct bucket access. With good distribution, each bucket has few items on average
โ Linear Growth
Space Complexity
O(n)
Space for n key-value pairs plus fixed bucket array overhead
โก Linearithmic Space
Constraints
- 0 โค key, value โค 106
- At most 104 calls will be made to put, get, and remove
- Keys and values are non-negative integers
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code