Quant Memo
Coding/●●●●●

Insert, delete, and get-random in O(1)

Asked at Citadel

Design a data structure (think: a live universe of tradable symbols you must sometimes sample uniformly at random) supporting:

  • insert(val), add val, return True if it was not already present;
  • remove(val), delete val, return True if it was present;
  • get_random(), return a uniformly random current element.

All three must run in O(1)O(1) average time.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions