Challenge - 5 Problems
Merge Intervals Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of merging intervals with overlapping pairs
What is the output of the following Java code that merges overlapping intervals?
DSA Java
import java.util.*; public class Main { public static List<int[]> mergeIntervals(int[][] intervals) { if (intervals.length == 0) return new ArrayList<>(); Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); List<int[]> merged = new ArrayList<>(); int[] current = intervals[0]; for (int i = 1; i < intervals.length; i++) { if (intervals[i][0] <= current[1]) { current[1] = Math.max(current[1], intervals[i][1]); } else { merged.add(current); current = intervals[i]; } } merged.add(current); return merged; } public static void main(String[] args) { int[][] intervals = {{1,3},{2,6},{8,10},{15,18}}; List<int[]> result = mergeIntervals(intervals); for (int[] interval : result) { System.out.print("[" + interval[0] + "," + interval[1] + "] "); } } }
Attempts:
2 left
💡 Hint
Think about how intervals that overlap are combined into one.
✗ Incorrect
The code sorts intervals by start time, then merges overlapping intervals by extending the end time. Intervals [1,3] and [2,6] overlap, so they merge into [1,6]. Others remain separate.
❓ Predict Output
intermediateResult of merging intervals with nested overlaps
What is the output of this Java code merging nested overlapping intervals?
DSA Java
import java.util.*; public class Main { public static List<int[]> mergeIntervals(int[][] intervals) { Arrays.sort(intervals, Comparator.comparingInt(a -> a[0])); List<int[]> merged = new ArrayList<>(); int[] current = intervals[0]; for (int i = 1; i < intervals.length; i++) { if (intervals[i][0] <= current[1]) { current[1] = Math.max(current[1], intervals[i][1]); } else { merged.add(current); current = intervals[i]; } } merged.add(current); return merged; } public static void main(String[] args) { int[][] intervals = {{1,10},{2,5},{6,9},{11,15}}; List<int[]> result = mergeIntervals(intervals); for (int[] interval : result) { System.out.print("[" + interval[0] + "," + interval[1] + "] "); } } }
Attempts:
2 left
💡 Hint
Nested intervals merge into the largest covering interval.
✗ Incorrect
Intervals [1,10], [2,5], and [6,9] overlap and merge into [1,10]. Interval [11,15] is separate.
🔧 Debug
advancedIdentify the error in merging intervals code
What error does this Java code produce when merging intervals?
DSA Java
import java.util.*; public class Main { public static List<int[]> mergeIntervals(int[][] intervals) { Arrays.sort(intervals, (a, b) -> a[0] - b[0]); List<int[]> merged = new ArrayList<>(); int[] current = intervals[0]; for (int i = 1; i < intervals.length; i++) { if (intervals[i][0] <= current[1]) { current[1] = Math.max(current[1], intervals[i][1]); } else { merged.add(current); current = intervals[i]; } } merged.add(current); return merged; } public static void main(String[] args) { int[][] intervals = {{1,4},{4,5}}; List<int[]> result = mergeIntervals(intervals); for (int[] interval : result) { System.out.print("[" + interval[0] + "," + interval[1] + "] "); } } }
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
The for loop uses i <= intervals.length which causes an index out of bounds when i equals intervals.length.
🚀 Application
advancedNumber of merged intervals after merging
Given intervals {{1,4},{2,3},{5,7},{6,8},{9,10}}, how many intervals remain after merging overlapping ones?
Attempts:
2 left
💡 Hint
Merge intervals that overlap or touch.
✗ Incorrect
Intervals [1,4] and [2,3] merge into [1,4]. Intervals [5,7] and [6,8] merge into [5,8]. Interval [9,10] remains separate. Total 3 intervals.
🧠 Conceptual
expertCorrect order of steps to merge intervals
What is the correct order of steps to merge overlapping intervals?
Attempts:
2 left
💡 Hint
Think about what must happen first before merging.
✗ Incorrect
You must first sort intervals, then initialize merged list, then iterate and merge or add intervals.
