What if you could add new time slots perfectly without worrying about overlaps or order?
Why Insert Interval into Sorted List in DSA Java?
Imagine you have a list of time slots booked for meetings, sorted by start time. Now, you want to add a new meeting without messing up the order or overlapping existing meetings.
Manually checking each time slot to find where the new meeting fits is slow and confusing. You might forget to merge overlapping meetings or place the new one in the wrong spot, causing double bookings or disorder.
Using the Insert Interval method, you can quickly find the right place for the new meeting, merge any overlaps automatically, and keep the list sorted and clean without extra effort.
for (int i = 0; i < intervals.length; i++) { if (newInterval.start < intervals[i].start) { // insert here manually } // check overlaps manually }
List<Interval> result = new ArrayList<>(); for (Interval interval : intervals) { if (interval.end < newInterval.start) result.add(interval); else if (interval.start > newInterval.end) { result.add(newInterval); newInterval = interval; } else { newInterval.start = Math.min(newInterval.start, interval.start); newInterval.end = Math.max(newInterval.end, interval.end); } } result.add(newInterval);
This method lets you manage schedules or ranges efficiently, ensuring no overlaps and keeping everything in order automatically.
Adding a new appointment to a doctor's daily schedule without causing conflicts or disorder.
Manual insertion is error-prone and slow.
Insert Interval merges overlaps and keeps order automatically.
Useful for managing time slots, ranges, or any sorted intervals.
Practice
Solution
Step 1: Understand the problem context
The list contains sorted, non-overlapping intervals. When inserting a new interval, we must keep the list sorted and merge any overlapping intervals.Step 2: Identify correct insertion behavior
Simply adding the new interval without merging or sorting breaks the list's properties. Proper merging keeps intervals non-overlapping and sorted.Final Answer:
Merge the new interval with overlapping intervals and keep the list sorted -> Option AQuick Check:
Insert interval = Merge and sort [OK]
- Inserting without merging overlaps
- Appending interval without sorting
- Removing intervals unnecessarily
Solution
Step 1: Analyze parameter types for intervals
Intervals are pairs of integers, so List is appropriate to hold multiple intervals. The new interval is an int[] of size 2.Step 2: Check return type and parameters
The method returns a List after insertion. public List insertInterval(List intervals, int[] newInterval) matches this signature correctly.Final Answer:
public List insertInterval(List intervals, int[] newInterval) -> Option BQuick Check:
Correct method signature uses List and int[] [OK]
- Using int[] instead of List for intervals
- Wrong return type like void or int
- Using List<int> instead of List
Solution
Step 1: Identify overlapping intervals
The new interval [2,5] overlaps with [1,3] because 2 ≤ 3. So they merge into [1,5].Step 2: Merge and keep other intervals
After merging, the list becomes [[1,5],[6,9]] as [6,9] does not overlap.Final Answer:
[[1,5],[6,9]] -> Option CQuick Check:
Merged intervals = [[1,5],[6,9]] [OK]
- Not merging overlapping intervals
- Appending new interval without sorting
- Incorrectly splitting intervals
List insertInterval(List intervals, int[] newInterval) {
List result = new ArrayList<>();
for (int[] interval : intervals) {
if (interval[1] < newInterval[0]) {
result.add(interval);
} else if (interval[0] > newInterval[1]) {
result.add(newInterval);
newInterval = interval; // Fix to avoid multiple additions
} else {
newInterval[0] = Math.min(newInterval[0], interval[0]);
newInterval[1] = Math.max(newInterval[1], interval[1]);
}
}
result.add(newInterval);
return result;
}
What is the bug in this code?Solution
Step 1: Analyze insertion of newInterval in else-if block
When interval[0] > newInterval[1], it adds newInterval to result, but continues looping, causing newInterval to be added again later.Step 2: Identify repeated addition of newInterval
Because newInterval is added again after the loop, it can be added multiple times, causing duplicates.Final Answer:
It adds newInterval multiple times if intervals after it exist -> Option AQuick Check:
Repeated addition of newInterval causes duplicates [OK]
- Adding newInterval inside loop multiple times
- Not updating newInterval correctly
- Forgetting to add remaining intervals
Solution
Step 1: Identify intervals overlapping with [4,8]
Intervals [3,5], [6,7], and [8,10] overlap with [4,8] because their ranges intersect.Step 2: Merge overlapping intervals
Merge [3,5], [6,7], [8,10], and [4,8] into one interval [3,10]. The others remain unchanged.Step 3: Construct final list
The final list is [[1,2],[3,10],[12,16]].Final Answer:
[[1,2],[3,10],[12,16]] -> Option DQuick Check:
Merged intervals = [[1,2],[3,10],[12,16]] [OK]
- Not merging all overlapping intervals
- Leaving duplicates of overlapping intervals
- Incorrectly splitting merged intervals
Ready to prove it?
Pass the test and this pattern moves unknown
