Tutorialspoint
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
ArraysHash MapKPMGSamsung
Editorial

Login to view the detailed solution and explanation for this problem.

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.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

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

Steps to solve by this approach:

 Step 1: Create a list to store the elements and a dictionary to map values to their indices in the list.
 Step 2: For insertion, check if the value exists in the dictionary. If not, add it to both the list and dictionary.
 Step 3: For removal, find the index of the value in the list using the dictionary.
 Step 4: Swap the element to be removed with the last element in the list to maintain O(1) removal time.
 Step 5: Update the dictionary with the new index of the swapped element.
 Step 6: Remove the last element from the list and remove the value from the dictionary.
 Step 7: For getRandom, generate a random index within the list's range and return the element at that index.

Submitted Code :