Data Structures & AlgorithmsVery hardProduct Company SDE · Infosys OA
Problem: A max-heap starts with the values 9, 8, 6, 5, 4, 3, 1 (inserted in that order). Then three times in succession: pop the maximum, and push half of the popped value rounded down. After these three rounds, what is the sum of all values remaining in the heap?
- A. 35
- B. 25
- C. 24
- D. 13
Correct answer: C. 24
Round 1 pops 9 and pushes 4, round 2 pops 8 and pushes 4, round 3 pops 6 and pushes 3, leaving 5, 4, 4, 4, 3, 3, 1 = 24. A min-heap pops 1, 0 and 0 instead (35), rounding up pushes 5, 4, 3 (25), and popping without pushing back leaves 13.
More Coding Interview Problems questions
- Problem: The string "abcbaxyzzyxq" is given. What is the minimum number of characters that must be deleted so the remaining string is a…
- Problem: A string is formed by writing the letters of "kayak" then "level" then "rotor". How many substrings of length 5 of…
- Problem: Starting from the string "00000" of five zeros, operations flip a substring: each operation chooses positions l ≤ r and flips…
- Problem: The number of ways to choose 3 vertices from a graph's vertex set of size n = 12 such that no two chosen vertices are adjacent in…
- Problem: A stream of 12 numbers 5, 1, 4, 4, 2, 5, 3, 1, 2, 3, 3, 4 arrives. How many distinct values appear at least twice in the stream?
- Problem: Start with x = 5. Repeat this rule: if x is odd, set x = x + 3; if x is even, set x = 2x − 1. Count how many times the rule is…