FastPrepMedian of Two Sorted Arrays

Median of Two Sorted Arrays

Rippling logoRippling● HardINTERNPHONE SCREEN
Learn

Problem statement

Given two integer arrays first and second, each sorted in nondecreasing order, return the median of their combined values.

Duplicates keep their multiplicities. For an odd total, the median is the middle value. For an even total, it is the arithmetic mean of the two middle values. Either input may be empty, but they are not both empty.

After describing a linear merge, find a method that uses O(log(min(m,n)+1)) time and constant extra space, where m and n are the input lengths.

Function

findMedianSortedArrays(first: int[], second: int[]) → double

Examples

Example 1

first = [2,8]second = [5]return = 5.0

The combined order is [2,5,8].

Example 2

first = [1,6]second = [3,10]return = 4.5

The middle values are 3 and 6.

Example 3

first = []second = [-2,4,9]return = 4.0

The nonempty input supplies every value.

Constraints

  • 0 <= first.length, second.length <= 10^5.
  • 1 <= first.length + second.length <= 2 * 10^5.
  • Values are signed 32-bit integers.
  • Both arrays are sorted in nondecreasing order.

More Rippling problems

See Rippling hiring insights
public double findMedianSortedArrays(int[] first, int[] second) {
    // Write your code here.
}
first[2,8]
second[5]
expected5.0
Checking account…