First interval [1,3] added. Second interval [2,6] overlaps with [1,3], merge to [1,6]. Third interval [8,10] no overlap, add. Fourth interval [15,18] no overlap, add.
Final Answer:
[[1,6],[8,10],[15,18]] -> Option C
Quick Check:
Merged intervals = [[1,6],[8,10],[15,18]] [OK]
Hint: Sort first, then merge overlapping intervals step-by-step [OK]
Common Mistakes:
Not sorting intervals before merging
Overwriting intervals incorrectly
Missing to merge overlapping intervals
4. The following TypeScript function is intended to merge overlapping intervals but has a bug. What is the bug?
function merge(intervals: number[][]): number[][] {
intervals.sort((a, b) => a[0] - b[0]);
const result: number[][] = [];
for (let i = 0; i < intervals.length; i++) {
if (result.length === 0 || result[result.length - 1][1] <= intervals[i][0]) {
result.push(intervals[i]);
} else {
result[result.length - 1][1] = Math.max(result[result.length - 1][1], intervals[i][1]);
}
}
return result;
}
medium
A. The max function is used incorrectly
B. The condition should use '<' instead of '<=' to detect overlap
C. The function does not handle empty input
D. The intervals are not sorted before merging
Solution
Step 1: Analyze overlap condition
Condition uses '<=' which treats touching intervals as non-overlapping.
Step 2: Correct overlap detection
Correct condition uses '<' so that when last_end == current_start (touching), it merges since the if condition fails and goes to else. Current '<=' incorrectly pushes new for touching intervals.
Final Answer:
The condition should use '<' instead of '<=' to detect overlap -> Option B
Quick Check:
Overlap check needs '<' not '<=' [OK]
Hint: Use '<' to detect overlap, not '<=' [OK]
Common Mistakes:
Using '<=' causes missing merges on touching intervals
Ignoring sorting requirement
Not checking empty input but that is not the bug here
5. Given a list of intervals, how would you modify the merge overlapping intervals algorithm to also count how many original intervals were merged into each resulting interval? Choose the best approach.
hard
A. Keep a count alongside each merged interval and increment it when merging overlaps
B. Sort intervals by end time instead of start time
C. Use a separate array to store counts and update only after merging all intervals
D. Merge intervals normally and count the total number of intervals merged at the end
Solution
Step 1: Track counts during merging
When merging intervals, keep a count for each merged interval starting at 1.
Step 2: Increment count on overlap
Each time an interval merges into the last merged interval, increment its count to reflect how many intervals combined.
Final Answer:
Keep a count alongside each merged interval and increment it when merging overlaps -> Option A
Quick Check:
Count merges while merging intervals [OK]
Hint: Track counts during merge, increment on overlap [OK]
Common Mistakes:
Sorting by end time does not help counting merges
Counting after merging loses interval mapping
Counting total merges instead of per merged interval
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.