IAE Academy

GATE 2023 CS Q.12: solution and answer

GATE 2023 · CSQ.12 · 1 mark · MCQ

Which one of the following sequences when stored in an array at locations forms a max-heap?

  1. (A)

    23, 17, 10, 6, 13, 14, 1, 5, 7, 12

  2. (B)

    23, 17, 14, 7, 13, 10, 1, 5, 6, 12

  3. (C)

    23, 17, 14, 6, 13, 10, 1, 5, 7, 15

  4. (D)

    23, 14, 17, 1, 10, 13, 16, 12, 7, 5

See the question in the paper

Answer and solution

ANSWER

Option B — 23, 17, 14, 7, 13, 10, 1, 5, 6, 12 — is the only sequence satisfying the max-heap property.

Official answer: BSource: Official answer key, GATE 2023 CS (IIT Kanpur)

  1. A max-heap stored 1-indexed in requires, for every node that has children, and (node 5 has only child index 10; nodes 6–10 are leaves since ).

  2. Option A: node 3, , has child . Since , the property fails — reject.

  3. Option B: check all internal nodes — ; ; ; ; . All pairs satisfy the property — B is a valid max-heap.

  4. Option C: node 5, , has child . Since , the property fails — reject.

  5. Option D: node 4, , has children . Since , the property fails — reject.

  6. Only option B satisfies the max-heap property everywhere.

KEY IDEA

In a 1-indexed array max-heap, node 's children sit at and , and the heap property requires every parent's value to be greater than or equal to both of its children's values, everywhere in the array — checking this level by level (or bottom-up from the last internal node) is enough to rule options in or out.

WATCH OUT FOR

Treating the array as 0-indexed (children at ) instead of 1-indexed, which shifts every parent-child pair and can make a genuine violation look fine, or a valid heap look broken.

From the GATE 2023 CS paper. Every question of this paper · Download the full paper · All 26 Trees & heaps questions, with notes

More Trees & heaps questions