Data Structures & AlgorithmsHardGATE CS
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, 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. [11, 15, 47, 28, 19, 56, 48, 32, 38]
- B. [15, 28, 11, 32, 19, 47, 48, 56, 38]
- C. [11, 15, 56, 28, 19, 47, 48, 32, 38]
- D. [11, 15, 28, 32, 19, 48, 47, 38, 56]
Correct answer: A. [11, 15, 47, 28, 19, 56, 48, 32, 38]
Sift-down must compare BOTH children and keep going until the heap property holds; doing that at every step gives [11, 15, 47, 28, 19, 56, 48, 32, 38]. 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
- 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?
- A BST holds integer keys between 1 and 100. Which sequence CANNOT be the sequence of keys examined while searching for 50?
- A directed graph on A–H has edges A→G, A→H, B→D, B→E, B→H, F→A, F→D, F→E, G→B, H→C, H→D. How many strongly connected components does it…
- Symbols occur with frequencies 7, 19, 20, 25, 26, 39. How many bits does an optimal (Huffman) prefix code need for the whole message, and…
- A rod of length 8 can be cut into integer pieces; a piece of length i sells for p(i) with p(1..8) = 2, 4, 15, 18, 19, 23, 24, 29. What is…