FastPrepMedian of Two Sorted Arrays

Median of Two Sorted Arrays

Bloomberg LP logoBloomberg LP● HardNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given two sorted integer arrays nums1 and nums2, return the median of the two arrays as if they were merged.

Your algorithm must run in O(log(min(nums1.length, nums2.length))) time.

Function

findMedianSortedArrays(nums1: int[], nums2: int[]) → double

Examples

Example 1

nums1 = [1,3]nums2 = [2]return = 2.0

The conceptual merged order is [1,2,3], whose middle value is 2.

Example 2

nums1 = [1,2]nums2 = [3,4]return = 2.5

The middle values are 2 and 3, so the median is their average.

Constraints

  • 0 <= nums1.length, nums2.length <= 10^5.
  • 1 <= nums1.length + nums2.length <= 2 * 10^5.
  • -10^9 <= nums1[i], nums2[i] <= 10^9.
  • Both arrays are sorted in nondecreasing order.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
  // Write your code here.
}
nums1[1,3]
nums2[2]
expected2.0
Checking account…