Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern

A heap is stored in a 0-indexed array. What is the index of the parent of the element at index 10, and of its left child?

  1. A. Parent 5, left child 21
  2. B. Parent 4, left child 21
  3. C. Parent 4, left child 20
  4. D. Parent 5, left child 20

Correct answer: B. Parent 4, left child 21

For 0-based heaps, parent(i) = ⌊(i − 1)/2⌋ = 4 and left(i) = 2i + 1 = 21. Parent 5 and child 20 are the 1-based formulas (i/2 and 2i) applied to the wrong indexing.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic