Challenge - 5 Problems
Interval Mastery
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 Go code that calculates the minimum number of intervals to remove to make the rest non-overlapping?
DSA Go
package main import ( "fmt" "sort" ) type Interval struct { start int end int } func eraseOverlapIntervals(intervals []Interval) int { if len(intervals) == 0 { return 0 } sort.Slice(intervals, func(i, j int) bool { return intervals[i].end < intervals[j].end }) count := 0 end := intervals[0].end for i := 1; i < len(intervals); i++ { if intervals[i].start < end { count++ } else { end = intervals[i].end } } return count } func main() { intervals := []Interval{{1, 3}, {2, 4}, {3, 5}, {6, 7}} fmt.Println(eraseOverlapIntervals(intervals)) }
Attempts:
2 left
💡 Hint
Sort intervals by their end time and count overlaps.
✗ Incorrect
The code sorts intervals by end time and counts how many intervals overlap with the previous chosen interval. Only one interval needs removal to avoid overlaps.
❓ Predict Output
intermediateResult after removing minimum intervals
Given the intervals, what is the count of intervals remaining after removing the minimum number to avoid overlaps?
DSA Go
package main import ( "fmt" "sort" ) type Interval struct { start int end int } func eraseOverlapIntervals(intervals []Interval) int { if len(intervals) == 0 { return 0 } sort.Slice(intervals, func(i, j int) bool { return intervals[i].end < intervals[j].end }) count := 0 end := intervals[0].end for i := 1; i < len(intervals); i++ { if intervals[i].start < end { count++ } else { end = intervals[i].end } } return len(intervals) - count } func main() { intervals := []Interval{{1, 2}, {2, 3}, {3, 4}, {1, 3}} fmt.Println(eraseOverlapIntervals(intervals)) }
Attempts:
2 left
💡 Hint
Calculate total intervals minus minimum removals.
✗ Incorrect
The code returns total intervals minus the count of removals needed. Here, only one interval overlaps, so 4 - 1 = 3 remain.
🧠 Conceptual
advancedWhy sort intervals by end time in minimum removal problem?
Why is sorting intervals by their end time the best approach to minimize the number of intervals removed to avoid overlaps?
Attempts:
2 left
💡 Hint
Think about how choosing intervals affects space for others.
✗ Incorrect
Sorting by end time allows greedy selection of intervals that finish earliest, maximizing space for others and minimizing removals.
🔧 Debug
advancedIdentify the bug in interval removal code
What error will the following Go code produce when run, and why?
DSA Go
package main import ( "fmt" "sort" ) type Interval struct { start int end int } func eraseOverlapIntervals(intervals []Interval) int { if len(intervals) == 0 { return 0 } sort.Slice(intervals, func(i, j int) bool { return intervals[i].start < intervals[j].start }) count := 0 end := intervals[0].end for i := 1; i < len(intervals); i++ { if intervals[i].start < end { count++ } else { end = intervals[i].end } } return count } func main() { intervals := []Interval{{1,5}, {2,3}, {3,4}} fmt.Println(eraseOverlapIntervals(intervals)) }
Attempts:
2 left
💡 Hint
Check the sorting criteria and how it affects overlap counting.
✗ Incorrect
Sorting by start time instead of end time causes the greedy approach to fail, producing wrong count (2 instead of 1) though code runs without error.
🚀 Application
expertMinimum removals for large interval set
Given 1,000 intervals where each interval i is [i, i+2], what is the minimum number of intervals to remove to make the rest non-overlapping?
Attempts:
2 left
💡 Hint
Intervals overlap with next one; think about maximum non-overlapping intervals.
✗ Incorrect
Each interval overlaps with the next one. Maximum non-overlapping intervals can be selected by picking every other interval, keeping 500 and removing 500.
