
Problem
Solution
Submissions
Insert, Delete, getRandom in O(1) Time
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 12
Write a C# program to design a data structure that supports the following operations in O(1) time: 1. Insert(val): Inserts an item val into the set if not already present. 2. Remove(val): Removes an item val from the set if present. 3. GetRandom(): Returns a random element from the current set of elements. Each element must have the same probability of being returned.
Example 1
- Input:
["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
[[], [1], [2], [2], [], [1], [2], []] - Output: [null, true, false, true, 2, true, true, 2]
- Explanation:
- RandomizedSet randomizedSet = new RandomizedSet();
- randomizedSet.insert(1); // Returns true because 1 was inserted successfully
- randomizedSet.remove(2); // Returns false because 2 does not exist in the set
- randomizedSet.insert(2); // Returns true because 2 was inserted successfully
- randomizedSet.getRandom(); // Returns 2 (randomly chosen from [1, 2])
- randomizedSet.remove(1); // Returns true because 1 was removed successfully
- randomizedSet.insert(2); // Returns false because 2 already exists in the set
- randomizedSet.getRandom(); // Returns 2 (the only number in the set)
Example 2
- Input:
["RandomizedSet", "insert", "insert", "remove", "insert", "remove", "getRandom"]
[[], [1], [2], [1], [3], [3], []] - Output: [null, true, true, true, true, true, 2]
- Explanation:
- RandomizedSet randomizedSet = new RandomizedSet();
- randomizedSet.insert(1); // Returns true because 1 was inserted successfully
- randomizedSet.insert(2); // Returns true because 2 was inserted successfully
- randomizedSet.remove(1); // Returns true because 1 was removed successfully
- randomizedSet.insert(3); // Returns true because 3 was inserted successfully
- randomizedSet.remove(3); // Returns true because 3 was removed successfully
- randomizedSet.getRandom(); // Returns 2 (the only number left in the set)
Constraints
- -2^31 ≤ val ≤ 2^31 - 1
- At most 2 * 10^5 calls will be made to insert, remove, and getRandom
- There will be at least one element in the data structure when getRandom is called
- Time Complexity: O(1) for all operations
- Space Complexity: O(n) where n is the number of elements stored
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use a combination of a hash map and an array/list to achieve O(1) time complexity for all operations
- The hash map should store the value and its index in the array for quick lookup
- For remove operation, swap the element to be removed with the last element in the array before removal
- For getRandom, use a random number generator to select a random index from the array
- Be careful with index updates in the hash map when removing elements