Tutorialspoint
Problem
Solution
Submissions

Insert Delete GetRandom O(1)

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

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

  1. insert(val): Inserts an item val into the collection.
  2. remove(val): Removes an item val from the collection if present.
  3. getRandom(): Returns a random element from the current collection of elements. Each element must have the same probability of being returned.

Note that duplicates are allowed, which means you need to handle multiple copies of the same value in the collection.

Example 1
  • // Initialize an empty collection.
    RandomizedCollection collection = new RandomizedCollection();

  • // Inserts 1 to the collection. Returns true as the collection did not contain 1.
    collection.insert(1); // return true

  • // Inserts another 1 to the collection. Returns false as the collection contained 1.
    // Collection now contains [1,1].
    collection.insert(1); // return false

  • // Inserts 2 to the collection. Returns true as the collection did not contain 2.
    // Collection now contains [1,1,2].
    collection.insert(2); // return true

  • // getRandom should return 1 with the probability 2/3, and returns 2 with the probability 1/3.
    collection.getRandom();

  • // Removes 1 from the collection, returns true. Collection now contains [1,2].
    collection.remove(1); // return true

  • // getRandom should return 1 and 2 both with a probability of 1/2.
    collection.getRandom();

  • // Removes 2 from the collection, returns true. Collection now contains [1].
    collection.remove(2); // return true

  • // getRandom should return 1 with probability 1.
    collection.getRandom();
Example 2
  • // Initialize an empty collection.
    RandomizedCollection collection = new RandomizedCollection();

  • // Inserts 1 to the collection. Returns true as the collection did not contain 1.
    collection.insert(1); // return true

  • // Inserts 2 to the collection. Returns true as the collection did not contain 2.
    // Collection now contains [1,2].
    collection.insert(2); // return true

  • // getRandom should return 1 and 2 both with a probability of 1/2.
    collection.getRandom();

  • // Removes 1 from the collection, returns true. Collection now contains [2].
    collection.remove(1); // return true

  • // Inserts 3 to the collection. Returns true as the collection did not contain 3.
    // Collection now contains [2,3].
    collection.insert(3); // return true

  • // getRandom should return 2 and 3 both with a probability of 1/2.
    collection.getRandom();
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 for all operations should be O(1) on average
  • Space Complexity: O(n) where n is the number of elements
ArraysHash MapTech MahindraEY
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 an ArrayList to store elements for O(1) access by index.
  • Use a HashMap to map values to their indices in the ArrayList for O(1) lookup.
  • Since duplicates are allowed, each value in the HashMap needs to map to a set of indices.
  • When removing, swap the element to be removed with the last element in the ArrayList.
  • When getting a random element, use a random index in the ArrayList.

Steps to solve by this approach:

 Step 1: Create an ArrayList to store all elements, which allows for O(1) access by index and random selection.

 Step 2: Create a HashMap that maps each value to a set of indices where the value is stored in the ArrayList.
 Step 3: For insertion, add the new value to the end of the ArrayList and add its index to the set in the HashMap.
 Step 4: For removal, find one index where the value is stored, remove that index from the set in the HashMap.
 Step 5: Swap the element to be removed with the last element in the ArrayList to maintain O(1) deletion.
 Step 6: Update the index of the last element that was moved to the position of the removed element.
 Step 7: For random access, simply return a random element from the ArrayList using a random index.

Submitted Code :