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:
  • MyHashMap() - Initialize an empty hash map
  • put(int key, int value) - Insert or update a key-value pair
  • get(int key) - Return the value for a key, or -1 if not found
  • remove(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

Constraints

  • 0 โ‰ค key, value โ‰ค 106
  • At most 104 calls will be made to put, get, and remove
  • Keys and values are non-negative integers

Visualization

Tap to expand
HashMap: Hash Function Maps Keys to BucketsInput: key=1337hash(1337) = 1337 % 8 = 1Bucket 0Bucket 1 โ†Bucket 2Bucket 3...Bucket 7[1337,99][9,55]๐ŸŽฏ Key Insight: Hash function provides O(1) direct access to the right bucket!Chaining handles collisions: multiple items can share the same bucket
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!
Asked in
Google 42 Amazon 38 Microsoft 31 Meta 26
98.3K Views
High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen