Data Structures & AlgorithmsModerateCognizant · Capgemini pattern

From the max-heap [50, 30, 40, 10, 20, 35, 25], delete the maximum once. What is the array?

From the max-heap [50, 30, 40, 10, 20, 35, 25], delete the maximum once. What is the array?
import heapq
h = [-x for x in [50, 30, 40, 10, 20, 35, 25]]
heapq.heappop(h)
print([-x for x in h])
  1. A. [40, 30, 35, 10, 20, 25]
  2. B. [40, 30, 25, 10, 20, 35]
  3. C. [30, 40, 35, 10, 20, 25]
  4. D. [40, 35, 30, 25, 20, 10]

Correct answer: A. [40, 30, 35, 10, 20, 25]

Move the last element (25) to the root, then sift down by swapping with the larger child: 25 ↔ 40, then 25 ↔ 35 (children of index 2 are 35 only). Result [40, 30, 35, 10, 20, 25]. Each delete is O(log n).

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic