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
Insert Delete GetRandom O(1) INPUT Operations: RandomizedSet() insert(1) remove(2) insert(2) getRandom() remove(1) insert(2) getRandom() Hash Set Structure: HashSet ALGORITHM STEPS 1 Insert Operation Add to set if not exists Return true/false 2 Remove Operation Remove from set if exists Return true/false 3 GetRandom Convert set to list Pick random index 4 O(1) Average HashSet provides O(1) for insert/remove Set State After insert(1), insert(2): 1 2 After remove(1): 2 FINAL RESULT Output Array: null true false true 2 true false 2 Explanation: 1. init: null 2. insert(1): true (added) 3. remove(2): false (not in) 4. insert(2): true (added) 5. getRandom(): 2 (random) 6. remove(1): true (removed) 7. insert(2): false (exists) 8. getRandom(): 2 (only elem) OK - All Pass Key Insight: HashSet provides O(1) average time for insert and remove operations. For getRandom, converting to list takes O(n), but using ArrayList + HashMap together enables true O(1) for all operations. The trick: swap element with last before removal to maintain O(1) random access. TutorialsPoint - Insert Delete GetRandom O(1) | Hash Set Only Approach
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