Skip to content
Onsite

Design HashMap

EasyP2Coding

Design a hash map for integer keys and values without using any built-in hash table type (Map, Object as a map, dict, set).

Implement the MyHashMap class:

  • MyHashMap() creates an empty map.
  • put(key, value) stores the pair; if key already exists, replace its value.
  • get(key) returns the value for key, or -1 if it is absent.
  • remove(key) deletes the key if present.

Operations should run in O(1) average time.

Example 1

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]
Why
Putting an existing key replaces its value; removed keys read as -1.

Constraints

  • 0 <= key, value <= 10^6
  • At most 10^4 calls in total.

Hints

Run checks the 2 visible tests, Submit all 7.