Data Structures & AlgorithmsHardProduct-company placement
A 4-ary max-heap is stored in a 0-indexed array (children of i at 4i + 1 … 4i + 4): [56, 50, 20, 30, 4, 3, 9, 27, 5, 1]. After inserting 54 (append, then sift up), what is the array?
- A. [56, 50, 54, 30, 4, 3, 9, 27, 5, 1, 20]
- B. [56, 54, 20, 30, 50, 3, 9, 27, 5, 1, 4]
- C. [56, 50, 20, 30, 4, 3, 9, 27, 5, 1, 54]
- D. [56, 54, 20, 30, 4, 3, 9, 27, 5, 1, 50]
Correct answer: A. [56, 50, 54, 30, 4, 3, 9, 27, 5, 1, 20]
In a 4-ary heap the parent of i is ⌊(i − 1)/4⌋; 54 starts at index 10 and swaps up while it beats its parent. Using the binary parent formula ⌊(i − 1)/2⌋ is the trap.
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
- What does f(4) print? c void f(int n) { if (n <= 0) return; printf("%d", n); f(n - 2); printf("%d", n); f(n - 1); }
- What is the length of the longest BITONIC subsequence (strictly increasing then strictly decreasing; either part may be empty) of [12, 2…
- DFS on the directed graph with adjacency lists A: –; B: ACF; C: FG; D: E; E: BC; F: A; G: A starts at A (scanning lists in the order…
- All suffixes of the string 'radar' are inserted into a character trie (a suffix trie). How many nodes does it have, including the root?
- A tree on vertices 1–9 has edges 2–1, 3–1, 4–2, 5–3, 6–3, 7–6, 8–6, 9–6. What are its diameter and radius, and which vertex/vertices form…
- A binary search tree holds exactly the keys 20, 26, 28, 45, 49, 87, 91, 92. Which ONE of the following sequences can be its PREORDER…