FastPrepMedian Across N Sorted Arrays

Median Across N Sorted Arrays

Rippling logoRippling● HardINTERNPHONE SCREEN
Learn

Problem statement

Given arrays, a collection of individually sorted integer arrays, return the median of every value across the collection. The rows may have different lengths or be empty, but at least one value exists overall.

Preserve duplicate multiplicities. For an odd total number of values, return the middle value in the combined sorted order. For an even total, return the arithmetic mean of the two middle values.

After a flatten-and-sort baseline, use each row’s sorted order to locate the required ranks without constructing a merged array.

Function

medianOfSortedArrays(arrays: int[][]) → double

Examples

Example 1

arrays = [[1,7],[2,6],[3]]return = 3.0

The combined order is [1,2,3,6,7].

Example 2

arrays = [[1,4],[],[2,8]]return = 3.0

The two middle values are 2 and 4.

Example 3

arrays = [[-9,-3],[0],[2,2,8]]return = 1.0

The middle pair is 0 and 2.

Constraints

  • 1 <= arrays.length <= 2000.
  • The total number of values is between 1 and 10^5.
  • Every row is sorted in nondecreasing order.
  • Values are signed 32-bit integers.

More Rippling problems

See Rippling hiring insights
public double medianOfSortedArrays(int[][] arrays) {
    // Write your code here.
}
arrays[[1,7],[2,6],[3]]
expected3.0
Checking account…