Design HashSet

Build a set of integers without any built-in hash table. Support add(key), remove(key) and contains(key). Keys go into a fixed array of buckets chosen by key % 769, and keys that land in the same bucket share a short chain.

30:00

Design HashSet
easy
Free written solution
Topics
Companies

Build a set of integers without any built-in hash table. Support add(key), remove(key) and contains(key). Keys go into a fixed array of buckets chosen by key % 769, and keys that land in the same bucket share a short chain.

Example 1:
Input: {"commands":["MyHashSet","add","add","contains","contains","add","contains","remove","contains"],"values":[[],[1],[2],[1],[3],[2],[2],[2],[2]]}
Output: [null,null,null,true,false,null,true,null,false]
Constraints:
  • 0≤key≤1060 \leq \text{key} \leq 10^6

  • At most 10410^4 calls will be made to add, remove, and contains.

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

Run code to see results.

Input
arr ={"commands":["MyHashSet","add","add","contains","contains","add","contains","remove","contains"],"values":[[],[1],[2],[1],[3],[2],[2],[2],[2]]}
new
add(1)
add(2)
contains(1)
contains(3)
add(2)
contains(2)
remove(2)
contains(2)

9 calls to run. Keys are spread over 769 buckets by key % 769; only buckets that hold something are drawn.

key
% 769 =
bucket

all 769 buckets (0 in use, 0 keys)

0768

non-empty buckets and their chains

every bucket is empty

Returned values

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