Question

A complete binary min-heap is made by including each integer in [1,1023][1, 1023] exactly once. The depth of a node in the heap is the length of the path from the root of the heap to that node. Thus, the root is at depth 0. The maximum depth at which integer 9 can appear is ________.

Official answer and solution
Official answer
8
From
Official GATE 2016 answer key

Solution

Find the maximum possible depth in a min-heap by maximizing the number of ancestors smaller than the target key.

A complete binary min-heap stores the integers [1,1023][1, 1023] with the root at depth 0.

In a min-heap, the key of every parent is strictly less than the keys of its children. Along any simple path from the root to a node at depth dd, the values must strictly increase:

key(root)<key(node1)<key(node2)<⋯<key(noded)\text{key}(\text{root}) < \text{key}(\text{node}_1) < \text{key}(\text{node}_2) < \dots < \text{key}(\text{node}_d)

If node 9 is located at depth dd, there must be exactly dd ancestor nodes on the path from the root to node 9, and every ancestor must have a value strictly less than 9.

The set of available integers in the heap strictly less than 9 is {1,2,3,4,5,6,7,8}\{1, 2, 3, 4, 5, 6, 7, 8\}, which contains exactly 8 values. Thus, the path can have at most 8 ancestors:

Path: 1→2→3→4→5→6→7→8→9\text{Path: } 1 \to 2 \to 3 \to 4 \to 5 \to 6 \to 7 \to 8 \to 9

This places 9 at depth 8.

Since N=1023=210−1N = 1023 = 2^{10} - 1, the tree is completely full up to depth 9 (containing 29=5122^9 = 512 leaves at depth 9 and 28=2562^8 = 256 nodes at depth 8). Therefore, depth 8 is well within the tree bounds and can be realized by placing the remaining 1014 larger elements in other branches.

Thus, the maximum depth at which integer 9 can appear is 8.

Final answer

8

Common trap

Confusing the maximum depth of the tree (9) with the maximum depth of key 9, or forgetting that the root is at depth 0.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2016 Computer Science and Information Technology (CS) Session 2
Question
44 of 65, printed as CS Q.34
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.