Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional
Problem: The stream of numbers 3, 8, 2, 9, 4, 7, 6, 1 is processed by a running median: after each number, the median of the numbers so far is recorded (for an even count, use the lower of the two middle values). What is the sum of the recorded medians?
- A. 45
- B. 37.5
- C. 26
- D. 30
Correct answer: D. 30
Two heaps give the lower medians 3, 3, 3, 3, 4, 4, 6, 4, summing to 30. Taking the upper middle for even counts gives 45, averaging the two middles gives 37.5, and missing the median after the final value gives 26.
More Coding Interview Problems questions
- Problem: A sequence is defined by a1 = 1, a2 = 1, and an = an−1 + an−2 mod 7 for n ≥ 3. What is a30 (1-indexed)?
- Problem: A grid of size 3×4 has the cell values 1 to 12 filled row-wise. Starting at the top-left (value 1), you may move right or down…
- For a = [2, -1, 4, -3, 5], which of the following statements is TRUE? A. Some contiguous subarray sums to 7. B. Every prefix sum from the…
- What does this program print? python a = [1, -1, 2, -2, 3] seen = {0: 0} s = 0; out = [] for i, x in enumerate(a, 1): s += x if s in seen…
- What does this program print? python best = None s = 0 for x in [4, -2, -2, 3, -1]: s += x best = s if best is None else max(best, s)…
- Array [1, 3, 2, 6, 4, 5, 7, 8]. How many inversions does it have (index pairs i < j with a[i] > a[j])?