Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA
Closed intervals [2,9], [4,7], [6,12], [10,14], [11,13], [15,18]. What is the total length of the set of points covered by at least one interval (measured as the length of the union)?
- A. 25
- B. 10
- C. 13
- D. 8
Correct answer: B. 10
Merge overlapping intervals after sorting by start; the union is [2,14] plus [15,18], total 12 + 3 = 15. Adding raw lengths counts the overlapped parts twice, which is why that total is larger.
More Coding Interview Problems questions
- With coins of values 1, 3 and 4 (each usable any number of times), in how many ways (order does not matter) can you make the amount 6?
- What is the length of the longest run of equal consecutive values in [4, 4, 1, 2, 2, 2, 5, 1]?
- 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…
- 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]?