This question evaluates a candidate's ability to design and analyze dynamic data structures that support weighted random sampling with inserts and deletes, testing skills in algorithm design, complexity analysis, and randomized sampling under large numeric constraints.
Design a data structure that maintains a dynamic set of items, each with a positive integer weight, and supports:
insert(id, weight)
id
with the given
weight
.
id
already exists, you may either reject or treat it as an update (state your choice).
delete(id)
sample() -> id
id
chosen randomly such that:
sample()
should be unbiased given an ideal RNG.
randInt(1, totalWeight)
that returns a uniform integer in that range.