Skip to content
Preptima

Heaps & Priority Queues interview questions

Top-K, streaming medians, and merges where partial ordering beats full sorting.

4 questions in Data Structures & Algorithms.

medium4

mediumCodingConcept

Numbers arrive one at a time and after each one you must answer what the Kth largest so far is. What do you keep, and what do you deliberately throw away?

Keep a min-heap holding exactly the K largest values seen. Its root is the answer, so the query is a read rather than a search, and every value smaller than that root is discarded the moment it arrives. What you give up is the ability to answer for any K larger than the one you chose, which is why the first thing to establish is whether K is fixed.

6 minmid, senior, staff