Skip to main content

23. Median of Two Sorted Arrays

hardAsked at Glassdoor

Glassdoor publishes salary medians from two independently sorted data warehouses — getting that number in O(log n) time, not O(n), is the kind of constraint engineers there live with, which is why this binary-search-on-partitions problem appears in their hard-tier loops.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Given two sorted arrays nums1 and nums2 of sizes m and n respectively, return the median of the two sorted arrays. The overall runtime complexity must be O(log(m + n)).

Constraints

  • nums1.length == m
  • nums2.length == n
  • 0 <= m <= 1000
  • 0 <= n <= 1000
  • 1 <= m + n <= 2000
  • -10^6 <= nums1[i], nums2[i] <= 10^6

Examples

Example 1

Input
nums1 = [1,3], nums2 = [2]
Output
2.00000

Explanation: Merged: [1,2,3]. Median is the middle element 2.

Example 2

Input
nums1 = [1,2], nums2 = [3,4]
Output
2.50000

Explanation: Merged: [1,2,3,4]. Median = (2+3)/2 = 2.5.

Approaches

1. Merge then find median

Merge both sorted arrays in O(m+n), then pick the middle element(s). Simple but doesn't meet the O(log(m+n)) requirement.

Time
O(m + n)
Space
O(m + n)
function findMedianSortedArrays(nums1, nums2) {
  const merged = [];
  let i = 0, j = 0;
  while (i < nums1.length && j < nums2.length) {
    if (nums1[i] <= nums2[j]) merged.push(nums1[i++]);
    else merged.push(nums2[j++]);
  }
  while (i < nums1.length) merged.push(nums1[i++]);
  while (j < nums2.length) merged.push(nums2[j++]);
  const mid = Math.floor(merged.length / 2);
  return merged.length % 2 === 1
    ? merged[mid]
    : (merged[mid - 1] + merged[mid]) / 2;
}

Tradeoff:

2. Binary search on partition

Binary search on the shorter array to find a partition where max-left <= min-right on both arrays simultaneously. The median is derived from the four boundary values. O(log(min(m,n))).

Time
O(log(min(m, n)))
Space
O(1)
function findMedianSortedArrays(nums1, nums2) {
  if (nums1.length > nums2.length) return findMedianSortedArrays(nums2, nums1);
  const m = nums1.length, n = nums2.length;
  let lo = 0, hi = m;
  while (lo <= hi) {
    const px = Math.floor((lo + hi) / 2);
    const py = Math.floor((m + n + 1) / 2) - px;
    const maxLeftX = px === 0 ? -Infinity : nums1[px - 1];
    const minRightX = px === m ? Infinity : nums1[px];
    const maxLeftY = py === 0 ? -Infinity : nums2[py - 1];
    const minRightY = py === n ? Infinity : nums2[py];
    if (maxLeftX <= minRightY && maxLeftY <= minRightX) {
      if ((m + n) % 2 === 1) return Math.max(maxLeftX, maxLeftY);
      return (Math.max(maxLeftX, maxLeftY) + Math.min(minRightX, minRightY)) / 2;
    } else if (maxLeftX > minRightY) {
      hi = px - 1;
    } else {
      lo = px + 1;
    }
  }
}

Tradeoff:

Glassdoor-specific tips

This is Glassdoor's hardest recurring problem. Interviewers know most candidates freeze on the partition invariant, so state it clearly upfront: 'I want to find a cut in both arrays such that every element on the left of the combined cut is ≤ every element on the right.' Mentioning that you always binary-search on the shorter array shows you've thought about worst-case bounds. If you run out of time, delivering the O(m+n) merge solution with a clear explanation of the O(log) path scores better than a half-finished binary search.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill Median of Two Sorted Arrays and other Glassdoor interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →