Possible Interview Question How to Find All Overlapping 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
Interview questions about intervals often hide an important ambiguity: do you need to merge overlapping intervals, or list every overlap that exists. Those are different tasks, so a strong answer starts by clarifying the output format before writing code.
Clarify The Problem First
If the interviewer says "find all overlapping intervals," you should ask whether they want:
- Merged intervals such as combining
1-3and2-5into1-5. - Every overlapping pair.
- Every interval that participates in at least one overlap.
A lot of candidates jump into the merge-intervals solution because it is familiar, but that does not always answer the actual question.
Finding Overlapping Pairs
One reasonable interpretation is to return every pair of intervals that overlaps. A clean approach is to sort by start time and maintain an active list of intervals whose end is still beyond the current start.
This returns the pairs ((1, 4), (2, 5)) and ((6, 8), (7, 9)).
Why Sorting Helps
Without sorting, every interval might need to be compared with every other interval, which leads to O(n^2) work even before you think about reporting the results.
Sorting by start time lets you discard intervals that can no longer overlap with the current interval. Once an interval ends before the current start, it never needs to be checked again.
That turns the problem into a sweep from left to right across the number line.
Define Overlap Carefully
Another detail to settle early is whether touching edges count. For example, do 1-3 and 3-5 overlap?
Some problems treat that as overlap because the intervals share a boundary. Others require positive-width intersection and would say those intervals only touch.
Your comparison operator changes accordingly:
- Use
interval[1] > startwhen touching does not count. - Use
interval[1] >= startwhen touching does count.
State that assumption explicitly in an interview.
When The Interview Really Wants Merge Intervals
If the interviewer actually wants merged results, the classic sort-and-scan merge algorithm is simpler. That is why clarifying the output matters so much. Both problems begin with sorting, but the data you collect afterward is different.
Strong interview performance often comes from noticing this distinction, not just from remembering a canned algorithm.
If the interviewer pivots and asks for merged output instead of pair output, the same initial sort still helps. You then keep one current interval and extend its end while overlaps continue. Being able to explain both variants shows that you understand the interval pattern rather than only memorizing one answer.
Common Pitfalls
One common mistake is solving the merge-intervals problem when the question asked for all overlapping pairs. Those answers are related but not interchangeable.
Another mistake is forgetting to define whether touching edges count as overlap. That changes the result set and should not be left implicit.
A third issue is ignoring output size. If many intervals overlap, the number of reported pairs can itself be large, which affects practical runtime even with a good sweep approach.
Summary
- Clarify whether the goal is merged intervals, overlapping pairs, or overlapping participants.
- Sorting by start time is the key first step for efficient interval processing.
- A sweep with an active list can report all overlapping pairs cleanly.
- Always state whether touching endpoints count as overlap.
Related reading
- Possible permutations of BST's input
- potential On solution to Longest Increasing Subsequence
- Practical Uses of Fractals in Programming
- Pre-order to post-order traversal
- Predicting Values with k-Means Clustering Algorithm
- Predictive blood glucose algorithm?
- Preferred Sorting For People Based On Their Age
- Prefix search against half a billion strings

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.