Interval Scheduling Implementation in Java
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Interval Scheduling runs in O(n log n) time and O(1) space, in Java as in every other language.
- ✓Arrays.sort(T[], Comparator<? super T>) and List.sort(Comparator) both run a modified TimSort for object references, guaranteed stable and O(n log n) worst case.
- ✓Sort with Comparator.comparingInt(Interval::end) rather than Comparator.comparing(iv -> (Integer) iv.end()): comparingInt keeps the comparison on the primitive int the whole way through instead of boxing an Integer object per comparison.
- ✓The Java trap: TimSort, the algorithm behind Arrays.sort for object arrays, actively checks for comparator consistency while it merges runs, and throws IllegalArgumentException with the message "Comparison method violates its general contract!" at runtime once the array is large enough for that check to trigger, if your comparator does not define a valid total order.
- ✓The object-array TimSort path guarantees O(n log n) worst case and, like Python's Timsort (which it was directly inspired by), is adaptive over existing runs in the input.
What Interval Scheduling actually is
Interval Scheduling in Java: what you actually reach for
Java implementation
import java.util.*;
class IntervalScheduling {
record Interval(int start, int end) {}
static List<Interval> maxNonOverlapping(List<Interval> intervals) {
List<Interval> sorted = new ArrayList<>(intervals);
// TimSort: guaranteed O(n log n) worst case, and throws IllegalArgumentException
// at runtime if the comparator is not a valid total order
sorted.sort(Comparator.comparingInt(Interval::end));
List<Interval> selected = new ArrayList<>();
int lastEnd = Integer.MIN_VALUE;
for (Interval iv : sorted) {
if (iv.start() >= lastEnd) {
selected.add(iv);
lastEnd = iv.end();
}
}
return selected;
}
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Java-specific trap
Performance in Java, beyond the O(n log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Java type or library should I use for Interval Scheduling?
Arrays.sort(T[], Comparator<? super T>) and List.sort(Comparator) both run a modified TimSort for object references, guaranteed stable and O(n log n) worst case. Arrays.sort(int[]) for a raw primitive array instead runs a Dual-Pivot Quicksort, which the JDK documents as typically outperforming a classic single-pivot quicksort but does not carry TimSort's worst-case guarantee. Comparator.comparingInt(Interval::end) builds a key extractor without boxing the int into an Integer.
What is the most common Java mistake when implementing Interval Scheduling?
TimSort, the algorithm behind Arrays.sort for object arrays, actively checks for comparator consistency while it merges runs, and throws IllegalArgumentException with the message "Comparison method violates its general contract!" at runtime once the array is large enough for that check to trigger, if your comparator does not define a valid total order. It is the one language on this page where a broken comparator is guaranteed to surface as a loud exception instead of a silently wrong schedule.
How does Interval Scheduling actually perform in Java?
The object-array TimSort path guarantees O(n log n) worst case and, like Python's Timsort (which it was directly inspired by), is adaptive over existing runs in the input. The primitive int[] Dual-Pivot Quicksort path has no comparator call overhead at all (no boxing, no virtual dispatch through a Comparator), so if Interval collapses to two parallel int[] arrays instead of an array of Interval objects, sorting is faster and touches far fewer cache lines than chasing object references through the heap. Time complexity is O(n log n) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for Replit, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.