Design HashMap

Build an integer-to-integer map without any built-in hash table. Support put(key, value), get(key) and remove(key). This version uses open addressing: keys live directly in one array, a collision steps to the next slot (linear probing), and removed keys leave a tombstone so later lookups keep probing past them.

30:00

Design HashMap
easy
Free written solution
Topics
Companies

Build an integer-to-integer map without any built-in hash table. Support put(key, value), get(key) and remove(key). This version uses open addressing: keys live directly in one array, a collision steps to the next slot (linear probing), and removed keys leave a tombstone so later lookups keep probing past them.

Example 1:
Input: {"commands":["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]
Constraints:
  • 0≤key,value≤1060 \leq \text{key}, \text{value} \leq 10^6

  • At most 10410^4 calls will be made to put, get, and remove.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"commands":["MyHashMap","put","put","get","get","put","get","remove","get"],"values":[[],[1,1],[2,2],[1],[3],[2,1],[2],[2],[2]]}
new
put(1,1)
put(2,2)
get(1)
get(3)
put(2,1)
get(2)
remove(2)
get(2)

9 calls to run on a table of 20011 slots. Only the slots near the current probe are drawn.

key
% 20011 =
home

the table is empty until the first call

whole table: 0 live keys, 0 tombstones

Returned values

[]
Variables
VariableValue
call
size0
output[]
DepthFunction Call
Stack empty
0/27