🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Heaps and Heap Sort Internals: all 15 questions

Quiz: Heaps and Heap Sort Internals · Question 9 of 15

To find the 10 largest values in a stream of a million numbers, the best heap approach is:

To find the 10 largest values in a stream of a million numbers, the best heap approach is:

  1. A.A max-heap of all one million numbers
  2. B.A min-heap that never holds more than 10 numbers
  3. C.A max-heap of size 10
  4. D.Sorting the numbers after they all arrive
Answer & explanation

Correct answer: A min-heap that never holds more than 10 numbers

The min-heap's root is the weakest of the current top 10, so each new number is compared with it: O(n log k) time and O(k) memory.

Can you get the rest right without peeking?

Take the timed Quiz: Heaps and Heap Sort Internals quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →