Data Structures & AlgorithmsHardTCS NQT · Infosys pattern
Build a binary min-heap in place from the array [29, 45, 8, 5, 74, 90, 69, 56, 38] using bottom-up heapify (sift-down from the last internal node to the root). What is the resulting array?
- A. 38, 56, 69, 90, 74, 5, 8, 45, 29
- B. 90, 74, 69, 56, 45, 8, 29, 5, 38
- C. 5, 29, 8, 38, 74, 90, 69, 56, 45
- D. 5, 8, 29, 38, 45, 56, 69, 74, 90
Correct answer: C. 5, 29, 8, 38, 74, 90, 69, 56, 45
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 [5, 29, 8, 38, 74, 90, 69, 56, 45]. Simply sorting the array, [5, 8, 29, 38, 45, 56, 69, 74, 90], 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
- Keys 45, 92, 18, 65, 91, 86, 78, 95 are inserted in this order into an empty BST (no rebalancing). What is the height of the tree (edges…
- Keys 55, 50, 19, 76, 39, 98, 34, 63, 58, 69 are inserted via separate chaining (h(k) = k mod 8, new keys appended to the end of the chain)…
- A trie is built from the words {cat, card, den, cats, dog, do, cap}. Using the trie (walk the prefix's characters, then count all words in…
- Build a binary min-heap in place from the array [2, 63, 36, 74, 23, 20, 15, 22] using bottom-up heapify (sift-down from the last internal…
- 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…
- 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?