Interval Scheduling Implementation in Rust
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 Rust as in every other language.
- ✓slice::sort_unstable_by_key and slice::sort_by_key both live directly on [T] (no crate needed).
- ✓For interval scheduling, prefer sort_unstable_by_key(|iv| iv.end): the key is a plain i32 field, essentially free to re-derive on every comparison, and the algorithm's correctness never depends on how equal-end ties are ordered relative to each other, so there is nothing to gain from paying for stability.
- ✓The Rust trap: sort_by_key does NOT cache the key: it re-invokes your closure on every comparison the sort performs, not once per element.
- ✓sort_unstable_by_key is pdqsort: O(n log n) expected, allocates nothing, which is what actually makes the O(1) auxiliary-space bound in this topic's metadata literally true rather than approximately true.
What Interval Scheduling actually is
Interval Scheduling in Rust: what you actually reach for
Rust implementation
#[derive(Clone, Copy, Debug)]
struct Interval {
start: i32,
end: i32,
}
fn max_non_overlapping(intervals: &mut [Interval]) -> Vec<Interval> {
// unstable + allocation-free: matches the O(1) auxiliary space bound;
// stability doesn't matter because tied ends don't change the optimal count
intervals.sort_unstable_by_key(|iv| iv.end);
let mut selected = Vec::new();
let mut last_end = i32::MIN;
for &iv in intervals.iter() {
if iv.start >= last_end {
selected.push(iv);
last_end = iv.end;
}
}
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 Rust-specific trap
Performance in Rust, beyond the O(n log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Rust type or library should I use for Interval Scheduling?
slice::sort_unstable_by_key and slice::sort_by_key both live directly on [T] (no crate needed). sort_unstable_by_key runs pattern-defeating quicksort (pdqsort): in-place, no heap allocation, not stable. sort_by_key runs a stable, adaptive merge sort that allocates a temporary buffer to hold the merge state. sort_by_cached_key exists for when the key itself is expensive to compute: it evaluates the key function exactly once per element into a temporary Vec instead of re-deriving it on every comparison.
What is the most common Rust mistake when implementing Interval Scheduling?
sort_by_key does NOT cache the key: it re-invokes your closure on every comparison the sort performs, not once per element. That is invisible and harmless here because the key is a bare field read, but the same code pattern generalized to a derived or computed key (a weighted score, a string built from several fields) silently turns an O(n) amount of key work into O(n log n) worth of repeated recomputation. sort_by_cached_key is the documented fix, and reaching for it trades away the O(1) auxiliary-space bound for an O(n) key buffer, which is exactly the trade this topic's stated complexities are pointing at.
How does Interval Scheduling actually perform in Rust?
sort_unstable_by_key is pdqsort: O(n log n) expected, allocates nothing, which is what actually makes the O(1) auxiliary-space bound in this topic's metadata literally true rather than approximately true. sort_by_key is also O(n log n) but is stable and pays for that with a temporary merge buffer, so choosing between the two _is_ choosing whether you hit the stated space bound or trade it away for a stability guarantee this particular algorithm does not need. 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 trialIf you are a student, see what Devin offers students for free 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.