Find median of two sorted arrays

by quasar_realm592
Apple
senior
coding
hard
pending
3
42

The interviewer asked me to find the median of two sorted arrays, with a target runtime better than merging them. I started by describing a merge-based solution, then caught myself and switched to binary search on the smaller array. The key idea was to partition both arrays so the left half contains the same number of elements as the right half, while the values on either side of the partition remain ordered.

I wrote the partition logic in Python and explained the boundary cases using negative and positive infinity. The interviewer followed closely and asked why the search should run on the smaller array; I said it keeps the runtime at O(log(min(m, n))) and avoids searching an unnecessarily large range. They nodded at the complexity explanation, but asked me to slow down while tracing the odd-length case.

I handled an empty array and unequal lengths correctly in the walkthrough. I did stumble on the even-length return value at first, using the wrong pair of partition boundaries; the interviewer pointed out a counterexample, and I corrected it to average the maximum on the left with the minimum on the right. That cost a few minutes, and I had not left enough time to run through every edge case.

As a follow-up, the interviewer asked how I would test the implementation and what happens when one input is much shorter than the other. I proposed tests for empty input, one-element arrays, odd and even totals, and highly uneven lengths, then noted that the shorter-array search keeps the bound tight. We also briefly discussed integer overflow in languages with fixed-width arithmetic; I mentioned using a wider type for the sum, though I was less crisp than I wanted to be about language-specific details.

The interviewer was engaged but fairly quiet during the coding portion, so it was hard to read the signal. I finished with the correct O(log(min(m, n))) algorithm and a working implementation, but the partition-boundary correction and rushed test coverage were clear weaknesses. The interview outcome was pending when I wrote this report.


Markdown supported