Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Merging overlapping intervals (sort by start, merge when the next interval's start is ≤ the current merged interval's end — touching endpoints count as overlapping) on [8,16], [18,25], [28,35], [21,28], [4,6], what is the result?
- A. [4,6], [8,16], [18,35]
- B. [4,6], [8,16], [18,28], [28,35]
- C. [18,35], [8,16], [4,6]
- D. [4,6], [8,16], [18,25], [21,28], [28,35]
Correct answer: A. [4,6], [8,16], [18,35]
After sorting by start time, two intervals merge whenever the next one's start is ≤ the current merged end (so [2,5] and [5,8] do merge, since they touch at 5); applying this gives [4,6], [8,16], [18,35]. Requiring a strictly smaller start (< instead of ≤) would wrongly keep touching intervals separate, giving [4,6], [8,16], [18,28], [28,35].
More Coding Interview Problems questions
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…
- Using a min-heap of size k (push each element, pop when size exceeds k, so the heap always holds the k largest seen so far), what is the k…
- Fractional knapsack, capacity 11 (items can be split). Items (weight, value): (5, 15), (4, 10), (4, 14), (4, 7). Using the greedy…
- A divide-and-conquer routine satisfies T(n) = 2T(n/2) + n / log n. What is its running time (by the master theorem, extended for polylog…