Data Structures & AlgorithmsHardAmazon · Microsoft OA

You must report the 100 largest values from a stream of 10⁹ integers using about 1 MB of memory. Which approach fits?

  1. A. Sort the whole stream, then take the last 100
  2. B. Keep a max-heap of all values seen so far
  3. C. A size-100 min-heap whose root is replaced by any larger arrival
  4. D. Use quickselect on the full stream

Correct answer: C. A size-100 min-heap whose root is replaced by any larger arrival

The heap's root is the smallest of the current top 100, so each new value needs one comparison and at most one O(log 100) replacement. Sorting, a full max-heap and quickselect all require storing the whole stream (4 GB).

More Coding Interview Problems questions

Exams that ask this topic