Data Structures & AlgorithmsHardProduct-company placement

A 3-ary max-heap is stored in a 0-indexed array (children of i at 3i + 1 … 3i + 3): [51, 43, 34, 20, 27, 18, 3, 8, 17, 10]. After inserting 59 (append, then sift up), what is the array?

  1. A. [59, 51, 34, 20, 43, 18, 3, 8, 17, 10, 27]
  2. B. [51, 43, 34, 20, 27, 18, 3, 8, 17, 10, 59]
  3. C. [59, 43, 51, 20, 27, 18, 3, 8, 17, 10, 34]
  4. D. [59, 43, 34, 51, 27, 18, 3, 8, 17, 10, 20]

Correct answer: D. [59, 43, 34, 51, 27, 18, 3, 8, 17, 10, 20]

In a 3-ary heap the parent of i is ⌊(i − 1)/3⌋; 59 starts at index 10 and swaps up while it beats its parent. Using the binary parent formula ⌊(i − 1)/2⌋ is the trap.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic