As reported: Arista Networks, two loops (2025). Backend (Go) round 1: LeetCode 380. Another round 1 (online, 1 hour): a data structure supporting get, add, remove and getRandom in O(1).
This is LeetCode 380 — Insert Delete GetRandom O(1).
Implement RandomizedSet:
insert(val)— insertsvalif absent; returnstrueif it was inserted,falseif already present.remove(val)— removesvalif present; returnstrueif it was removed.getRandom()— returns a random element; each element must have the same probability. At least one element exists when it is called.
Every function must work in average O(1) time.
Example
["RandomizedSet","insert","remove","insert","getRandom","remove","insert","getRandom"]
[[],[1],[2],[2],[],[1],[2],[]]
-> [null,true,false,true,(1 or 2),true,false,2]
Constraints: −2^31 ≤ val ≤ 2^31 − 1; at most 2·10^5 calls.
Code runner format: LeetCode's design format — line 1 the method names, line 2 their arguments. Implement the RandomizedSet class; the harness checks the insert/remove results and that getRandom returns a current element, and prints ok.