search for interval overlap in list of intervals?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Interval overlap search appears in scheduling, booking systems, memory allocators, and genomic analysis. The right algorithm depends on whether you need one overlap, all overlaps, or repeated online queries. This article covers practical approaches from simple sorting scans to interval trees.
Defining Overlap Clearly
For half-open intervals represented as (start, end), two intervals overlap when:
a.start < b.endb.start < a.end
Using half-open semantics avoids boundary ambiguity for adjacent ranges.
Approach 1: Naive Pairwise Check
For small lists, compare all pairs.
Time complexity is quadratic, which becomes expensive as input grows.
Approach 2: Sort by Start and Scan
If you need to detect any overlap efficiently in a static list, sort by start time and scan once.
Sorting plus linear scan gives O(n log n) time and is usually the best default for one-time checks.
Approach 3: Interval Tree for Repeated Queries
When intervals are queried repeatedly, use an interval tree. Each node stores an interval and the max end value of its subtree, enabling pruning.
This is useful for calendar systems and stream-like insert plus query workloads.
Returning All Overlaps
If you need all overlapping pairs, you can use sweep-line techniques with event sorting. For large datasets, this is often faster than checking every pair.
For moderate sizes, sorted scan with an active set structure is usually sufficient and easier to maintain than a fully optimized computational geometry implementation.
Choosing the Right Strategy
Use a simple decision rule:
- Small one-off list: naive approach.
- Medium or large static list: sort and scan.
- Many repeated dynamic queries: interval tree.
Choose based on workload shape, not only theoretical complexity.
Common Pitfalls
A common pitfall is inconsistent endpoint semantics. Mixing closed intervals and half-open intervals causes boundary bugs for adjacent ranges.
Another issue is not normalizing invalid intervals where start is greater than end. Validate or swap endpoints before processing.
Developers also forget stable sorting and tie behavior when starts are equal. Define deterministic ordering and overlap policy explicitly.
Finally, using advanced trees for tiny datasets can overcomplicate code without measurable benefit. Start simple and optimize only when profiling shows need.
Summary
- Define overlap semantics clearly before implementing.
- Use
O(n log n)sort and scan for most static overlap checks. - Use interval trees for repeated online overlap queries.
- Validate interval inputs and boundary rules early.
- Match algorithm complexity to actual data and query patterns.
Related reading
- Search in Rotated Sorted Array in Olog n time
- Search ranking/relevance algorithms
- Searching a tree using LINQ
- Searching for a fast/efficient histogram algorithm with pre-specified bins
- Searching for an element in a circular sorted array
- Searching in a sorted and rotated array
- Secret Santa - Generating 'valid' permutations
- Segmented Sieve of Eratosthenes?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.