Bird
0
0
DSA Cprogramming~10 mins

Merge Overlapping Intervals in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Merge Overlapping Intervals
Sort intervals by start
Initialize merged list empty
For each interval
Check overlap with last merged
Merge intervals
Repeat
Done
Sort intervals first, then go through each interval to merge if overlapping or add if not.
Execution Sample
DSA C
int intervals[][2] = {{1,3},{2,6},{8,10},{15,18}};
// Sort by start
// Merge overlapping intervals
// Result: {{1,6},{8,10},{15,18}}
This code merges overlapping intervals from a list of intervals.
Execution Table
StepOperationCurrent IntervalMerged List BeforeActionMerged List AfterVisual State
1Sort intervals[1,3],[2,6],[8,10],[15,18][]Sort by start[1,3],[2,6],[8,10],[15,18][1,3] -> [2,6] -> [8,10] -> [15,18] -> null
2Initialize merged list[1,3][]Add first interval[1,3][1,3] -> null
3Check overlap[2,6][1,3]2 <= 3 overlapMerge to [1,6][1,6] -> null
4Check overlap[8,10][1,6]8 > 6 no overlapAdd [8,10][1,6] -> [8,10] -> null
5Check overlap[15,18][1,6],[8,10]15 > 10 no overlapAdd [15,18][1,6] -> [8,10] -> [15,18] -> null
6EndN/A[1,6],[8,10],[15,18]All intervals processed[1,6],[8,10],[15,18][1,6] -> [8,10] -> [15,18] -> null
💡 All intervals processed and merged where overlapping.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
intervals[[1,3],[2,6],[8,10],[15,18]][[1,3],[2,6],[8,10],[15,18]][[1,3],[2,6],[8,10],[15,18]][[1,3],[2,6],[8,10],[15,18]][[1,3],[2,6],[8,10],[15,18]][[1,3],[2,6],[8,10],[15,18]]
merged[][[1,3]][[1,6]][[1,6],[8,10]][[1,6],[8,10],[15,18]][[1,6],[8,10],[15,18]]
current_intervalN/A[1,3][2,6][8,10][15,18]N/A
Key Moments - 3 Insights
Why do we merge intervals when current start <= last merged end?
Because intervals overlap if the current interval starts before or exactly when the last merged interval ends. See step 3 in execution_table where [2,6] overlaps with [1,3].
Why do we add the current interval to merged list when no overlap?
Because intervals do not overlap if current start is greater than last merged end, so we keep them separate. See step 4 and 5 where [8,10] and [15,18] are added separately.
Why do we sort intervals first?
Sorting ensures intervals are in order by start time, so merging can be done in one pass. Without sorting, overlapping intervals might be missed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the merged list after step 3?
A[[1,3]]
B[[1,6]]
C[[2,6]]
D[[1,3],[2,6]]
💡 Hint
Check the 'Merged List After' column in step 3 of execution_table.
At which step does the condition for no overlap first occur?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in execution_table where it says 'no overlap'.
If intervals were not sorted, what would change in the execution_table?
AMerging might miss overlapping intervals
BIntervals would be merged correctly anyway
CMerged list would be empty
DSorting step would be skipped but no effect
💡 Hint
Refer to the key_moments explanation about sorting importance.
Concept Snapshot
Merge Overlapping Intervals:
1. Sort intervals by start time.
2. Initialize merged list with first interval.
3. For each next interval, if it overlaps with last merged, merge them.
4. Else, add interval to merged list.
5. Result is merged intervals without overlap.
Full Transcript
To merge overlapping intervals, first sort the intervals by their start times. Then start with an empty merged list and add the first interval. For each next interval, check if it overlaps with the last interval in the merged list. Overlap means the current interval's start is less than or equal to the last merged interval's end. If overlapping, merge by updating the end of the last merged interval to the maximum end of both. If no overlap, add the current interval as a new entry in the merged list. Continue until all intervals are processed. The final merged list contains intervals without overlaps.