Insert Delete GetRandom O(1) - Duplicates allowed - Problem
Imagine you need to build a smart bag that can hold multiple copies of the same item and perform three lightning-fast operations:
- Insert: Add an item to the bag (even if it already exists)
- Remove: Take out one copy of a specific item (if it exists)
- Get Random: Pull out a completely random item, where items with more copies have higher chances of being selected
The challenge? Each operation must work in O(1) average time complexity!
Your task: Implement the RandomizedCollection class that supports duplicates (multiset) with these methods:
insert(val)- Returnstrueif val wasn't present before,falseif it was already thereremove(val)- Returnstrueif val was found and removed,falseif not foundgetRandom()- Returns a random element where probability is proportional to frequency
This is the advanced version of the classic RandomizedSet problem, now supporting duplicates!
Input & Output
example_1.py β Basic Operations
$
Input:
randomizedCollection = new RandomizedCollection();
randomizedCollection.insert(1); // return true since 1 is not present
randomizedCollection.insert(1); // return false since 1 is already present
randomizedCollection.insert(2); // return true since 2 is not present
randomizedCollection.getRandom(); // return 1 or 2 randomly
βΊ
Output:
[null, true, false, true, 1 or 2]
π‘ Note:
The collection starts empty. First insert(1) returns true as 1 wasn't present. Second insert(1) returns false as 1 was already there. insert(2) returns true as 2 is new. getRandom() can return either 1 or 2, with 1 having higher probability since it appears twice.
example_2.py β Remove Operations
$
Input:
randomizedCollection = new RandomizedCollection();
randomizedCollection.insert(1); // [1], return true
randomizedCollection.insert(1); // [1,1], return false
randomizedCollection.insert(2); // [1,1,2], return true
randomizedCollection.remove(1); // [1,2], return true
randomizedCollection.remove(1); // [2], return true
randomizedCollection.remove(1); // [2], return false
βΊ
Output:
[null, true, false, true, true, true, false]
π‘ Note:
After inserting 1,1,2 we have [1,1,2]. First remove(1) removes one copy, leaving [1,2]. Second remove(1) removes the last 1, leaving [2]. Third remove(1) returns false as no 1 exists.
example_3.py β Probability Distribution
$
Input:
randomizedCollection = new RandomizedCollection();
randomizedCollection.insert(1); // [1]
randomizedCollection.insert(1); // [1,1]
randomizedCollection.insert(1); // [1,1,1]
randomizedCollection.insert(2); // [1,1,1,2]
randomizedCollection.getRandom(); // should return 1 with 75% probability, 2 with 25%
βΊ
Output:
[null, true, false, false, true, 1 (75% chance) or 2 (25% chance)]
π‘ Note:
The collection contains [1,1,1,2]. Since 1 appears 3 times out of 4 total elements, getRandom() should return 1 with probability 3/4 = 75% and 2 with probability 1/4 = 25%.
Constraints
- -231 β€ val β€ 231 - 1
- At most 2 * 105 calls will be made to insert, remove, and getRandom
- getRandom will not be called if the collection is empty
- Each test case will have at least one call to insert before any call to getRandom
Visualization
Tap to expand
Understanding the Visualization
1
Setup
The machine has a tube to store balls in order and a catalog to track where each number's balls are located
2
Adding Balls
New balls are always added to the end of the tube, and their positions are recorded in the catalog
3
Removing Balls
To remove a ball, we swap it with the last ball in the tube, update the catalog, and remove the last position
4
Random Draw
Pick a random position in the tube and return that ball - higher frequency numbers have proportionally higher chances
Key Takeaway
π― Key Insight: By combining array indexing (for instant random access) with hash map position tracking (for instant removal), we achieve O(1) performance for all operations while maintaining proper probability distribution for duplicates!
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code