Data Structures & AlgorithmsVery hardProduct-company placement

A min-heap holds 31 distinct keys in a 0-indexed array (a full tree with 5 levels). At how many different indices can the 3rd-smallest key possibly be?

  1. A. 2
  2. B. 7
  3. C. 4
  4. D. 6

Correct answer: D. 6

The 3rd-smallest key has at most 2 smaller keys, so all its ancestors come from them: it sits at depth 1 to 2, and every such slot is reachable. That is 2 + 4 + … + 2^2 = 6 positions (never the root).

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic