Back to problems

Insert Delete GetRandom O(1)

Object-Oriented Programming · Apple · Medium

Constant-Time Insert, Delete, and Random Selection Create the RandomizedSet class with these operations: RandomizedSet() constructs an empty set. bool insert(int val) adds val only when it is not already stored, returning true for a new insertion and false when the value is already present. bool remove(int val) deletes val when it exists, returning true if removal occurs and false otherwise. int getRandom() chooses and returns one value from the set. The method is called…

Checking your access…