Data Structures & AlgorithmsHardGATE CS · Codeforces Div2 B

Distinct integers are inserted in this order into an empty binary min-heap stored in a 0-indexed array (the children of index i are at 2i+1 and 2i+2, and a new element is sifted up while it is smaller than its parent): 19 9 37 10 38 46 20 88 23 79. What is the array after all insertions?

  1. A. 19 9 37 10 38 46 20 88 23 79
  2. B. 9 10 20 19 38 46 37 88 23 79
  3. C. 88 79 38 37 46 19 20 9 23 10
  4. D. 9 10 23 19 37 46 20 88 38 79

Correct answer: B. 9 10 20 19 38 46 37 88 23 79

Every insertion appends to the end and swaps upward past larger parents; the parent of index i is (i-1)//2. Skipping the sift-up, building a max-heap, or using i//2 as the parent gives the trap arrays.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic