Data Structures & AlgorithmsHardInfosys · Accenture pattern

A min-heap of size k = 3 tracks the 3 largest values of the stream 5, 1, 9, 3, 7, 8, 2 (push each value; if the size exceeds 3, pop the minimum). What is at the root of the heap at the end?

  1. A. 9
  2. B. 8
  3. C. 7
  4. D. 2

Correct answer: C. 7

The heap ends holding the three largest values 9, 8, 7, and its root is the smallest of them, 7 – the 3rd largest overall. This is how a k-th largest query runs in O(n log k). Expecting 9 at the root confuses it with a max-heap.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic