intervals
overlapping intervals
interview questions
algorithm
coding interview

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.

Practice algorithms

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-3 and 2-5 into 1-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.

python
1from typing import List, Tuple
2
3Interval = Tuple[int, int]
4
5
6def find_overlapping_pairs(intervals: List[Interval]) -> List[Tuple[Interval, Interval]]:
7    intervals = sorted(intervals, key=lambda item: item[0])
8    active: List[Interval] = []
9    overlaps: List[Tuple[Interval, Interval]] = []
10
11    for current in intervals:
12        start, end = current
13        active = [interval for interval in active if interval[1] > start]
14
15        for interval in active:
16            overlaps.append((interval, current))
17
18        active.append(current)
19
20    return overlaps
21
22
23sample = [(1, 4), (2, 5), (6, 8), (7, 9)]
24print(find_overlapping_pairs(sample))

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] > start when touching does not count.
  • Use interval[1] >= start when 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.