As reported: Visa Staff Data Engineer (India), round 2 — DSA (2025): design a data structure supporting GET, PUT, DELETE and GETRANDOM, all in
O(1).
Implement RandomizedStore, a map from integer keys to integer values:
put(key, value)— insert the key, or update its value if it exists.get(key)— the value ofkey, or-1if it is not stored.remove(key)— delete the key; returntrueif it was stored,falseotherwise.getRandom()— return a uniformly random stored key: every stored key with the same probability. It is only called when at least one key is stored.
Every operation must run in average O(1) time.
This extends LeetCode 380 — Insert Delete GetRandom O(1) from a set to a key-value map.
Example
["RandomizedStore","put","put","get","getRandom","remove","get","getRandom"]
[[],[1,10],[2,20],[1],[],[1],[1],[]]
get(1) returns 10; the first getRandom() returns 1 or 2 with equal probability; remove(1) returns true; get(1) then returns -1; the last getRandom() returns 2.
Constraints: -10^5 ≤ key ≤ 10^5; -10^6 ≤ value ≤ 10^6; at most 2·10^4 calls.
Code runner format: two JSON lines: the operation names, then their arguments (LeetCode's design-problem format). The harness checks every return value, that getRandom returns a stored key, and — over many calls on an unchanged store — that each key comes up roughly equally often; it prints "ok" or the first problem found.