Data Structures & AlgorithmsVery hardProduct Company SDE · binary search round

Problem: Two sorted arrays of sizes m and n are merged in O(m + n) time with two pointers. What is the complexity of finding the median of the merged array without building it, using binary search over partitions?

  1. A. O(m + n)
  2. B. O(m * n * log n)
  3. C. O(log(min(m, n)))
  4. D. O(log(m + n)) only if m equals n

Correct answer: C. O(log(min(m, n)))

Binary search over the cut position in the smaller array gives O(log(min(m,n))) time. Merging first costs O(m + n) and is the trap.

More Coding Interview Problems questions

Exams that ask this topic