...
/DIY: Insert Delete GetRandom O(1) - Duplicates Allowed
DIY: Insert Delete GetRandom O(1) - Duplicates Allowed
Solve the interview question "Insert Delete GetRandom O(1) - Duplicates Allowed" in this lesson.
We'll cover the following...
Problem statement
Implement a set data structure that allows duplicates and can perform the following operations:
The
obj
parameter being passed as an argument in theinsert()
,remove()
,andget_random()
methods refers to the struct object.
insert(obj, data)
: This function should insertdata
into the set (if the set does not contain it already). It should returnfalse
if thedata
already exists in the set. Otherwise, it should returntrue
.remove(obj, data)
: If thedata
is present, this function should removedata
from the set and returntrue
. If thedata
does not exist in the set, the function should returnfalse
.get_random(obj)
: This function should return a random element from the set in constant time.
Note: Your implementation should aim for constant ...