Median of Two Sorted Arrays
Updated
Problem statement
Return the median of the values in two sorted integer arrays a and b. For an odd total count, the median is the middle value. For an even total count, it is the average of the two middle values.
For this exercise, assume either array may be empty, but they cannot both be empty. Aim for logarithmic time in the shorter array's length.
Function
findMedianSortedArrays(a: int[], b: int[]) → doubleExamples
Example 1
a = [1,3]b = [2]return = 2.0The combined sorted order is [1, 2, 3], whose middle value is 2.
Example 2
a = [1,2]b = [3,4]return = 2.5The middle values of [1, 2, 3, 4] are 2 and 3. Their average is 2.5.
Example 3
a = []b = [-2,0,8]return = 0.0Only the second array contributes values. Its middle value is 0.
Constraints
0 <= a.length <= 100000 <= b.length <= 10000-1000000 <= a[i], b[i] <= 1000000- Both arrays are sorted in nondecreasing order.
- At least one array is nonempty.
More JP Morgan Chase Onsite Interview questions
- LFU CacheONSITE INTERVIEW · Seen Nov 2026
- LRU CacheONSITE INTERVIEW · Seen Nov 2026
- Employees Above Average SalaryONSITE INTERVIEW · Seen Sep 2026
- Highest-Earning Employees By DepartmentONSITE INTERVIEW · Seen Sep 2026
- K Largest Integers in Descending OrderONSITE INTERVIEW · Seen Aug 2026
- Tile a Three-Row Board with DominoesONSITE INTERVIEW · Seen Jan 2026
- Two SumONSITE INTERVIEW · Seen Nov 2025
- Intersection of Two ListsONSITE INTERVIEW · Seen Jan 2025
- Maximum Subarray SumONSITE INTERVIEW · Seen Jan 2025
- Fibonacci NumberONSITE INTERVIEW · Seen Nov 2024
Similar questions at other companies
- 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
- Median of Two Sorted ArraysMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Split Array Largest SumBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Median of Two Sorted ArraysExpedia · ONSITE INTERVIEWExpedia interview questions
- Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEWByteDance interview questions
Interview reports that mention this question
JP Morgan Chase interview experiences
- JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
- JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
- JP Morgan Chase Data and AI Intern Superday: 11 Experiences
- JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)
- JPMorgan Code for Good India: 10 Experiences (Part 4 of 5)