Code RoomRandom set
MediumPrep Room Coding #4519

Random set

CodingAlgorithms & data structuresMid–Senior~25 min

Design a set supporting insert(x), remove(x), and get_random() each in average O(1). Since randomness is hard to test deterministically, instead of true randomness, get_random must return the element currently stored at internal index (call_counter % current_size) where call_counter starts at 0 and increments on each get_random call. insert returns 1 if x was newly added (0 if already present); remove returns 1 if x was present and removed (0 otherwise). Replay ["insert", x], ["remove", x], ["getRandom"] and return the list of all results in order.

Implement
random_set(ops: list[list]) → list[int]
Examples
in[[["insert",1],["remove",2],["insert",2],["getRandom"],["remove",1],["insert",2],["getRandom"]]]out[1,0,1,1,1,0,2]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 25 min
InputExpectedGot
[[["insert",1],["remove",2],["insert",2],["getRandom"],["remove",1],["insert",2],["getRandom"]]][1,0,1,1,1,0,2]not run yetsample