Data Structures & AlgorithmsHardProduct-company placement
A binary min-heap is stored in a 0-indexed array [2, 19, 33, 58, 25, 35, 53]. Perform, in order: insert 36, insert 8, delete-min, delete-min. (Insert appends and sifts up; delete-min moves the last element to the root and sifts it down, swapping with the smaller child.) What is the final array?
- A. [19, 36, 33, 58, 25, 35, 53]
- B. [33, 36, 58, 19, 25, 35, 53]
- C. [19, 33, 25, 36, 35, 53, 58]
- D. [19, 25, 33, 36, 58, 35, 53]
Correct answer: D. [19, 25, 33, 36, 58, 35, 53]
Sift-down must compare BOTH children and keep going until the heap property holds; doing that at every step gives [19, 25, 33, 36, 58, 35, 53]. Swapping with the left child whenever it beats the parent, or stopping after one swap, leaves a different (often invalid) array.
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 78, 77, 48, 42, 39, 27, 67, 23, 57 are inserted in that order into an empty AVL tree. What is the preorder traversal of the final tree?
- Lectures occupy half-open intervals [start, end): [0, 3), [0, 7), [3, 7), [4, 12), [8, 12), [10, 16), [12, 18), [19, 27). A lecture ending…
- Kruskal's algorithm runs on the weighted undirected graph A–D (13), A–F (22), B–F (5), B–G (19), C–D (12), C–G (17), D–F (27), D–G (20)…
- A binary min-heap is stored in a 0-indexed array [4, 15, 11, 32, 56, 47, 48]. Perform, in order: insert 28, insert 38, insert 19…
- A directed graph on A–H has edges A→C, A→D, A→H, B→G, B→H, D→E, E→A, E→B, G→F, H→C, H→E. How many strongly connected components does it…
- How many topological orderings does the DAG on vertices A–F with edges C→A, C→B, C→E, F→B, F→C, F→D have?