Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA
Problem: n = 10⁵ intervals; for each, count how many other intervals it overlaps. What is the best complexity?
- A. O(n log n): sort starts and ends; overlapping count = n − 1 − (intervals ending before its start) − (intervals starting after its end), each found by binary search
- B. O(n²) – each pair must be checked
- C. O(n) – a single pass over unsorted intervals
- D. O(n log² n) – a 2-D segment tree is required
Correct answer: A. O(n log n): sort starts and ends; overlapping count = n − 1 − (intervals ending before its start) − (intervals starting after its end), each found by binary search
Two intervals do NOT overlap only if one ends before the other starts, and those two cases are disjoint, so subtract both counts. Two sorted arrays and two binary searches per interval give O(n log n).
More Coding Interview Problems questions
- Problem: A function does for i in range(n): for j in range(i, n, i + 1): work(). What is its time complexity?
- Problem: A recursive function on n does O(1) work and makes two calls on n/2. What is T(n)?
- Problem: Inside a loop of n iterations, the code calls Python's list.pop(0) and Java's String +=. What is the real complexity of the loop?
- Problem: For n = 10⁶ distinct integers up to 10⁹, find the smallest positive integer missing from the array in O(n) time and O(1) extra…
- Problem: Find the smallest missing positive integer using cyclic placement. python def firstmissing(a): n = len(a) for i in range(n)…
- Problem: Binary search for x in a sorted int array (C++): cpp int lo = 0, hi = n - 1; while (lo <= hi) { int mid = (lo + hi) / 2; // line…