Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: In a binary heap stored in an array with 1-based indexing, the array [1, 3, 2, 7, 4, 5, 6] is a valid min-heap. What is the sum of the values at indices whose children both exist?

  1. A. 22
  2. B. 28
  3. C. 3
  4. D. 6

Correct answer: D. 6

With 1-based indexing, i has both children iff 2i + 1 ≤ 7, so i = 1, 2, 3 holding 1, 3, 2: sum 6. Summing the leaves gives 22, every value gives 28, and 3 counts the nodes.

More Coding Interview Problems questions

Exams that ask this topic