📖 Scenario: You are working on a calendar app that manages meeting times. Sometimes, meetings overlap and you want to combine those overlapping meetings into one to avoid confusion.
🎯 Goal: Build a program that takes a list of meeting time intervals and merges all overlapping intervals into one.
📋 What You'll Learn
Create a slice of intervals with exact values
Add a variable to hold the merged intervals
Write logic to merge overlapping intervals
Print the merged intervals in the specified format
💡 Why This Matters
🌍 Real World
Merging overlapping intervals is useful in calendar apps, booking systems, and timeline visualizations to simplify overlapping time slots.
💼 Career
Understanding interval merging is important for software engineers working on scheduling, resource allocation, and data processing tasks.
Progress0 / 4 steps
1
Create the initial intervals slice
Create a slice of intervals called intervals with these exact values: {1, 3}, {2, 6}, {8, 10}, {15, 18}. Use a struct type Interval with start and end fields of type int.
DSA Go
Hint
Define the slice with the exact intervals as shown.
2
Add a variable to hold merged intervals
Add a variable called merged of type slice of Interval to hold the merged intervals. Initialize it as an empty slice.
DSA Go
Hint
Initialize merged as an empty slice of Interval.
3
Write logic to merge overlapping intervals
Write a for loop to iterate over intervals. For each interval, if merged is empty or the last interval in merged does not overlap with interval, append interval to merged. Otherwise, merge by updating the end of the last interval in merged to the maximum of its current end and interval.end.
DSA Go
Hint
Check if merged is empty or no overlap, then append. Else update the end of last merged interval.
4
Print the merged intervals
Print the merged slice in the format: each interval as {start,end} separated by spaces on one line.
DSA Go
Hint
Use a loop to print each merged interval in the format {start,end} separated by spaces.
Practice
(1/5)
1. What is the main goal when merging overlapping intervals in a list like [[1,3],[2,6],[8,10]]?
easy
A. Remove all intervals that overlap
B. Sort intervals by their end values only
C. Combine intervals that overlap into one continuous interval
D. Split intervals into smaller parts
Solution
Step 1: Understand overlapping intervals
Intervals overlap if one starts before the other ends. For example, [1,3] and [2,6] overlap because 2 is between 1 and 3.
Step 2: Merge overlapping intervals
We combine overlapping intervals into one that covers the full range, like merging [1,3] and [2,6] into [1,6].
Final Answer:
Combine intervals that overlap into one continuous interval -> Option C
Quick Check:
Merging = Combine overlaps [OK]
Hint: Merge intervals by combining overlapping ranges [OK]
Common Mistakes:
Only sorting by end values
Removing instead of merging overlaps
Splitting intervals unnecessarily
2. Which Go code snippet correctly sorts intervals by their start value before merging?