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 structurebool insert(int val)- Insertsvalinto the set if not present. Returnstrueif inserted,falseif already existsbool remove(int val)- Removesvalfrom the set if present. Returnstrueif removed,falseif not foundint 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.
Visualization
Tap to expand
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!
Time & Space Complexity
Time Complexity
O(1)
Array operations and HashMap operations are both O(1) on average
โ Linear Growth
Space Complexity
O(n)
Array stores n elements, HashMap stores n key-value pairs
โก Linearithmic Space
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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code