Data Structures & AlgorithmsHardProduct-company placement

A 4-ary max-heap is stored in a 0-indexed array (children of i at 4i + 1 … 4i + 4): [56, 50, 20, 30, 4, 3, 9, 27, 5, 1]. After inserting 54 (append, then sift up), what is the array?

  1. A. [56, 50, 54, 30, 4, 3, 9, 27, 5, 1, 20]
  2. B. [56, 54, 20, 30, 50, 3, 9, 27, 5, 1, 4]
  3. C. [56, 50, 20, 30, 4, 3, 9, 27, 5, 1, 54]
  4. D. [56, 54, 20, 30, 4, 3, 9, 27, 5, 1, 50]

Correct answer: A. [56, 50, 54, 30, 4, 3, 9, 27, 5, 1, 20]

In a 4-ary heap the parent of i is ⌊(i − 1)/4⌋; 54 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