Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: Finding the median of two sorted arrays of sizes m and n can be done faster than merging them. What is the best known time complexity, and how?

  1. A. O((m+n) log(m+n)) by sorting the concatenation
  2. B. O(log(m + n)) by binary searching the concatenation directly
  3. C. O(m + n) by merging both arrays
  4. D. O(log(min(m, n))) via binary search on a partition point in the smaller array

Correct answer: D. O(log(min(m, n))) via binary search on a partition point in the smaller array

Binary searching for a partition of the smaller array (and deriving the matching partition of the other) that balances left/right halves avoids a full merge, achieving O(log(min(m,n))).

More Coding Interview Problems questions

Exams that ask this topic