Problem · Array

Median of Two Sorted Arrays

Learn this problem
HardGoldman Sachs logoGoldman SachsFULLTIMEPHONE SCREEN

Problem statement

Given two individually sorted integer arrays nums1 and nums2, return the median of all their values.

At least one array is nonempty. The required running time is O(log(min(m, n))).

Function

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

Examples

Example 1

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

The merged order is [1,2,3].

Example 2

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

The two middle values are 2 and 3.

Constraints

  • 0 <= nums1.length, nums2.length <= 100000
  • 1 <= nums1.length + nums2.length
  • Both arrays are sorted in nondecreasing order.
  • Values fit in a signed 32-bit integer.

More Goldman Sachs problems

drafts saved locally
public double medianOfTwoSortedArrays(int[] nums1, int[] nums2) {
  // write your code here
}
nums1[1,3]
nums2[2]
expected2.0
checking account