Data Structures & AlgorithmsHardTCS NQT · Infosys pattern

Build a binary min-heap in place from the array [29, 45, 8, 5, 74, 90, 69, 56, 38] using bottom-up heapify (sift-down from the last internal node to the root). What is the resulting array?

  1. A. 38, 56, 69, 90, 74, 5, 8, 45, 29
  2. B. 90, 74, 69, 56, 45, 8, 29, 5, 38
  3. C. 5, 29, 8, 38, 74, 90, 69, 56, 45
  4. D. 5, 8, 29, 38, 45, 56, 69, 74, 90

Correct answer: C. 5, 29, 8, 38, 74, 90, 69, 56, 45

Bottom-up heapify sifts down every internal node starting from index ⌊n/2⌋ − 1 up to the root, swapping a node with its smaller child until the min-heap property holds; this gives [5, 29, 8, 38, 74, 90, 69, 56, 45]. Simply sorting the array, [5, 8, 29, 38, 45, 56, 69, 74, 90], is not what heapify produces — a heap is only partially ordered.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic