Challenge - 5 Problems
Interval Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Merging Overlapping Intervals
What is the output of the following Java code that merges overlapping intervals?
DSA Java
import java.util.*; class Interval { int start, end; Interval(int s, int e) { start = s; end = e; } public String toString() { return start + "->" + end; } } public class Main { public static List<Interval> mergeIntervals(List<Interval> intervals) { if (intervals.size() <= 1) return intervals; intervals.sort(Comparator.comparingInt(i -> i.start)); List<Interval> merged = new ArrayList<>(); Interval last = intervals.get(0); for (int i = 1; i < intervals.size(); i++) { Interval current = intervals.get(i); if (current.start <= last.end) { last.end = Math.max(last.end, current.end); } else { merged.add(last); last = current; } } merged.add(last); return merged; } public static void main(String[] args) { List<Interval> intervals = Arrays.asList( new Interval(1, 3), new Interval(2, 6), new Interval(8, 10), new Interval(15, 18) ); List<Interval> result = mergeIntervals(intervals); for (Interval interval : result) { System.out.print(interval + " "); } } }
Attempts:
2 left
💡 Hint
Think about how overlapping intervals are combined into one.
✗ Incorrect
The code sorts intervals by start time, then merges overlapping ones by extending the end of the last interval. Intervals (1,3) and (2,6) overlap and merge into (1,6). Others remain separate.
🧠 Conceptual
intermediateWhy Are Intervals Important in Algorithms?
Why are interval problems common and important in algorithm challenges?
Attempts:
2 left
💡 Hint
Think about real-life situations like booking rooms or scheduling.
✗ Incorrect
Intervals represent ranges like time slots or numeric ranges. They often overlap, so algorithms must efficiently merge or find gaps. This makes interval problems common and important.
🔧 Debug
advancedIdentify the Error in Interval Overlap Check
What error does the following code produce when checking if two intervals overlap?
Code snippet:
if (interval1.end < interval2.start && interval2.end < interval1.start) {
System.out.println("No overlap");
} else {
System.out.println("Overlap");
}
DSA Java
if (interval1.end < interval2.start && interval2.end < interval1.start) { System.out.println("No overlap"); } else { System.out.println("Overlap"); }
Attempts:
2 left
💡 Hint
Check the logic of the condition carefully.
✗ Incorrect
The condition uses && but the intervals cannot satisfy both interval1.end < interval2.start and interval2.end < interval1.start at the same time. It should use || to detect no overlap.
🚀 Application
advancedFind Maximum Number of Overlapping Intervals
Given a list of intervals, which approach correctly finds the maximum number of intervals overlapping at any point?
Attempts:
2 left
💡 Hint
Think about scanning through all start and end points in order.
✗ Incorrect
Sorting start and end times separately and using two pointers allows counting how many intervals are active at each point, finding maximum overlap efficiently.
❓ Predict Output
expertOutput of Interval Scheduling Maximum Set
What is the output of the following Java code that finds the maximum number of non-overlapping intervals?
DSA Java
import java.util.*; class Interval implements Comparable<Interval> { int start, end; Interval(int s, int e) { start = s; end = e; } public int compareTo(Interval other) { return this.end - other.end; } public String toString() { return start + "->" + end; } } public class Main { public static List<Interval> maxNonOverlapping(List<Interval> intervals) { Collections.sort(intervals); List<Interval> result = new ArrayList<>(); int lastEnd = Integer.MIN_VALUE; for (Interval interval : intervals) { if (interval.start >= lastEnd) { result.add(interval); lastEnd = interval.end; } } return result; } public static void main(String[] args) { List<Interval> intervals = Arrays.asList( new Interval(1, 4), new Interval(3, 5), new Interval(0, 6), new Interval(5, 7), new Interval(8, 9), new Interval(5, 9) ); List<Interval> result = maxNonOverlapping(intervals); for (Interval interval : result) { System.out.print(interval + " "); } } }
Attempts:
2 left
💡 Hint
The algorithm picks intervals by earliest finish time without overlap.
✗ Incorrect
The code sorts intervals by end time and greedily selects intervals starting after the last selected interval ends. The selected intervals are (1,4), (5,7), and (8,9).
