Median of Two Sorted Arrays
Updated
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[]) → doubleExamples
Example 1
first = [1,3]second = [2]return = 2.0The combined sorted order is [1,2,3], whose middle value is 2.
Example 2
first = [1,2]second = [3,4]return = 2.5The two middle values are 2 and 3, so their average is 2.5.
Constraints
0 <= first.length, second.length <= 1000001 <= 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