Interval questions are a subset of array questions where you are given an array of two-element arrays (an interval) and the two values represent a start and an end value. Interval questions are considered part of the array family but they involve some common techniques hence they are extracted out to this special section of their own.
An example interval array:
[[1, 2], [4, 7]].
Interval questions can be tricky to those who have not tried them before because of the sheer number of cases to consider when they overlap.
Things to look out for during interviews
- Clarify with the interviewer whether
[2, 3]are considered overlapping intervals as it affects how you will write your equality checks.
- Clarify whether an interval of
[a, b]will strictly follow
ais smaller than
- No intervals
- Single interval
- Two intervals
- Non-overlapping intervals
- An interval totally consumed within another interval
- Duplicate intervals (exactly the same start and end)
- Intervals which start right where another interval ends -
[[1, 2], [2, 3]]
Sort the array of intervals by its starting point
A common routine for interval questions is to sort the array of intervals by each interval's starting value. This step is crucial to solving the Merge Intervals question.
Checking if two intervals overlap
Be familiar with writing code to check if two intervals overlap.
def is_overlap(a, b):
return a < b and b < a
Merging two intervals
def merge_overlapping_intervals(a, b):
return [min(a, b), max(a, b)]
These are essential questions to practice if you're studying for this topic.
Recommended practice questions
These are recommended questions to practice after you have studied for the topic and have practiced the essential questions.
AlgoMonster aims to help you ace the technical interview in the shortest time possible. By Google engineers, AlgoMonster uses a data-driven approach to teach you the most useful key question patterns and has contents to help you quickly revise basic data structures and algorithms. Best of all, AlgoMonster is not subscription-based - pay a one-time fee and get lifetime access. Join today for a 70% discount →