GATE Computer ScienceHardGATE CS (PYQ pattern)
The array [5, 3, 17, 10, 84, 19, 6, 22, 9, 15, 2] is turned into a max-heap by Floyd's linear-time Build-Max-Heap. Which array results? (Inserting the elements one by one into an empty heap would give a different array.)
- A. [84, 22, 19, 17, 15, 5, 6, 3, 9, 10, 2]
- B. [84, 22, 19, 10, 15, 17, 6, 5, 9, 3, 2]
- C. [84, 22, 19, 10, 15, 3, 6, 5, 9, 17, 2]
- D. [84, 22, 17, 19, 15, 10, 6, 5, 9, 3, 2]
Correct answer: B. [84, 22, 19, 10, 15, 17, 6, 5, 9, 3, 2]
Sift down from node 5 to node 1 (1-indexed); the root 5 ends up deep after 84 and 22 rise, and the run (checked by simulation) gives [84, 22, 19, 10, 15, 17, 6, 5, 9, 3, 2]. The first distractor is what repeated insertion produces.
More Algorithms questions
- Insertion sort on [7, 3, 9, 1, 8, 2, 6, 5, 4] scans left-to-right from position j = i − 1, comparing a[j] with the key until a[j] ≤ key or…
- Lomuto partition (pivot = last element, loop j from lo to hi−1 comparing a[j] ≤ pivot) is used in quicksort on [5, 2, 8, 1, 9, 3, 7, 4…
- A randomised quicksort on a random permutation of n distinct keys picks a pivot uniformly and every partition of m elements costs exactly…
- Binary search on a sorted array of 20 distinct keys uses mid = ⌊(lo + hi)/2⌋ and one three-way comparison per probe, stopping when lo >…
- A base-3 (ternary) counter starts at 0 and is incremented 1000 times. The cost of an increment is the number of digits that change…
- A dynamic array starts with capacity 2. When it is full and an element is appended, the capacity becomes ⌈1.5 × capacity⌉ and all current…