📖 Scenario: You work in a calendar app team. Users add meeting times as intervals. Sometimes meetings overlap. You want to merge overlapping meetings to show a clean schedule.
🎯 Goal: Build a program that merges overlapping intervals from a list of meeting times.
📋 What You'll Learn
Create an array of intervals where each interval is an array of two numbers [start, end].
Create a variable to hold the merged intervals.
Write logic to merge overlapping intervals.
Print the merged intervals as an array.
💡 Why This Matters
🌍 Real World
Merging overlapping intervals is useful in calendar apps, booking systems, and resource scheduling to avoid conflicts.
💼 Career
Understanding interval merging helps in software roles involving scheduling, data processing, and algorithm design.
Progress0 / 4 steps
1
Create the intervals array
Create an array called intervals with these exact intervals: [1, 3], [2, 6], [8, 10], [15, 18].
DSA Javascript
Hint
Use const intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]; to create the array.
2
Create the merged array
Create an empty array called merged to store merged intervals.
DSA Javascript
Hint
Use const merged = []; to create an empty array.
3
Merge overlapping intervals
Sort intervals by start time using intervals.sort((a, b) => a[0] - b[0]). Then use a for loop with variable interval to iterate over intervals. Inside the loop, if merged is empty or merged[merged.length - 1][1] < interval[0], push interval to merged. Otherwise, update the end of the last interval in merged to the maximum of merged[merged.length - 1][1] and interval[1].
DSA Javascript
Hint
Sort intervals first. Then loop through each interval. If no overlap, add to merged. If overlap, update the end time.
4
Print the merged intervals
Print the merged array using console.log(merged).
DSA Javascript
Hint
Use console.log(merged) to print the merged intervals.
Practice
(1/5)
1. What is the main purpose of the Merge Overlapping Intervals algorithm?
easy
A. To sort intervals by their length
B. To combine all intervals that overlap into one continuous interval
C. To find the maximum number of intervals
D. To split intervals into smaller parts
Solution
Step 1: Understand the problem goal
The algorithm aims to merge intervals that share any overlapping range into a single interval.
Step 2: Identify the correct purpose
Among the options, only combining overlapping intervals matches the goal.
Final Answer:
To combine all intervals that overlap into one continuous interval -> Option B