Challenge - 5 Problems
Interval Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Minimum Interval Removals Calculation
What is the output of the following C# code that calculates the minimum number of intervals to remove to avoid overlaps?
DSA C#
using System; using System.Collections.Generic; public class Solution { public int EraseOverlapIntervals(int[][] intervals) { if (intervals.Length == 0) return 0; Array.Sort(intervals, (a, b) => a[1].CompareTo(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 class Program { public static void Main() { int[][] intervals = new int[][] { new int[] {1, 3}, new int[] {2, 4}, new int[] {3, 5}, new int[] {6, 7} }; Solution sol = new Solution(); Console.WriteLine(sol.EraseOverlapIntervals(intervals)); } }
Attempts:
2 left
💡 Hint
Sort intervals by their end time and count overlaps.
✗ Incorrect
The code sorts intervals by their end time and counts how many intervals overlap with the previous chosen interval. Only one interval needs to be removed to avoid 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 earlier affects the rest.
✗ Incorrect
Choosing intervals with the earliest end time allows more intervals to fit after it without overlapping, minimizing removals.
🔧 Debug
advancedIdentify the Bug in Interval Removal Code
What error will the following code produce when run, and why?
DSA C#
public int EraseOverlapIntervals(int[][] intervals) { Array.Sort(intervals, (a, b) => a[0].CompareTo(b[0])); 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; }
Attempts:
2 left
💡 Hint
Consider what happens if intervals array is empty.
✗ Incorrect
If intervals is empty, accessing intervals[0] causes IndexOutOfRangeException.
❓ Predict Output
advancedOutput of Interval Removal with Nested Intervals
What is the output of the following code for nested intervals?
DSA C#
int[][] intervals = new int[][] { new int[] {1, 10}, new int[] {2, 3}, new int[] {4, 5}, new int[] {6, 7}, new int[] {8, 9} }; Solution sol = new Solution(); Console.WriteLine(sol.EraseOverlapIntervals(intervals));
Attempts:
2 left
💡 Hint
The large interval overlaps with all smaller intervals inside it.
✗ Incorrect
After sorting by end time, the greedy algorithm selects the four small non-overlapping intervals [2,3], [4,5], [6,7], [8,9] and removes the large [1,10], resulting in 1 removal.
🚀 Application
expertMinimum Removals for Overlapping Intervals with Same End Time
Given intervals with some having the same end time, what is the minimum number of intervals to remove?
DSA C#
int[][] intervals = new int[][] { new int[] {1, 3}, new int[] {2, 3}, new int[] {3, 4}, new int[] {3, 4}, new int[] {4, 5} }; Solution sol = new Solution(); Console.WriteLine(sol.EraseOverlapIntervals(intervals));
Attempts:
2 left
💡 Hint
Intervals with same end time can cause overlaps if start times overlap.
✗ Incorrect
Intervals [1,3] and [2,3] overlap, and [3,4] duplicates overlap. Minimum removals is 2.
