Given a list of appointments represented as intervals [[start, end], ...], find all pairs of appointments that conflict (i.e., overlap). Return or print every pair of intervals that overlap.
The standard approach is to sort intervals by start and then compare each interval with the previous (or maintain an active set). When an interval’s start is less than the previous interval’s end, they overlap — record the conflicting pair.
Method 1 - Sort by start and scan (record overlapping pairs)#
Sorting by start brings potential overlapping intervals next to each other. If interval[i].start < interval[i-1].end, the two intervals overlap. Scanning once after sorting finds all adjacent overlaps; to find all conflicts (including overlapping multiple previous intervals), keep a running prev interval that tracks the rightmost end seen so far or compare with nearby intervals as needed.
If there are fewer than 2 appointments return an empty list.
Sort intervals by start in non-decreasing order.
Iterate i from 1 to n-1:
If intervals[i].start < intervals[i-1].end then record the pair (intervals[i-1], intervals[i]) as a conflict.
To capture chains where one interval overlaps multiple previous intervals (rare for simple adjacent-check approach), you can maintain an active interval with the maximum end among overlapping group members and compare subsequent intervals against it.
Return the list of conflicting pairs.
Edge cases:
Intervals that share an endpoint (e.g., [1,2] and [2,3]) are considered non-overlapping (start >= prev.end allowed).
If many intervals overlap a common window, the simple adjacent-pair method will find each adjacent conflict; to enumerate all unique conflicting pairs in a dense cluster you may need an active structure (heap/interval tree) — this version reports the obvious adjacent conflicts which covers typical interview expectations.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
classSolution {
public List<int[][]>findConflicts(int[][] intervals) {
List<int[][]> res =new ArrayList<>();
if (intervals ==null|| intervals.length< 2) return res;
Arrays.sort(intervals, java.util.Comparator.comparingInt(a -> a[0]));
for (int i = 1; i < intervals.length; i++) {
if (intervals[i][0]< intervals[i-1][1]) {
res.add(newint[][]{intervals[i-1], intervals[i]});
}
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
classSolution:
deffind_conflicts(self, intervals: list[tuple[int,int]]) -> list[tuple[tuple[int,int], tuple[int,int]]]:
res: list[tuple[tuple[int,int], tuple[int,int]]] = []
ifnot intervals or len(intervals) <2:
return res
intervals.sort(key=lambda x: x[0])
for i in range(1, len(intervals)):
if intervals[i][0] < intervals[i-1][1]:
res.append((intervals[i-1], intervals[i]))
return res
⏰ Time complexity: O(n log n) due to sorting; the subsequent single pass is O(n), so total O(n log n).
🧺 Space complexity: O(k) for the output list of k conflict pairs (worst-case O(n)), plus O(1) auxiliary space beyond input and output (sorting may require O(n) auxiliary space depending on language/runtime).