Median of Two Sorted Arrays
Updated
Problem statement
You are given two integer arrays, nums1 and nums2. Each array is sorted on its own. Return the median of all their values taken together.
The median is the middle value once all the values are in sorted order. When the total count is even, it is the average of the two middle values.
At least one array is nonempty. Let m and n be the lengths of nums1 and nums2. The required running time is O(log(min(m, n))).
Function
medianOfTwoSortedArrays(nums1: int[], nums2: int[]) → doubleExamples
Example 1
nums1 = [1,3]nums2 = [2]return = 2.0Taken together and sorted, the values are [1,2,3]. There is an odd number of values, so the median is the middle one.
Example 2
nums1 = [1,2]nums2 = [3,4]return = 2.5Taken together and sorted, there is an even number of values. So the median is the average of the two middle values, 2 and 3.
Constraints
0 <= nums1.length, nums2.length <= 1000001 <= nums1.length + nums2.length- Both arrays are sorted in nondecreasing order.
- Values fit in a signed 32-bit integer.
More Goldman Sachs Phone Screen questions
- House Robber IIIPHONE SCREEN · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · Seen Nov 2026
- Best Average Student ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Check a Complete Binary TreePHONE SCREEN · Seen Oct 2026
- Meeting RoomsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Count Staircase WaysPHONE SCREEN · Seen Oct 2026
- Is a Power of TenPHONE SCREEN · Seen Oct 2026
- Minimum Traveling Salesperson TourPHONE SCREEN · Seen Oct 2026
- Square Root with Newton IterationPHONE SCREEN · Seen Oct 2026
- First Non-Repeating CharacterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Split Array to Minimize the Largest SumNation with NaMo · PHONE SCREEN
- Median Across N Sorted ArraysRippling · PHONE SCREENRippling interview questions
- Maximize Fish over Limited HoursByteDance · PHONE SCREENByteDance interview questions
- Median Across Sorted ArraysGoogle · PHONE SCREENGoogle interview questions
- Employee Ratings Management SystemAmazon · PHONE SCREEN
- Running Medians and PercentilesStackAdapt · PHONE SCREENStackAdapt interview questions
- Dynamic First and Last Position in Sorted DataBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Minimize the Largest Part BeautyAdobe · PHONE SCREENAdobe interview questions
- First Element Greater Than KMeta · PHONE SCREENMeta interview questions
- Koko Eating BananasThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions