Interval questions are 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.
Do clarify with the interviewer whether
[1, 2] and
[2, 3] are considered overlapping intervals as it affects how you will write your equality checks.
A common routine for interval questions is to sort the array of intervals by each interval's starting value.
Be familiar with writing code to check if two intervals overlap and merging two overlapping intervals:
def is_overlap(a, b): return a < b and b < a def merge_overlapping_intervals(a, b): return [min(a, b), max(a, b)]
- Single interval
- Non-overlapping intervals
- An interval totally consumed within another interval
- Duplicate intervals