Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA

Problem: A leaderboard needs: insert a score, delete a score, and 'how many scores are below x?' – all online, 10⁵ operations, scores up to 10⁹. Which structure fits?

  1. A. A hash set – O(1) for all three
  2. B. An order-statistics balanced BST (or a Fenwick tree over coordinate-compressed scores if all scores are known in advance) – O(log n) each
  3. C. A sorted Python list with insort – O(log n) for all three
  4. D. A max-heap – O(log n) for all three

Correct answer: B. An order-statistics balanced BST (or a Fenwick tree over coordinate-compressed scores if all scores are known in advance) – O(log n) each

Rank queries need order plus subtree sizes. bisect.insort finds the position in O(log n) but shifting makes inserts and deletes O(n). A heap only knows its top element; a hash set has no order. Offline, compress the values and use a BIT.

More Coding Interview Problems questions

Exams that ask this topic