How to Check for Overlapping Intervals
Posted3 months agoActive2 months ago
zayenz.seTechstory
calmpositive
Debate
40/100
Interval AnalysisData StructuresAlgorithm Design
Key topics
Interval Analysis
Data Structures
Algorithm Design
The post discusses how to check for overlapping intervals, sparking a thoughtful discussion on related topics such as interval analysis, data structures, and algorithm design.
Snapshot generated from the HN discussion
Discussion Activity
Very active discussionFirst comment
1h
Peak period
25
Day 1
Avg / period
9.3
Comment distribution28 data points
Loading chart...
Based on 28 loaded comments
Key moments
- 01Story posted
Oct 11, 2025 at 11:26 AM EDT
3 months ago
Step 01 - 02First comment
Oct 11, 2025 at 12:52 PM EDT
1h after posting
Step 02 - 03Peak activity
25 comments in Day 1
Hottest window of the conversation
Step 03 - 04Latest activity
Oct 23, 2025 at 3:07 PM EDT
2 months ago
Step 04
Generating AI Summary...
Analyzing up to 500 comments to identify key contributors and discussion patterns
ID: 45549888Type: storyLast synced: 11/20/2025, 6:45:47 PM
Want the full context?
Jump to the original sources
Read the primary article or dive into the live Hacker News thread when you're ready.
While the overlap algorithm (or rather "condition") is cute, there a lot more "cool" stuff to do with intervals, which I would have liked to see in there.
- Checking whether multiple intervals overlap
- Checking whether multiple intervals are contiguous
- Merging contiguous intervals
- Etc..
From experience, something is also crucial when working with intervals: trivially knowing which boundaries are closed and which are opened. I found that defining a strict vocabulary helps a lot here. e.g. "last" is "inclusive", while "end" is exclusive.
[closed; opened[ intervals are also the best when dealing with time intervals (if that makes sense in your use case), because you can trivially join them.
However you aren't just given all the existing edges (pair overlaps) in advance, maybe there's a way to have the graph-exploration side guide the edge-detection to minimize work.
See also: <https://martinfowler.com/eaaDev/Range.html>
Hey, _I'm_ a source that's older than that one: <https://stackoverflow.com/a/13513973>
Not so sure about "more authoritative", though.
The earliest version I could find on IA is from 2003 (https://web.archive.org/web/20030606033520/http://c2.com/cgi...), last edited in 2002 at that point, but wouldn't surprise me to page was initially created in the 90s.
The 3D case comes up in collision detection.
For collision detection in games, the objects are usually kept in a sorted order, with separate lists for X, Y and Z. Amusingly, a bubble sort is useful, because, as objects move, they tend to move locally, so a bubble sort quickly restores the order. The sorting algorithm should terminate quickly when there are few or no changes. First seen in I-Collide, 1995. When objects are moving slowly, speed is slightly worse than O(N), but degrades if there's too much motion.
2D sorting speeds things up. If you sort the intervals by start X, start Y, you can process the intervals sequentially. Here's something of mine which does that.[1] A MySQL database does the sort, then feeds the data to this algorithm. Overlaps are detected, sets of overlapping objects are merged, and the sets of overlapping 2D rectangles are emitted. Sort is O(N log N) as usual, and overlap detection is O(N).
[1] https://github.com/John-Nagle/maptools/blob/main/rust/src/ge...
Presumably just for boxes aligned with the axes (or some other condition?)? EG two lines can have x’s in common and y’s but not overlap if they are sloped at some angle.
Most collision detection systems use axis-aligned bounding boxes as a filter. Then more detailed algorithms are used on possibly-colliding objects.
There are cases where even though the sort executes more instructions that the size of the elements/code still fits into some Ln cache level and makes it faster, but in general the prefix approach comes out ahead.
1. Please always make closed and open interval explicit on all code examples. "Detecting overlap" is ambiguous and open intervals have no given solution in the article, if I'm not mistaken. 2. How do you define the empty interval on floating point numbers? How do you define an open interval on floating point numbers? Number representation, input range etc can be very important.
Disclosure: Did some stupidly crazy time series eval for OCPP1.6 and OCPP2.01 charging profiles.
Wikipedia: https://en.wikipedia.org/wiki/R-tree
Suppose you start with two separated intervals. The left one starts sliding rightward. At what point do they contact? That's easy, it's just when (end1 > start2).
As it continues sliding, at what point do they lose contact? Again, easy: it's where (start1 >= end2).
So the solution is the first condition and the negation of the second, i.e.: (end1 > start2) && (start1 < end2)
More so when you have to distinguish between the different types of overlap and non overlap and carry through the reasoning over a chain of overlap/no-overlap relations. I sure underestimated it.
The one dimensional case is covered(there you go again) by Allen algebra. The more richer notion is that of topological relations. I will find the Wikipedia pages and post.
https://en.wikipedia.org/wiki/Allen%27s_interval_algebra
https://en.wikipedia.org/wiki/Region_connection_calculus
https://en.wikipedia.org/wiki/Spatial_relation
https://en.wikipedia.org/w/index.php?title=DE-9IM
Interval trees, range trees help if you have a large static set of interval like objects against which you have to relate a query object.
Here's a package for Python that presumably uses some sort index data structure to be efficient: https://pyranges.readthedocs.io/en/latest/