Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon OA
Problem: Two heaps are maintained over the stream 5, 15, 1, 3, 8, 7, 9, 10, 6, 11 to report the median after each element (lower median for even counts). What is the sum of the medians reported after each arrival?
- A. 72
- B. 56
- C. 64
- D. 51
Correct answer: B. 56
Lower medians after each arrival: 5, 5, 5, 3, 5, 5, 7, 7, 7, 7, summing to 56. Using the upper median for even counts gives 72, averaging the two middle values gives 64, and skipping the first arrival gives 51.
More Coding Interview Problems questions
- Problem: The string "abcabcbb" is scanned and for each position the length of the longest run of distinct characters starting at that…
- Problem: Dijkstra's algorithm in an undirected graph with edges 0-1 (2), 1-2 (2), 0-2 (5), 2-3 (1), 1-3 (6). How many vertices have a…
- Problem: The string "xyyxzzyx" is examined for palindromic substrings of length 4 or more. How many such palindromic substrings are there…
- Problem: A binary string is "good" if it has no two consecutive zeros. How many good strings of length 7 begin with 1 and end with 1?
- Problem: A process list is [3, 7, 2, 9] with priorities; the CPU always runs the largest value next and runs it for its full value, then…
- Problem: How many subarrays of [2, 4, 6, 1, 3, 5] have all elements even or all elements odd?