← All problems

Insert Delete GetRandom O(1)

Medium hash mapsarraysdesign LeetCode ↗
Design a set supporting insert, delete, and getRandom (uniform over current elements), all in amortized O(1) time. The delete operation is the tricky one.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(1) avg, Space O(n)

Common pitfalls

  • Linear delete scan
  • Stale indices after swap

Your solution

Log in to save your code and track progress.