Challenge - 5 Problems
Interval Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting an interval that merges with existing intervals
What is the output of the intervals list after inserting [4, 8] into [[1, 3], [6, 9]]?
DSA Java
import java.util.*; public class Solution { public static List<int[]> insertInterval(List<int[]> intervals, int[] newInterval) { List<int[]> result = new ArrayList<>(); int i = 0; while (i < intervals.size() && intervals.get(i)[1] < newInterval[0]) { result.add(intervals.get(i)); i++; } while (i < intervals.size() && intervals.get(i)[0] <= newInterval[1]) { newInterval[0] = Math.min(newInterval[0], intervals.get(i)[0]); newInterval[1] = Math.max(newInterval[1], intervals.get(i)[1]); i++; } result.add(newInterval); while (i < intervals.size()) { result.add(intervals.get(i)); i++; } return result; } public static void main(String[] args) { List<int[]> intervals = new ArrayList<>(); intervals.add(new int[]{1, 3}); intervals.add(new int[]{6, 9}); int[] newInterval = {4, 8}; List<int[]> result = insertInterval(intervals, newInterval); for (int[] interval : result) { System.out.print("[" + interval[0] + ", " + interval[1] + "] "); } } }
Attempts:
2 left
💡 Hint
Think about how intervals that overlap with the new interval are merged.
✗ Incorrect
The new interval [4, 8] overlaps with [6, 9], so they merge into [4, 9]. The first interval [1, 3] does not overlap and remains unchanged.
❓ Predict Output
intermediateOutput after inserting an interval that does not overlap
What is the output of the intervals list after inserting [10, 12] into [[1, 5], [6, 9]]?
DSA Java
import java.util.*; public class Solution { public static List<int[]> insertInterval(List<int[]> intervals, int[] newInterval) { List<int[]> result = new ArrayList<>(); int i = 0; while (i < intervals.size() && intervals.get(i)[1] < newInterval[0]) { result.add(intervals.get(i)); i++; } while (i < intervals.size() && intervals.get(i)[0] <= newInterval[1]) { newInterval[0] = Math.min(newInterval[0], intervals.get(i)[0]); newInterval[1] = Math.max(newInterval[1], intervals.get(i)[1]); i++; } result.add(newInterval); while (i < intervals.size()) { result.add(intervals.get(i)); i++; } return result; } public static void main(String[] args) { List<int[]> intervals = new ArrayList<>(); intervals.add(new int[]{1, 5}); intervals.add(new int[]{6, 9}); int[] newInterval = {10, 12}; List<int[]> result = insertInterval(intervals, newInterval); for (int[] interval : result) { System.out.print("[" + interval[0] + ", " + interval[1] + "] "); } } }
Attempts:
2 left
💡 Hint
New interval does not overlap, so it should be added at the end.
✗ Incorrect
Since [10, 12] does not overlap with any existing intervals, it is simply added at the end.
🔧 Debug
advancedIdentify the error in interval insertion code
What error will this code produce when inserting [2, 5] into [[1, 3], [6, 9]]?
Code snippet:
List intervals = new ArrayList<>();
intervals.add(new int[]{1, 3});
intervals.add(new int[]{6, 9});
int[] newInterval = {2, 5};
List result = insertInterval(intervals, newInterval);
public static List insertInterval(List intervals, int[] newInterval) {
List result = new ArrayList<>();
int i = 0;
while (i < intervals.size() && intervals.get(i)[1] <= newInterval[0]) {
result.add(intervals.get(i));
i++;
}
while (i < intervals.size() && intervals.get(i)[0] < newInterval[1]) {
newInterval[0] = Math.min(newInterval[0], intervals.get(i)[0]);
newInterval[1] = Math.max(newInterval[1], intervals.get(i)[1]);
i++;
}
result.add(newInterval);
while (i < intervals.size()) {
result.add(intervals.get(i));
i++;
}
return result;
}
Attempts:
2 left
💡 Hint
Check the comparison operators in the while loops carefully.
✗ Incorrect
The conditions use <= and < incorrectly, causing the first interval [1,3] to be added before merging, and the new interval added at the end, resulting in wrong output.
🧠 Conceptual
advancedUnderstanding interval insertion complexity
What is the time complexity of inserting a new interval into a sorted list of n non-overlapping intervals using the standard merge approach?
Attempts:
2 left
💡 Hint
Consider how many intervals you might need to check or merge.
✗ Incorrect
Even though the list is sorted, we may need to scan all intervals to merge overlapping ones, so the complexity is linear O(n).
🚀 Application
expertResult after multiple insertions with merges
Given an initially empty list, insert intervals in this order: [1, 2], [3, 5], [4, 7], [6, 8]. What is the final list of intervals?
Attempts:
2 left
💡 Hint
Merge intervals as you insert if they overlap.
✗ Incorrect
After inserting [1, 2], list is [[1, 2]]. Insert [3, 5], list is [[1, 2], [3, 5]]. Insert [4, 7], merges with [3, 5] to become [3, 7]. Insert [6, 8], merges with [3, 7] to become [3, 8]. Final list: [[1, 2], [3, 8]].
