← Questions
Insert, delete and get random in O(1) with duplicates
0
I was asked this
No reports yet. Sign in to say where you were asked it.
Design a collection of integers that allows duplicates and supports, each in average O(1) time:
-
insert(val): add one copy ofval; return whethervalwas absent before. -
remove(val): remove one copy ofvalif present; return whether anything was removed. -
get_random(): return a random element, where each value's chance is proportional to how many copies of it the collection holds.
Explain the array-plus-index-map layout and how you remove from the middle of the array in constant time.
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
