Design a HashMap without using any built-in hash table libraries.

Implement the MyHashMap class:

  • MyHashMap() initializes the object with an empty map.
  • void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.
  • int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
  • void remove(int key) removes the key and its corresponding value if the map contains the mapping for the key.

Input & Output

Example 1 — Basic Operations
$ Input: operations = ["MyHashMap","put","put","get","get","put","get","remove","get"], values = [[],[1,1],[2,2],[1],[3],[2,1],[2],[2],[2]]
Output: [null,null,null,1,-1,null,1,null,-1]
💡 Note: Create empty hashmap, put(1,1), put(2,2), get(1)→1, get(3)→-1, put(2,1), get(2)→1, remove(2), get(2)→-1
Example 2 — Key Updates
$ Input: operations = ["MyHashMap","put","get","put","get"], values = [[],[5,10],[5],[5,20],[5]]
Output: [null,null,10,null,20]
💡 Note: put(5,10) stores key 5 with value 10, get(5) returns 10, put(5,20) updates key 5 to value 20, get(5) returns 20
Example 3 — Non-existent Keys
$ Input: operations = ["MyHashMap","get","remove","get"], values = [[],[1],[2],[1]]
Output: [null,-1,null,-1]
💡 Note: Empty hashmap: get(1) returns -1 (not found), remove(2) does nothing, get(1) still returns -1

Constraints

  • 0 ≤ key, value ≤ 106
  • At most 104 calls will be made to put, get, and remove.

Visualization

Tap to expand
Design HashMap - Hash Table with Chaining INPUT Operations: ["MyHashMap", "put","put","get","get", "put","get","remove", "get"] Values: [[],[1,1],[2,2],[1],[3], [2,1],[2],[2],[2]] Initial Bucket Array: 0 1 2 ... n Hash: key % bucket_size Each bucket: linked list [0] [1] [2] ALGORITHM STEPS 1 Hash Function index = key % size 2 Put Operation Add/update at bucket[idx] 3 Get Operation Search chain, return val/-1 4 Remove Operation Delete node from chain Execution Trace: put(1,1) --> bucket[1]:(1,1) put(2,2) --> bucket[2]:(2,2) get(1) --> found: 1 get(3) --> not found: -1 put(2,1) --> update (2,1) get(2) --> found: 1 remove(2) --> delete (2,1) get(2) --> not found: -1 FINAL RESULT Final HashMap State: [0] null [1] (1,1) [2] null (removed) ... null Output: [null,null,null,1,-1, null,1,null,-1] OK - All Operations Complete Time: O(N/K) average per op Space: O(K + M) K=buckets, M=entries, N=keys Key Insight: Hash Table with Chaining uses an array of linked lists (buckets). Each key is hashed to find its bucket index. Collisions (multiple keys mapping to same index) are handled by storing entries in a linked list at that bucket. This provides O(1) average time for put, get, and remove operations with proper hash distribution. TutorialsPoint - Design HashMap | Hash Table with Chaining Approach
Asked in
Amazon 45 Google 38 Microsoft 32 Facebook 28
78.4K 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