GATE Computer ScienceVery hardGATE CS (PYQ pattern)
The distinct keys 1..8 are arranged as a max-heap in a 0-indexed array of size 8 (children of i at 2i+1, 2i+2). Over all valid max-heaps, in how many different array positions can the key 3 (the third smallest) appear?
- A. 3
- B. 5
- C. 6
- D. 4
Correct answer: C. 6
Brute force over all 210 valid heaps shows key 3 can sit in the positions [2, 3, 4, 5, 6, 7]: only keys 1 and 2 are smaller, so key 3 can have at most two descendants; that rules out the root and index 1 (which have more), leaving indices 2 to 7 (6 positions).
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- Heapsort (max-heap, in-place) is applied to [9, 4, 7, 1, 8, 2, 6, 3, 5, 10, 12, 11, 15, 14, 13]. First the array is made a max-heap with…
- Keys 50, 30, 70, 20, 40, 60, 80, 10, 25, 35, 45, 55, 65, 75, 85, 5, 12, 22, 27, 90 are inserted in this order into an empty BST (root at…
- How many structurally distinct binary trees (left and right children distinguished) with exactly 7 nodes have exactly 3 leaves?
- A full binary tree has every node with 0 or 2 children; left and right are distinguished. How many structurally different full binary…
- An undirected unweighted graph has edges 1-2, 1-3, 2-4, 2-5, 3-4, 3-6, 4-7, 5-7, 6-7, 6-8, 7-9, 8-9. Using BFS from vertex 1, what is the…
- A directed graph on vertices 1..4 has edges 1->2, 1->3, 2->3, 2->4, 3->1, 3->4, 4->1, 4->2. How many walks of length exactly 4 (edges…