FastPrepMedian of Two Sorted Arrays

Median of Two Sorted Arrays

Microsoft logoMicrosoft● HardFULLTIMEONSITE INTERVIEW
Learn

Problem statement

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

At least one array is nonempty. Solve the problem in O(log(min(first.length, second.length))) time.

Function

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

Examples

Example 1

first = [1,3]second = [2]return = 2.0

The combined sorted order is [1,2,3], whose middle value is 2.

Example 2

first = [1,2]second = [3,4]return = 2.5

The two middle values are 2 and 3, so their average is 2.5.

Constraints

  • 0 <= first.length, second.length <= 100000
  • 1 <= first.length + second.length
  • -10^9 <= first[i], second[i] <= 10^9
  • Both arrays are sorted in nondecreasing order

More Microsoft problems

See Microsoft hiring insights
public double medianTwoSortedArrays(int[] first, int[] second) {
  // Write your code here.
}
first[1,3]
second[2]
expected2.0
Checking account…