Find median of two sorted arrays
RipplingSoftware Engineer · Technical ScreenLast updated
This question evaluates algorithmic problem-solving skills related to sorted arrays and median computation, testing competency in handling numeric order properties and complexity analysis within the Coding & Algorithms domain.
You are given two sorted arrays of integers nums1 and nums2 in non-decreasing order.
Let the lengths be m = nums1.length and n = nums2.length. Either array may be empty, but not both at the same time.
If you were to conceptually merge them into a single sorted array merged of length m + n, the median is defined as:
- If
m + nis odd: the element at index(m + n) / 2(0-based) inmerged. - If
m + nis even: the average of the two middle elements at indices(m + n) / 2 - 1and(m + n) / 2inmerged.
Task
Implement a function that returns the median of the two sorted arrays without actually merging them.
- Function signature (conceptual):
- Input:
nums1: integer[],nums2: integer[] - Output:
median: float(or double)
Constraints (you may assume)
0 ≤ m, n ≤ 10^5m + n ≥ 1- Each array is individually sorted in non-decreasing order.
- Integer values fit in a 32-bit signed integer range.
- Target time complexity: O(log(m + n)).
- Extra space complexity: O(1) beyond input storage.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.