Data Structures & AlgorithmsVery hardProduct-company placement
Build a binary min-heap in place from the array [32, 59, 35, 72, 53, 57, 16, 42, 15] using bottom-up heapify (sift-down from the last internal node to the root). What is the resulting array?
- A. 15, 16, 32, 35, 42, 53, 57, 59, 72
- B. 72, 59, 57, 42, 53, 35, 16, 32, 15
- C. 15, 32, 16, 42, 53, 57, 35, 59, 72
- D. 15, 42, 16, 57, 53, 72, 35, 59, 32
Correct answer: C. 15, 32, 16, 42, 53, 57, 35, 59, 72
Bottom-up heapify sifts down every internal node starting from index ⌊n/2⌋ − 1 up to the root, swapping a node with its smaller child until the min-heap property holds; this gives [15, 32, 16, 42, 53, 57, 35, 59, 72]. Simply sorting the array, [15, 16, 32, 35, 42, 53, 57, 59, 72], is not what heapify produces — a heap is only partially ordered.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Keys 47, 33, 29, 18, 55, 67 are inserted via separate chaining (h(k) = k mod 7, new keys appended to the end of the chain) into a table of…
- Undirected graph edges: A–B, C–B, A–D, D–B, C–D, C–A. Using a BFS 2-colouring, is this graph bipartite?
- A disjoint-set (union-find with union by rank and path compression) over 9 elements (0 to 8), each initially its own component, runs this…
- Directed edges: A→B (5), A→C (6), C→B (-3), B→D (1). Dijkstra's algorithm, which finalises each vertex once when it is removed from the…
- An undirected connected graph has edges A–B, B–C, C–D, D–E, E–F, F–G, G–H, D–F, F–H. How many articulation points (cut vertices) and how…
- Keys 14, 21, 49, 22, 15, 16, 33, 66 are inserted via separate chaining (h(k) = k mod 7, new keys appended to the end of the chain) into a…