Data Structures & AlgorithmsModerateTCS NQT · Product Company SDE
Problem: The array [4, 2, 5, 1, 3] is turned into a max-heap by sift-down from the last parent to the root (standard heapify, 0-indexed). What is the value at index 0 afterwards?
- A. 6
- B. 4
- C. 7
- D. 5
Correct answer: D. 5
Heapify sifts each internal node down from the last internal node up to the root, so the maximum rises to index 0. Building the heap by inserting elements one at a time gives the same root but via a different process.
More Coding Interview Problems questions
- Problem: A string is "racecar" and we ask: how many substrings of length 2 or 3 are palindromes (counted by index ranges)?
- Problem: The array [6, 1, 9, 4, 4, 8] is scanned, and for each index the number of earlier elements that are strictly smaller is recorded…
- Problem: The array [1, 5, 2, 6, 3, 7, 4, 8] is split into two halves, [1, 5, 2, 6] and [3, 7, 4, 8]. The result is formed by interleaving…
- Problem: How many integers from 1 to 1000 are perfect squares or perfect cubes (counted once even if both)?
- Problem: An undirected graph has edges 0-1, 1-2, 2-3, 3-4, 4-0, 1-5, 5-6, 6-2. What is the length of the shortest cycle that passes…
- Problem: In a directed graph with arcs 0→1, 1→2, 2→0, 2→3, 3→4, 4→3, the strongly connected components are formed. What is the size of the…