easy +8 pts

Overlap two intervals

Compute the intersection of two closed intervals [a,b] and [c,d].

Write a function `overlap(interval1, interval2)` that takes two tuples representing closed intervals `(start, end)` and returns a tuple `(overlap_start, overlap_end)` representing the overlapping part of the intervals. If the intervals do not overlap (i.e., the intersection is empty), return `None`. Intervals are inclusive: an overlap of a single point (e.g., [1,3] and [3,5] overlap at 3) is considered valid and should return `(3, 3)`. Assume all inputs are integers and each interval is valid (start <= end).

Constraints

The input tuples contain integers. Length of each tuple is exactly 2. There is no limit on the values other than Python's integer range. The function should run in O(1) time and O(1) space.

Example

>>> overlap((1, 5), (3, 7))
(3, 5)
>>> overlap((1, 3), (4, 5)) is None
True
>>> overlap((1, 5), (5, 9))
(5, 5)
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

The overlap starts at the maximum of the two start values.
The overlap ends at the minimum of the two end values.
Check if the computed start is greater than the end to determine no overlap.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.