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): 25 55 47 52 2 56 29 57 18 68 14. What is the array after all insertions?

  1. A. 25 55 47 52 2 56 29 57 18 68 14
  2. B. 68 57 55 52 56 47 29 25 18 2 14
  3. C. 2 14 18 29 47 25 52 57 55 68 56
  4. D. 2 14 29 25 18 56 47 57 55 68 52

Correct answer: D. 2 14 29 25 18 56 47 57 55 68 52

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