← Questions

Median of two run-length encoded arrays

0
GoogleCodingLast asked Jan 2026

A coding interview question asked in Google's first round (coding), last asked Jan 2026. It tests binary-search and compression.

Two sorted arrays are given in run-length form: lists of (value, count) pairs in increasing order of value. Return the median of all values of both arrays combined, without expanding the runs. With an even total, the median is the average of the two middle values.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • rle_median(a: list[tuple[int, int]], b: list[tuple[int, int]]) -> float

Example

a = [(1, 2), (3, 1)]          # 1, 1, 3
b = [(2, 2)]                  # 2, 2
rle_median(a, b)  ->  2       # 1, 1, 2, 2, 3

a = [(1, 3)]                  # 1, 1, 1
b = [(4, 1), (9, 4)]          # 4, 9, 9, 9, 9
rle_median(a, b)  ->  6.5     # middle values 4 and 9

Constraints

  • Up to 10^5 runs per array; counts up to 10^9, so totals can reach about 10^14.
  • At least one value in total.

Clarifying questions

  • Is each array sorted by value, with distinct values per run?
  • How is the median defined for an even total?
  • Can one array be empty?

What a strong answer covers

  • A helper that finds the k-th smallest value of the combined arrays; the median is one or two such calls.
  • Option 1: merge the runs with two pointers, adding counts until the running total passes k: O(R_a + R_b).
  • Option 2: binary search on the value, counting how many values are at most x in each array with prefix sums of counts and binary search over runs: O(log(range) · log R).
  • Why expanding the runs is not an option with counts up to 10^9.
  • Edge cases: an empty array, the two middle values falling in different runs or in different arrays, very large counts.

Follow-ups

  • k encoded arrays instead of two.
  • Many median queries while runs are appended to the arrays.
  • The arrays are not sorted; runs are just compressed sequences.

Where it's asked

  • Google · First round: coding · Last asked Jan 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI

What it tests

binary-search · compression

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding