Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: In a binary heap stored in an array with 1-based indexing, the array [1, 3, 2, 7, 4, 5, 6] is a valid min-heap. What is the sum of the values at indices whose children both exist?
- A. 22
- B. 28
- C. 3
- D. 6
Correct answer: D. 6
With 1-based indexing, i has both children iff 2i + 1 ≤ 7, so i = 1, 2, 3 holding 1, 3, 2: sum 6. Summing the leaves gives 22, every value gives 28, and 3 counts the nodes.
More Coding Interview Problems questions
- Problem: Vertices 0–5 form a graph with edges 0-1, 1-2, 2-3, 3-0 and 4-5. A BFS from vertex 0 gives each vertex a distance. What is the…
- Problem: A stack-sortable check is applied to the permutation [3, 1, 2]: elements are pushed in order and popped whenever the top equals…
- Problem: The array [5, 2, 8, 2, 9, 1, 5, 5] contains repeated values. How many distinct values appear exactly twice?
- Problem: What is the maximum sum of a subsequence of [4, −1, 2, 1] with no two adjacent elements chosen (the empty selection is not…
- Problem: The string "ABCABCABC" is read and each letter is replaced by its position in the alphabet (A=1, B=2, C=3). What is the sum of…
- Problem: From the list [12, 18, 24, 30, 36], what is the number of distinct prime divisors of the product of all five numbers?