FastPrepMedian of Two Sorted Arrays
Problems/Median of Two Sorted Arrays

Median of Two Sorted Arrays

Microsoft logoMicrosoft● HardFULLTIMEONSITE INTERVIEW

Updated Oct 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Microsoft Data Science PhD internship report asks for the median of two arrays and describes repeated optimization follow-ups. Sorted inputs, empty-array behavior, parity handling, logarithmic target complexity, bounds, and tolerance are conventional enrichment.

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 Onsite Interview questions

  • Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
  • Coin ChangeONSITE INTERVIEW · Seen Oct 2026
  • Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
  • Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Shortest Subarray with Sum at Least KOracle · ONSITE INTERVIEWOracle interview questions
  • Russian Doll EnvelopesGoogle · ONSITE INTERVIEWGoogle interview questions
  • K-th Element of Two Sorted ArraysFlipkart · ONSITE INTERVIEW
  • Allocate Minimum PagesAmazon · ONSITE INTERVIEW
  • Minimum Processing Speed with Multiple RidersDoorDash · ONSITE INTERVIEWDoorDash interview questions
  • Russian Doll EnvelopesInMobi · ONSITE INTERVIEWInMobi interview questions
  • Median of Two Sorted ArraysRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
  • Split Array Largest SumBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Russian Doll EnvelopesAirbnb · ONSITE INTERVIEWAirbnb interview questions
  • Median of Two Sorted ArraysExpedia · ONSITE INTERVIEWExpedia interview questions

Interview reports that mention this question

  • Microsoft AI/ML Intern Interviews: 114 Experiences (Part 1 of 2)

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public double medianTwoSortedArrays(int[] first, int[] second) {
      // Write your code here.
    }

}
Checking account…