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?
- A. O(m + n)
- B. O(m * n * log n)
- C. O(log(min(m, n)))
- 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
- Array a = [3, 2, 5, 1, 4, 6, 2, 7]. Consider all contiguous subarrays and count those whose sum is a power of two from the set {1, 2, 4…
- Array a = [4, 7, 1, 8, 3, 6, 5, 2]. What is the largest sum of a contiguous subarray that contains exactly two odd numbers?
- Array a = [2, 5, 1, 6, 3, 8, 4]. How many ordered pairs of distinct positions (i, j) satisfy a[j] >= 2 a[i]?
- From the values [4, 9, 1, 12, 7, 3, 10, 6], keep as many as possible so that in the kept set the maximum is at most twice the minimum. How…
- Seven people weigh [3, 5, 3, 4, 2, 6, 1] (kg). A boat carries at most two people and at most 7 kg in total. What is the minimum number of…
- Sorted list [1, 2, 4, 6, 7, 9, 11]. How many index pairs i < j satisfy vals[i] + vals[j] <= 10?