What if you could instantly combine all overlapping times without missing a single clash?
Why Merge Overlapping Intervals in DSA Java?
Imagine you have a list of booked meeting times in a day, but some meetings overlap. You want to find the total free time, but first, you need to combine all overlapping meetings into one.
Manually checking each meeting against all others to merge overlaps is slow and confusing. It's easy to miss overlaps or merge incorrectly, especially when many meetings overlap in complex ways.
Using the "Merge Overlapping Intervals" method, you sort the meetings by start time and then combine overlapping ones in a single pass. This makes merging simple, fast, and error-free.
List<Interval> merged = new ArrayList<>(); for (Interval i : intervals) { // manually check and merge overlaps }
Collections.sort(intervals, (a, b) -> a.start - b.start); for (Interval interval : intervals) { if (merged.isEmpty() || merged.get(merged.size() - 1).end < interval.start) { merged.add(interval); } else { merged.get(merged.size() - 1).end = Math.max(merged.get(merged.size() - 1).end, interval.end); } }
This lets you quickly find combined time ranges, making scheduling, booking, and resource planning easy and reliable.
When booking rooms in an office, merging overlapping reservations helps show when rooms are truly free or busy without confusion.
Manually merging intervals is slow and error-prone.
Sorting intervals first simplifies merging overlaps.
One pass after sorting efficiently merges all overlaps.
Practice
Solution
Step 1: Understand overlapping intervals
Overlapping intervals share some common range, so merging combines them into one continuous interval.Step 2: Purpose of merging
Merging reduces multiple overlapping intervals into fewer intervals without overlaps, simplifying the list.Final Answer:
To combine intervals that overlap into a single continuous interval -> Option AQuick Check:
Merging = Combine overlaps [OK]
- Thinking merging sorts intervals by length
- Confusing merging with removing duplicates
- Assuming merging finds max end only
Solution
Step 1: Identify sorting by start time ascending
We need to sort intervals by their start value in ascending order for merging.Step 2: Check each option
Collections.sort(intervals, (a, b) -> a.start - b.start); sorts by start ascending correctly. intervals.sort((a, b) -> b.start - a.start); sorts descending. Collections.sort(intervals, (a, b) -> a.end - b.end); sorts by end, not start. intervals.sort((a, b) -> a.length() - b.length()); sorts by length, which is incorrect.Final Answer:
Collections.sort(intervals, (a, b) -> a.start - b.start); -> Option AQuick Check:
Sort by start ascending = Collections.sort(intervals, (a, b) -> a.start - b.start); [OK]
- Sorting by end time instead of start time
- Sorting in descending order
- Sorting by interval length
List<Interval> intervals = List.of(new Interval(1,3), new Interval(2,6), new Interval(8,10), new Interval(15,18));
List<Interval> merged = mergeIntervals(intervals);
for (Interval i : merged) {
System.out.print("[" + i.start + "," + i.end + "] ");
}Assume
mergeIntervals merges intervals correctly.Solution
Step 1: Identify overlapping intervals
Intervals (1,3) and (2,6) overlap, so they merge into (1,6). Others do not overlap.Step 2: List merged intervals
Merged list is (1,6), (8,10), (15,18).Final Answer:
[1,6] [8,10] [15,18] -> Option CQuick Check:
Merged intervals = [1,6] [8,10] [15,18] [OK]
- Not merging overlapping intervals
- Printing original intervals without merge
- Merging incorrectly leaving duplicates
public List<Interval> mergeIntervals(List<Interval> intervals) {
Collections.sort(intervals, (a, b) -> a.start - b.start);
List<Interval> merged = new ArrayList<>();
for (Interval interval : intervals) {
if (merged.isEmpty() || merged.get(merged.size() - 1).end < interval.start) {
merged.add(interval);
} else {
merged.get(merged.size() - 1).end = interval.end;
}
}
return merged;
}Solution
Step 1: Check merging logic
When intervals overlap, the code sets end to interval.end directly, which may be smaller than current end.Step 2: Correct approach
It should update end to max of current end and interval.end to cover full overlap.Final Answer:
It does not update the end to the max of overlapping intervals -> Option BQuick Check:
Update end = max(end, interval.end) needed [OK]
- Setting end to interval.end without max check
- Sorting by wrong field
- Adding overlapping intervals separately
[(1,10), (2,5), (6,9)]? What is the final merged list?Solution
Step 1: Analyze nested intervals
Intervals (2,5) and (6,9) are fully inside (1,10), so they overlap completely.Step 2: Merge nested intervals
Merging results in a single interval (1,10) covering all nested intervals.Final Answer:
[(1,10)] -> Option DQuick Check:
Nested intervals merge into outer interval [OK]
- Keeping nested intervals separate
- Splitting outer interval incorrectly
- Ignoring full overlap cases
Ready to prove it?
Pass the test and this pattern moves unknown
