Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA
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 [1,8], [18,25], [8,10], [4,8], [15,17], [21,24], what is the result?
- A. [1,8], [4,8], [8,10], [15,17], [18,25], [21,24]
- B. [1,8], [8,10], [15,17], [18,25]
- C. [18,25], [15,17], [1,10]
- D. [1,10], [15,17], [18,25]
Correct answer: D. [1,10], [15,17], [18,25]
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 [1,10], [15,17], [18,25]. Requiring a strictly smaller start (< instead of ≤) would wrongly keep touching intervals separate, giving [1,8], [8,10], [15,17], [18,25].
More Coding Interview Problems questions
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…
- What is the length of the longest common subsequence of "AEEBABADE" and "EDBDEA"?
- 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…
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…
- Merging overlapping intervals (sort by start, merge when the next interval's start is ≤ the current merged interval's end — touching…
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…