Design HashMap

IF
AlgoAxiomStaff Engineers
JSTS
Easy20 mins

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

Implement a function that processes an array of operations and an array of arguments:

  • "MyHashMap" — Initializes the HashMap object.
  • "put" — Inserts a (key, value) pair into the HashMap. If key already exists, update its value.
  • "get" — Returns the value mapped to key, or -1 if key is not present.
  • "remove" — Removes the key and its associated value from the HashMap, if present.

Examples

Example 1:

Input: operations = ["MyHashMap","put","put","get","get","put","get","remove","get"], args = [[],[1,1],[2,2],[1],[3],[2,1],[2],[2],[2]]

Output: [null,null,null,1,-1,null,1,null,-1]

Explanation: Put (1,1) and (2,2). Get(1)=1. Get(3)=-1 (not present). Update (2,1). Get(2)=1. Remove 2. Get(2)=-1.

Example 2:

Input: operations = ["MyHashMap","put","get","put","get"], args = [[],[5,10],[5],[5,20],[5]]

Output: [null,null,10,null,20]

Explanation: Put (5,10), get returns 10. Update (5,20), get returns 20.

Constraints

  • 0 <= key, value <= 10^6
  • At most 10^4 calls will be made to put, get, and remove
Source: Custom Data Structures pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle