Question presented to candidate: "You have two sorted lists of integers, of lengths m and n. Return the median of all the numbers together. A merge is O(m + n); I would like O(log(min(m, n))). Please write it in Python and explain the partition idea."
What a strong answer should cover:
- The merge baseline in O(m + n), then the observation that choosing a cut
iin one list forcesj = half - iin the other. - The validity test
A[i-1] <= B[j]andB[j-1] <= A[i], with infinities at the edges, and how each failure says which way to move. - Binary search on
iover the shorter list: O(log(min(m, n))) time, O(1) space, and why it keepsjin range. - Reading the median from the cut for odd and even totals.
- Python specifics:
float('inf')sentinels,//forhalf,/returning a float for the even case, unboundedint, and thatstatistics.median(a + b)is an O((m+n) log(m+n)) shortcut, not the answer.
Clarifying questions expected:
- "Can either list be empty?"
- "For an even total, do you want the average of the two middle values?"
Code / implementation expected: Yes — find_median_sorted_arrays(a, b) in Python using the partition binary search.