Data Structures & AlgorithmsHardAmazon · Microsoft OA

Design a set supporting insert, delete and getRandom (uniform) all in O(1) average time. Which design works?

  1. A. An array plus a value → index map; delete by swapping in the last element and popping
  2. B. A hash set alone; getRandom picks a random bucket
  3. C. A balanced BST; getRandom walks a random path
  4. D. A linked list plus a hash map value → node

Correct answer: A. An array plus a value → index map; delete by swapping in the last element and popping

The array allows a uniform random index in O(1), and swapping with the last element keeps it dense so deletion is O(1). Random buckets are not uniform (buckets differ in size or are empty), a BST walk is O(log n) and biased, and a list cannot index randomly.

More Coding Interview Problems questions

Exams that ask this topic