Data Structures & AlgorithmsVery hardGATE CS · ICPC regional · Product Company SDE

Keys 17, 90, 39, 58, 63, 18, 37, 95 are inserted one at a time into an initially empty max-heap (array representation, sift-up). What is the final array?

  1. A. [95, 90, 39, 63, 58, 18, 37, 17]
  2. B. [95, 90, 63, 58, 39, 37, 18, 17]
  3. C. [95, 90, 39, 58, 63, 18, 37, 17]
  4. D. [17, 37, 18, 58, 63, 39, 90, 95]

Correct answer: A. [95, 90, 39, 63, 58, 18, 37, 17]

Insert each key at the end and swap with its parent while larger. Result [95, 90, 39, 63, 58, 18, 37, 17]. Building by Floyd's bottom-up method may give a different (also valid) heap.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic