Insert Delete GetRandom O(1) - Duplicates allowed - Problem

Design a data structure RandomizedCollection that supports inserting and removing values, as well as getting a random element from it. Duplicates are allowed in this collection.

Implement the RandomizedCollection class:

  • RandomizedCollection() - Initializes the empty RandomizedCollection object
  • bool insert(int val) - Inserts an item val into the multiset. Returns true if the item was not present, false otherwise
  • bool remove(int val) - Removes an item val from the multiset if present. Returns true if the item was present, false otherwise. If val has multiple occurrences, only remove one of them
  • int getRandom() - Returns a random element from the current multiset. The probability of each element being returned is linearly related to the number of the same values the multiset contains

All functions must work in average O(1) time complexity.

Input & Output

Example 1 — Basic Operations
$ Input: operations = ["RandomizedCollection", "insert", "insert", "insert", "getRandom", "remove", "getRandom"], values = [[], [1], [1], [2], [], [1], []]
Output: [null, true, false, true, 1, true, 2]
💡 Note: Initialize empty collection → insert(1) returns true (new) → insert(1) returns false (duplicate) → insert(2) returns true (new) → getRandom() could return 1 or 2 → remove(1) returns true (present) → getRandom() from remaining elements
Example 2 — Multiple Duplicates
$ Input: operations = ["RandomizedCollection", "insert", "insert", "insert", "getRandom"], values = [[], [4], [3], [4], []]
Output: [null, true, true, false, 4]
💡 Note: Insert 4 (new), insert 3 (new), insert 4 (duplicate). Collection has [4, 3, 4]. getRandom has 2/3 chance for 4, 1/3 chance for 3
Example 3 — Remove from Duplicates
$ Input: operations = ["RandomizedCollection", "insert", "insert", "remove", "getRandom"], values = [[], [10], [10], [10], []]
Output: [null, true, false, true, 10]
💡 Note: Insert 10 twice, then remove one occurrence. One 10 remains, so getRandom returns 10

Constraints

  • -231 ≤ val ≤ 231 - 1
  • At most 2 * 105 calls will be made to insert, remove, and getRandom
  • There will be at least one element in the data structure when getRandom is called

Visualization

Tap to expand
RandomizedCollection: Supporting Duplicates with O(1) OperationsOperations Sequenceinsert(1) → trueinsert(1) → falseinsert(2) → truegetRandom() → ?Internal State112idx 0idx 1idx 2Hash Map1 → {0,1}2 → {2}Array provides O(1) random accessHash map provides O(1) index lookupAll operations achieve O(1) average time complexity
Understanding the Visualization
1
Input
Sequence of insert/remove/getRandom operations
2
Process
Maintain array + hash map for O(1) operations
3
Output
Boolean results and random elements
Key Takeaway
🎯 Key Insight: Combine array's O(1) random access with hash map's O(1) lookup to handle duplicates efficiently
Asked in
Google 15 Facebook 12 Amazon 8 Microsoft 6
28.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen