← 返回 meta 的题目列表Randomized Container
类型:qbank
Implement a generic container with `insert(T element)` and `popRandom()`, where `popRandom` removes and returns one stored element with equal probability. The expected implementation keeps insertion and random removal at O(1).
Requirements
Implement a generic mutable container with two methods:
insert(T element): add the element to the container.
popRandom(): remove and return one currently stored element, giving every stored element an equal chance of selection.
Keep both insertion and random removal at O(1).
Notes
Duplicate-value semantics and behavior on an empty container are not specified. Clarify both before coding.
Uniform selection applies to the container's current state, so every removal must leave the indexing structure consistent for the next call.
Preparation
Implement the O(1) design with an indexed sequence plus value-to-position bookkeeping; rehearse updating the moved element's index when the selected slot is filled from the end.
State the representation invariant before coding and prove that the random choice remains uniform after any sequence of inserts and removals.
Test one element, repeated operations, removal until empty, and any agreed duplicate-value behavior.