Insert Delete GetRandom O(1) - Problem

Design a data structure that supports all following operations in average O(1) time.

Implement the RandomizedSet class:

  • RandomizedSet() Initializes the RandomizedSet object.
  • bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.
  • bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.
  • int getRandom() Returns a random element from the current set of elements (it's guaranteed that at least one element exists when this method is called). Each element must have the same probability of being returned.

You must implement the functions of the class such that each function works in average O(1) time complexity.

Input & Output

Example 1 — Basic Operations
$ Input: ["RandomizedSet","insert","remove","insert","getRandom","remove","insert","getRandom"] [[], [1], [2], [2], [], [1], [2], []]
Output: [null, true, false, true, 2, true, false, 2]
💡 Note: RandomizedSet() initializes empty set. insert(1) adds 1, returns true. remove(2) tries to remove non-existent 2, returns false. insert(2) adds 2, returns true. getRandom() returns 2 (only element). remove(1) removes 1, returns true. insert(2) tries to add existing 2, returns false. getRandom() returns 2.
Example 2 — Multiple Elements
$ Input: ["RandomizedSet","insert","insert","insert","getRandom","getRandom"] [[], [1], [2], [3], [], []]
Output: [null, true, true, true, 1, 2]
💡 Note: Insert three elements [1,2,3]. Each getRandom() call returns one of these elements with equal probability.
Example 3 — Edge Case Single Element
$ Input: ["RandomizedSet","insert","getRandom","remove"] [[], [5], [], [5]]
Output: [null, true, 5, true]
💡 Note: Insert single element 5. getRandom() must return 5 (only choice). Remove 5 successfully.

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
RandomizedSet: O(1) Insert, Remove, GetRandomArray OnlygetRandom: O(1)insert/remove: O(n)HashMap Onlyinsert/remove: O(1)getRandom: O(n)Array + HashMapAll operations: O(1)✓ Optimal SolutionNeed O(1) randomNeed O(1) insert/removeCombined SolutionArray: [val1, val2, val3] + HashMap: {val1→0, val2→1, val3→2}Remove: Swap with last element, update HashMap, pop lastResult: O(1) All Operations!
Understanding the Visualization
1
Requirements
Need O(1) insert, remove, and getRandom operations
2
Challenge
Array good for random, HashMap good for insert/remove
3
Solution
Combine both: Array + HashMap with swap technique
Key Takeaway
🎯 Key Insight: Combine array's O(1) random access with HashMap's O(1) lookup using swap-with-last removal technique
Asked in
Google 45 Amazon 38 Facebook 32 Microsoft 28
89.0K Views
High Frequency
~25 min Avg. Time
2.8K 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