Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE
Problem: In a binary max-heap stored in an array with 1-based indexing, the array is [40, 30, 35, 10, 20, 25, 28, 5, 8, 12]. An operation sets the value at position 10 to 45 and then sifts it up. Which position (1-based) does the value 45 end at?
- A. 2
- B. 0
- C. 1
- D. 3
Correct answer: C. 1
Sift-up swaps the value with its parent while it is larger, climbing the implicit tree (parent of i is i//2). Forgetting the parent index convention gives a wrong final position.
More Coding Interview Problems questions
- Problem: In the array [3, 8, 1, 9, 4, 7, 2, 6, 5], what is the length of the longest subarray whose sum is exactly 20?
- Problem: A sorted list 2, 4, 6, 8, 10, 12, 14 is treated as a rotated array: the first 3 values are moved to the end, giving 8, 10, 12…
- Problem: Four processes arrive at times 0, 2, 4, 6 with burst times 6, 2, 8, 3. The scheduler is shortest-remaining-time-first…
- Problem: In the grid of 0s and 1s below, count the number of 2×2 all-ones squares. 1110 1111 0111 1111
- Problem: A string s = "abacaba" is given. How many of its substrings are palindromes of odd length (counting index ranges, so repeated…
- Problem: A directed graph has 4 vertices (0–3) and arcs with weights: 0→1 (4), 1→2 (−3), 0→2 (2), 2→3 (1), 1→3 (5), 3→1 (−1). Start with…