Insert Delete GetRandom O(1) - Problem

Design a data structure that supports insert, delete, and getRandom operations, all in average O(1) time complexity.

You need to implement the RandomizedSet class with these methods:

  • RandomizedSet() - Initializes the data structure
  • bool insert(int val) - Inserts val into the set if not present. Returns true if inserted, false if already exists
  • bool remove(int val) - Removes val from the set if present. Returns true if removed, false if not found
  • int getRandom() - Returns a random element from the set with equal probability for each element

The challenge is achieving O(1) average time complexity for all operations while maintaining uniform random distribution.

Input & Output

example_1.py โ€” Basic Operations
$ Input: RandomizedSet rs = new RandomizedSet(); rs.insert(1); // return true rs.remove(2); // return false (not present) rs.insert(2); // return true rs.getRandom(); // return 1 or 2 (equal probability) rs.remove(1); // return true rs.insert(2); // return false (already present) rs.getRandom(); // return 2
โ€บ Output: [true, false, true, 1 or 2, true, false, 2]
๐Ÿ’ก Note: Insert 1 succeeds. Remove 2 fails as it doesn't exist. Insert 2 succeeds. GetRandom returns either 1 or 2. Remove 1 succeeds. Insert 2 fails as it already exists. GetRandom returns 2 (only element left).
example_2.py โ€” Single Element
$ Input: RandomizedSet rs = new RandomizedSet(); rs.insert(5); // return true rs.getRandom(); // return 5 rs.remove(5); // return true
โ€บ Output: [true, 5, true]
๐Ÿ’ก Note: With only one element, getRandom always returns that element. All operations work correctly with single element.
example_3.py โ€” Duplicate Operations
$ Input: RandomizedSet rs = new RandomizedSet(); rs.insert(10); // return true rs.insert(10); // return false (duplicate) rs.remove(10); // return true rs.remove(10); // return false (already removed)
โ€บ Output: [true, false, true, false]
๐Ÿ’ก Note: First insert succeeds. Second insert fails due to duplicate. First remove succeeds. Second remove fails as element no longer exists.

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
๐ŸŽฒ VIP Club Management System๐Ÿ“‹ Member Roster (Array)AliceSeat #0BobSeat #1CarolSeat #2DaveSeat #3๐ŸŽฏ Random winner picker - O(1)๐Ÿ“ž Member Directory (HashMap)Alice โ†’ 0Bob โ†’ 1Carol โ†’ 2Dave โ†’ 3๐Ÿ” Instant member lookup - O(1)๐Ÿš€ The Magic: Swap-with-Last Removal1. Bob wants to leave (position 1)2. Move Dave from last seat (3) to Bob's seat (1)3. Update directory: Dave โ†’ 1, remove Bob4. Shrink roster by removing last empty seatSwap!
Understanding the Visualization
1
New Member Joins
Check directory to see if they're already a member. If not, add their name to the end of the roster and record their position in the directory.
2
Member Leaves
Look up their position in the directory. Move the last person on the roster to fill their spot, update the directory, then remove the now-empty last position.
3
Pick Random Winner
Generate a random number within the roster size and announce the winner at that position. Every member has equal chances!
Key Takeaway
๐ŸŽฏ Key Insight: By combining array's instant random access with HashMap's instant lookups, and using the swap-with-last trick, we achieve O(1) for all operations while maintaining perfect randomness!
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
63.5K Views
High Frequency
~25 min Avg. Time
1.5K 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