Median of Two Sorted Arrays
Updated
Problem statement
Given two integer arrays first and second, each sorted from smallest to largest, return the median of their combined values.
Count every occurrence, including repeated values. For an odd total, the median is the middle value. For an even total, it is the average of the two middle values. Either input may be empty, but they are not both empty.
After describing a linear merge, find a method that uses O(log(min(m,n)+1)) time and constant extra space, where m and n are the input lengths.
Function
findMedianSortedArrays(first: int[], second: int[]) → doubleExamples
Example 1
first = [2,8]second = [5]return = 5.0Merged in order, the values are [2,5,8]. There is an odd number of values, so the median is the one in the middle.
Example 2
first = [1,6]second = [3,10]return = 4.5The merged list has an even number of values. Its two middle values are 3 and 6, so the median is their average.
Example 3
first = []second = [-2,4,9]return = 4.0first is empty, so every value comes from second. The median is the middle value of second.
Constraints
0 <= first.length, second.length <= 10^5.1 <= first.length + second.length <= 2 * 10^5.- Values are signed 32-bit integers.
- Both arrays are sorted in nondecreasing order.
More Rippling Phone Screen questions
- Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dependency-Aware Task SchedulerPHONE SCREEN · Seen Nov 2026
- Delivery Cost Tracker with Effective-Dated RatesPHONE SCREEN · Seen Nov 2026
- Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best and Worst Camel Card CompletionsPHONE SCREEN · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- In-Memory Priority Task SchedulerPHONE SCREEN · Seen Oct 2026
- Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Median Across N Sorted ArraysPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Shortest Subarray with Sum at Least KOracle · ONSITE INTERVIEW
- Russian Doll EnvelopesGoogle · ONSITE INTERVIEW
- Split Array to Minimize the Largest SumNation with NaMo · PHONE SCREEN
- K-th Element of Two Sorted ArraysFlipkart · ONSITE INTERVIEW
- Maximize Fish over Limited HoursByteDance · PHONE SCREEN
- Allocate Minimum PagesAmazon · ONSITE INTERVIEW
- Minimum Processing Speed with Multiple RidersDoorDash · ONSITE INTERVIEW
- Running Medians and PercentilesStackAdapt · PHONE SCREEN
- Russian Doll EnvelopesInMobi · ONSITE INTERVIEW
- Median of Two Sorted ArraysGoldman Sachs · PHONE SCREEN