Data Structures & AlgorithmsVery hardIBM · Deloitte pattern
Build a binary min-heap in place from the array [47, 72, 84, 82, 36, 61, 51, 7, 2] using bottom-up heapify (sift-down from the last internal node to the root). What is the resulting array?
- A. 2, 7, 51, 61, 36, 82, 84, 72, 47
- B. 2, 7, 36, 47, 51, 61, 72, 82, 84
- C. 2, 7, 51, 47, 36, 61, 84, 72, 82
- D. 84, 82, 61, 72, 36, 47, 51, 7, 2
Correct answer: C. 2, 7, 51, 47, 36, 61, 84, 72, 82
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 [2, 7, 51, 47, 36, 61, 84, 72, 82]. Simply sorting the array, [2, 7, 36, 47, 51, 61, 72, 82, 84], 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
- Undirected graph edges: E–C, C–A, B–E, B–C, A–B, A–E, C–D. Using a BFS 2-colouring, is this graph bipartite?
- A trie is built from the words {cat, card, dot, den, deed, dog}. Using the trie (walk the prefix's characters, then count all words in the…
- An undirected graph has adjacency lists (neighbours visited in alphabetical order) A: [B, C, G]; B: [A, E, G]; C: [A, G]; D: [F, G]; E…
- A disjoint-set (union-find with union by rank and path compression) over 7 elements (0 to 6), each initially its own component, runs this…
- Keys 55, 65, 75 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- Keys 4, 49, 97, 15, 7, 96, 24, 50 are inserted in this order into an empty BST. Using the BST-specific LCA algorithm (walk down from the…