Data Structures & AlgorithmsVery hardAmazon/Microsoft SDE pattern
Build a binary min-heap in place from the array [35, 63, 89, 24, 71, 8, 16, 88, 2, 79] using bottom-up heapify (sift-down from the last internal node to the root). What is the resulting array?
- A. 89, 88, 35, 63, 79, 8, 16, 24, 2, 71
- B. 79, 2, 88, 16, 8, 71, 24, 89, 63, 35
- C. 2, 24, 8, 35, 71, 89, 16, 88, 63, 79
- D. 2, 8, 16, 24, 35, 63, 71, 79, 88, 89
Correct answer: C. 2, 24, 8, 35, 71, 89, 16, 88, 63, 79
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, 24, 8, 35, 71, 89, 16, 88, 63, 79]. Simply sorting the array, [2, 8, 16, 24, 35, 63, 71, 79, 88, 89], 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
- A binary tree has preorder traversal [52, 25, 42, 27, 64, 92, 78] and inorder traversal [25, 27, 42, 52, 64, 78, 92]. Reconstructing the…
- Keys 45, 55, 65 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- Keys 56, 48, 53, 36, 81, 29, 64 are inserted via separate chaining (h(k) = k mod 7, new keys appended to the end of the chain) into a…
- A binary tree has preorder traversal [72, 40, 18, 19, 54, 47, 92, 81] and inorder traversal [18, 19, 40, 47, 54, 72, 81, 92]…
- Build a binary min-heap in place from the array [37, 10, 58, 45, 19, 47, 86, 41] using bottom-up heapify (sift-down from the last internal…
- Build a binary min-heap in place from the array [34, 57, 50, 49, 69, 76, 15, 74] using bottom-up heapify (sift-down from the last internal…