Data Structures & AlgorithmsHardProduct-company placement

A binary min-heap is stored in a 0-indexed array [2, 19, 33, 58, 25, 35, 53]. Perform, in order: insert 36, insert 8, delete-min, delete-min. (Insert appends and sifts up; delete-min moves the last element to the root and sifts it down, swapping with the smaller child.) What is the final array?

  1. A. [19, 36, 33, 58, 25, 35, 53]
  2. B. [33, 36, 58, 19, 25, 35, 53]
  3. C. [19, 33, 25, 36, 35, 53, 58]
  4. D. [19, 25, 33, 36, 58, 35, 53]

Correct answer: D. [19, 25, 33, 36, 58, 35, 53]

Sift-down must compare BOTH children and keep going until the heap property holds; doing that at every step gives [19, 25, 33, 36, 58, 35, 53]. Swapping with the left child whenever it beats the parent, or stopping after one swap, leaves a different (often invalid) array.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic