Check an Interval Against Existing Intervals for Conflicts
GoogleSoftware Engineer · Technical ScreenLast updated
Determine whether a proposed half-open interval overlaps with any interval in an unsorted list. This tests basic interval overlap logic and boundary condition handling.
Problem Statement
You are given an unsorted list of existing time reservations, intervals, where each reservation is represented as a half-open interval [start, end). You are also given a new candidate reservation [start, end).
Two half-open intervals [a, b) and [c, d) conflict if they share at least one point in time. That is, they conflict if and only if max(a, c) < min(b, d). If two intervals merely touch at their endpoints (for example, [1, 3) and [3, 5)), they do not conflict.
Write a function to determine if the candidate interval conflicts with any interval in the existing list.
Function Signature
def has_conflict(intervals: list[tuple[int, int]], candidate: tuple[int, int]) -> bool:
pass
Inputs
intervals: A list of tuples(start, end)representing existing booked intervals. Each tuple satisfiesstart < end.candidate: A tuple(start, end)representing the interval to check, withstart < end.
Output
- Return
Trueifcandidateconflicts with at least one interval inintervals; otherwise returnFalse.
Examples
Example 1
intervals = [(1, 4), (6, 10), (12, 15)]
candidate = (4, 6)
# Output: False
# Explanation: Candidate touches (1, 4) at 4 and (6, 10) at 6, but shares no interior points.
Example 2
intervals = [(10, 15), (1, 5), (20, 25)]
candidate = (4, 7)
# Output: True
# Explanation: Candidate overlaps with (1, 5) on the interval [4, 5).
Constraints
0 <= len(intervals) <= 10^5-10^9 <= start < end <= 10^9for all intervals includingcandidate
Edge Cases to Consider
- Empty
intervalslist: should immediately returnFalse. - Exact endpoint matches:
candidatestart equals an existing end, orcandidateend equals an existing start. - Candidate completely enclosed by or completely enclosing an existing interval.
Hints
Hint 1
Consider the mathematical condition under which two half-open intervals [a, b) and [c, d) overlap.
Hint 2
Two intervals [a, b) and [c, d) do not overlap if one finishes on or before the other starts: either b <= c or d <= a.
Hint 3
You can iterate through the list and check each interval individually in O(1) time without sorting.
Hint 4
Return True as soon as the first conflict is encountered to allow early exit.
Follow-up questions
- How would you optimize queries if the collection of intervals is static, but you need to check thousands of different candidate intervals?
- How would you design the data structure if intervals are continually inserted and removed while queries arrive in real time?
- If candidate conflicts, how would you return all conflicting intervals or the total overlapping duration instead of just a boolean?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.