Interval Scheduling Implementation in Go
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 Go as in every other language.
- ✓slices.SortFunc, from the generics-based slices package (Go 1.21+), sorts any []E using a three-way comparator; cmp.Compare, from the cmp package, supplies that comparator for any ordered type.
- ✓Reach for slices.SortFunc over sort.Slice on any codebase targeting Go 1.21+: it is monomorphized per element type by the generics compiler instead of going through reflection, and the comparator returning an int via cmp.Compare(a.End, b.End) matches the same three-way-comparison convention used across sort, slices, and cmp.
- ✓The Go trap: Neither sort.Slice nor slices.SortFunc validates that your comparator defines a strict weak ordering, and neither panics if it does not, an inconsistent less/compare function (for example one that accidentally compares Start on one branch and End on another) just produces a slice that is not correctly sorted, with no error, no warning, and no stack trace to point at the bug.
- ✓sort.Slice's reflection path swaps elements through reflect.Swapper, which is measurably slower than a type-specific swap because every element access goes through an interface and a runtime type check.
What Interval Scheduling actually is
Interval Scheduling in Go: what you actually reach for
Go implementation
package main
import (
"cmp"
"slices"
)
type Interval struct {
Start, End int
}
func MaxNonOverlapping(intervals []Interval) []Interval {
// slices.SortFunc: generic, no reflection (unlike sort.Slice); in place, no allocation
slices.SortFunc(intervals, func(a, b Interval) int {
return cmp.Compare(a.End, b.End)
})
selected := make([]Interval, 0, len(intervals))
lastEnd := -1 << 31
for _, iv := range intervals {
if iv.Start >= lastEnd {
selected = append(selected, 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 Go-specific trap
Performance in Go, beyond the O(n log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Go type or library should I use for Interval Scheduling?
slices.SortFunc, from the generics-based slices package (Go 1.21+), sorts any []E using a three-way comparator; cmp.Compare, from the cmp package, supplies that comparator for any ordered type. The older sort.Slice (sort package) takes an any and a less func(i, j int) bool, and reaches that generality through reflection under the hood.
What is the most common Go mistake when implementing Interval Scheduling?
Neither sort.Slice nor slices.SortFunc validates that your comparator defines a strict weak ordering, and neither panics if it does not, an inconsistent less/compare function (for example one that accidentally compares Start on one branch and End on another) just produces a slice that is not correctly sorted, with no error, no warning, and no stack trace to point at the bug. That is a real, load-bearing difference from Java's TimSort on this same page, which actively detects an inconsistent comparator at runtime and throws.
How does Interval Scheduling actually perform in Go?
sort.Slice's reflection path swaps elements through reflect.Swapper, which is measurably slower than a type-specific swap because every element access goes through an interface and a runtime type check. slices.SortFunc avoids that entirely: Go's generics compile via GC-shape stenciling with a runtime dictionary (one compiled body shared by every type with the same underlying memory layout, not a fresh copy per concrete type the way Rust or C++ templates work), but even that dictionary-passing path uses directly-compiled element access and swaps instead of reflect.Swapper, so it stays categorically faster than sort.Slice's reflection. Since Go 1.19 the underlying sort algorithm for both is a pattern-defeating quicksort variant, in place, with no extra allocation beyond the small recursion stack. 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 full price for a tool, check the student deals on AI and developer tools listed 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.