Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA
Problem: Find the median of two sorted arrays of sizes m and n (up to 10⁶ each) in better than linear time. What is the optimal complexity?
- A. O(m + n) – merging is required
- B. O(log m · log n)
- C. O((m + n) log(m + n)) – sort the union
- D. O(log(min(m, n))) – binary search the partition of the smaller array
Correct answer: D. O(log(min(m, n))) – binary search the partition of the smaller array
Choose i elements from the smaller array and (m + n + 1)/2 − i from the other so that all left parts ≤ all right parts; this condition is monotonic in i, so binary search on the smaller array suffices. Merging is O(m + n).
More Coding Interview Problems questions
- Problem: A social graph has V = 10⁶ users and E = 10⁷ friendships. You must store it to run BFS. Which representation is appropriate and…
- Problem: Given 'a/b = k' equations (e.g. a/b = 2, b/c = 3), answer queries like a/c. Which model and algorithm fit?
- Problem: Evaluate division queries on the ratio graph a/b = 2, b/c = 3, d/e = 4. python from collections import defaultdict g =…
- Problem: Reverse a word after prepending a marker using Java's StringBuilder. java StringBuilder sb = new StringBuilder("interview")…
- Problem: Find the longest substring in which every character appears at least k times (divide and conquer on characters that are too…
- Problem: Return the minimum number of characters to insert to make a string a palindrome. python def mininsert(s): t = s[::-1]; n = len(s)…