Challenge - 5 Problems
Interval Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of minimum removals for overlapping intervals
What is the output of the following Java code that calculates the minimum number of intervals to remove to make the rest non-overlapping?
DSA Java
import java.util.*; public class Solution { public static int eraseOverlapIntervals(int[][] intervals) { if (intervals.length == 0) return 0; Arrays.sort(intervals, (a, b) -> Integer.compare(a[1], b[1])); int count = 0; int end = intervals[0][1]; for (int i = 1; i < intervals.length; i++) { if (intervals[i][0] < end) { count++; } else { end = intervals[i][1]; } } return count; } public static void main(String[] args) { int[][] intervals = {{1,3},{2,4},{3,5},{6,7}}; System.out.println(eraseOverlapIntervals(intervals)); } }
Attempts:
2 left
💡 Hint
Sort intervals by their end time and count overlaps.
✗ Incorrect
The code sorts intervals by their end time. It keeps track of the end of the last added interval. If the next interval starts before this end, it counts as an overlap and increments removal count. Here, only one interval overlaps.
🧠 Conceptual
intermediateUnderstanding the greedy choice in interval removal
Why does sorting intervals by their end time help in minimizing the number of intervals to remove to avoid overlaps?
Attempts:
2 left
💡 Hint
Think about how choosing intervals that finish early affects the rest.
✗ Incorrect
Choosing intervals that end earliest leaves maximum space for the remaining intervals, reducing overlaps and removals.
🔧 Debug
advancedIdentify the error in interval removal code
What error will the following Java code produce when run, and why?
DSA Java
import java.util.*; public class Solution { public static int eraseOverlapIntervals(int[][] intervals) { Arrays.sort(intervals, (a, b) -> a[1] - b[1]); int count = 0; int end = intervals[0][1]; for (int i = 1; i < intervals.length; i++) { if (intervals[i][0] < end) { count++; } else { end = intervals[i][1]; } } return count; } public static void main(String[] args) { int[][] intervals = {}; System.out.println(eraseOverlapIntervals(intervals)); } }
Attempts:
2 left
💡 Hint
Check what happens when intervals length is zero.
✗ Incorrect
The code accesses intervals[0][1] without checking if intervals is empty, causing ArrayIndexOutOfBoundsException.
❓ Predict Output
advancedOutput of minimum removals with overlapping intervals
What is the output of this Java code that finds the minimum number of intervals to remove?
DSA Java
import java.util.*; public class Solution { public static int eraseOverlapIntervals(int[][] intervals) { if (intervals.length == 0) return 0; Arrays.sort(intervals, Comparator.comparingInt(a -> a[1])); int count = 0; int end = intervals[0][1]; for (int i = 1; i < intervals.length; i++) { if (intervals[i][0] < end) { count++; } else { end = intervals[i][1]; } } return count; } public static void main(String[] args) { int[][] intervals = {{1,2},{2,3},{3,4},{1,3}}; System.out.println(eraseOverlapIntervals(intervals)); } }
Attempts:
2 left
💡 Hint
Sort by end time and count overlaps.
✗ Incorrect
Only the interval {1,3} overlaps with others and needs removal.
🚀 Application
expertMinimum removals for large interval set
Given 1 million intervals where each interval is [i, i+2] for i from 0 to 999999, what is the minimum number of intervals to remove to make the rest non-overlapping?
Attempts:
2 left
💡 Hint
Consider how intervals overlap and how many can fit without overlapping.
✗ Incorrect
Each interval overlaps with the next one. We can keep every other interval to avoid overlaps, so half must be removed.
