Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern

10, 4, 7, 1, 3 are inserted in that order into an empty binary min-heap (array form, sift-up after each insert). What is the array at the end?

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

Correct answer: B. 1 3 7 10 4

10; 4 swaps up → 4 10; 7 → 4 10 7; 1 goes to index 3, swaps with 10 then 4 → 1 4 7 10; 3 goes to index 4 (parent 4) and swaps once → 1 3 7 10 4. A heap is not a sorted array, so 1 3 4 7 10 is wrong.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic