Katkat

Analyze Overlap in Closed Intervals

NetflixSoftware Engineer · Technical Screen

Last updated

Implement an algorithm to determine how many closed intervals contain each point in a given list of query coordinates. The problem evaluates interval sweep-line techniques or binary search decomposition, boundary handling, and time/space complexity.

Problem Statement

Streaming services often schedule content availability, promotions, or feature flags across specific time windows modeled as closed intervals [start, end]. Given a list of such intervals and a list of discrete query timestamps, compute the number of intervals active at each query timestamp.

Write a function closed_interval_overlap(intervals, query_points) that takes:

  • intervals: A list of pairs [start, end], where each pair represents an inclusive interval covering all points x such that start <= x <= end.
  • query_points: A list of numbers representing timestamps to evaluate.

The function should return a list of integers of the same length as query_points, where the i-th element is the count of intervals containing query_points[i].

Function Signature

def closed_interval_overlap(intervals: list[list[int]], query_points: list[int]) -> list[int]:
    pass

Constraints

  • 0 <= len(intervals) <= 10^5
  • 0 <= len(query_points) <= 10^5
  • intervals[i] has length 2, and -10^9 <= intervals[i][0] <= intervals[i][1] <= 10^9
  • -10^9 <= query_points[j] <= 10^9

Examples

Example 1

intervals = [[1, 4], [2, 6], [7, 9], [3, 5]]
query_points = [2, 3, 5, 7, 10]
print(closed_interval_overlap(intervals, query_points))
# Output: [2, 3, 2, 1, 0]

Explanation:

  • At 2: contained in [1, 4] and [2, 6] (count = 2).
  • At 3: contained in [1, 4], [2, 6], and [3, 5] (count = 3).
  • At 5: contained in [2, 6] and [3, 5] (count = 2).
  • At 7: contained in [7, 9] (count = 1).
  • At 10: contained in no intervals (count = 0).

Example 2

intervals = [[5, 5], [1, 10]]
query_points = [5, 4, 11]
print(closed_interval_overlap(intervals, query_points))
# Output: [2, 1, 0]

Explanation:

  • At 5: contained in both [5, 5] and [1, 10].
  • At 4: contained only in [1, 10].
  • At 11: outside all intervals.

Edge Cases

  • intervals or query_points is empty (return empty list if queries empty, or list of zeros if intervals empty).
  • Degenerate intervals where start == end.
  • Duplicate query points or duplicate intervals.
  • Negative coordinate ranges.

Hints

Hint 1

A point `q` is contained in interval `[start, end]` if and only if `start = q`.

Hint 2

Notice that the count of intervals with `start = q` equals the total number of intervals with `start <= q` minus the number of intervals with `end < q`.

Hint 3

If you separate and sort all start points and all end points independently, you can answer each query in `O(log N)` time using binary search (e.g., `bisect_right` and `bisect_left`).

Follow-up questions

  • How would your approach change if intervals arrive dynamically as an online stream alongside the queries?
  • If query points are strictly sorted upfront, how can you solve this using a two-pointer or sweep-line approach without binary searching each query?
  • How would you modify the solution to return the maximum overlap across the entire timeline rather than counts at specific points?