Data Structures & AlgorithmsHardAmazon/Microsoft SDE pattern
Build a binary min-heap in place from the array [50, 42, 62, 89, 76, 45, 3, 82] using bottom-up heapify (sift-down from the last internal node to the root). What is the resulting array?
- A. 3, 42, 45, 50, 62, 76, 82, 89
- B. 82, 3, 45, 76, 89, 62, 42, 50
- C. 89, 82, 62, 50, 76, 45, 3, 42
- D. 3, 42, 45, 82, 76, 50, 62, 89
Correct answer: D. 3, 42, 45, 82, 76, 50, 62, 89
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 [3, 42, 45, 82, 76, 50, 62, 89]. Simply sorting the array, [3, 42, 45, 50, 62, 76, 82, 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 directed graph has edges C→B, B→D, A→D, C→A, C→D, A→C. Using 3-colour DFS (white/grey/black, a grey-to-grey back edge means a cycle)…
- Directed edges: A→B (6), A→C (6), C→B (-3), B→D (1). Dijkstra's algorithm, which finalises each vertex once when it is removed from the…
- A directed graph has edges D→B, C→B, B→A, D→C, B→C. Using 3-colour DFS (white/grey/black, a grey-to-grey back edge means a cycle), does…
- Directed edges: A→B (5), A→C (6), C→B (-3), B→D (3). Dijkstra's algorithm, which finalises each vertex once when it is removed from the…
- Keys 34, 69, 39, 53, 61, 76, 35 are inserted in this order into an empty BST (no rebalancing). What is the height of the tree (edges on…
- An undirected graph has adjacency lists (neighbours visited in alphabetical order) A: [C, E, F]; B: [C, E]; C: [A, B, F]; D: [F]; E: [A…