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?
- A. O((m+n) log(m+n)) by sorting the concatenation
- B. O(log(m + n)) by binary searching the concatenation directly
- C. O(m + n) by merging both arrays
- 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
- Problem: Which statement about the backtracking solution to generating all subsets of an n-element set is correct?
- Problem: Generating all permutations of an n-element array via backtracking takes how much time, and why?
- Problem: In the N-Queens backtracking search, what pruning technique keeps the search from degenerating into a full O(N^N) brute force?
- Problem: Which best explains why a monotonic stack is the standard technique for the 'next greater element' problem, giving O(n) overall…
- Problem: A sliding window maximum over an array of size n with window size k is computed with a monotonic deque. What is its time…
- Problem: Two Sum on an unsorted array can be solved in O(n) using a hash map, or in O(n log n) using sort-and-two-pointers. Why might the…