Data Structures & AlgorithmsModerateWipro · Accenture pattern
The bottom-up build-heap procedure (sift down from the last internal node back to the root) turns [3, 1, 6, 5, 2, 4] into a max-heap. What is the result?
- A. 6 5 4 3 2 1
- B. 6 3 5 1 2 4
- C. 6 5 4 1 2 3
- D. 6 5 3 1 2 4
Correct answer: C. 6 5 4 1 2 3
Index 2 (6) already beats its child 4; index 1 (1) swaps with 5 → 3 5 6 1 2 4; the root 3 swaps with 6 and then with 4 → 6 5 4 1 2 3. Inserting one by one would give a different heap, and 6 5 4 3 2 1 is just sorted order.
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
- The maximum is deleted from the max-heap [50, 30, 40, 10, 20, 35]: the last element replaces the root and is sifted down. What is the heap…
- An undirected graph has edges A–B, A–C, B–D, C–D, C–E, D–F, E–F, and neighbours are always taken in alphabetical order. What is the DFS…
- An undirected graph has edges S–A, S–B, A–C, B–C, B–D, C–E, D–E, E–F. After BFS from S, which vertices are at level 2, and what is the…
- A directed graph has weighted edges A→B (4), A→C (1), C→B (2), B→D (1), C→D (5), D→E (3). Dijkstra's algorithm is run from A. What is the…
- An undirected graph has edges A–B (2), A–C (3), B–C (1), B–D (4), C–D (5), C–E (6), D–E (2). What is the total weight of its minimum…
- A DAG has edges A→B, A→C, B→D, C→D, D→E. How many different topological orderings does it have?