Constant-Time Insert, Remove, and Uniform Random Sampling
xAIMachine Learning Engineer · OnsiteLast updated
This question evaluates the ability to design a data structure that supports constant-time insertions, removals, and uniform random sampling, testing concepts such as hashing, indexed storage, and randomized selection within the coding and algorithms domain.
A data-sampling service maintains a dynamically changing pool of integer example IDs. Examples are continuously added and retired, and the service must repeatedly draw a uniformly random example from the current pool. All three operations must run in average O(1) time.
Implement a class RandomizedPool supporting:
insert(val)— insertsvalinto the pool if it is not already present. Returnstrueifvalwas inserted,falseif it was already present.remove(val)— removesvalfrom the pool if it is present. Returnstrueifvalwas removed,falseif it was not present.getRandom()— returns a uniformly random element from the current pool. Each element currently in the pool must have an equal probability of being returned.getRandomis only called when the pool is non-empty.
The pool contains no duplicates: inserting a value that is already present is a no-op (returning false).
Example
pool = RandomizedPool()
pool.insert(1) -> true (pool = {1})
pool.remove(2) -> false (2 not present; pool = {1})
pool.insert(2) -> true (pool = {1, 2})
pool.getRandom() -> 1 or 2, each with probability 1/2
pool.remove(1) -> true (pool = {2})
pool.insert(2) -> false (2 already present; pool = {2})
pool.getRandom() -> 2 (only element)
Constraints
-2^31 <= val <= 2^31 - 1- At most
2 * 10^5calls in total will be made acrossinsert,remove, andgetRandom getRandomis called only when the pool contains at least one element- All three operations must run in average O(1) time — solutions that take O(n) per operation (for example, scanning a list to remove an element, or converting a hash set to a list on every
getRandomcall) are not acceptable
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.