Find Overlapping Intervals at a Given Point in Time

viaGlassdoor

Problem: Given a list of time intervals, answer multiple queries where each query asks: how many (or which) intervals are active/overlapping at a given point in time?

Constraints: Large number of queries and intervals; a naive per-query linear scan across all intervals would be too slow.

Example: Intervals [(1,5),(2,6),(8,10)], query at time=3 -> intervals (1,5) and (2,6) overlap (count=2).

Approach: Preprocess using a sweep-line technique: create +1 events at each interval start and -1 events at each interval end, sort all events by time, and compute a prefix sum of active-interval counts over time. For a point query at time t, binary search the sorted event timeline to find the count of active intervals at that time in O(log n). This converts each query from O(n) to O(log n) after O(n log n) preprocessing.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account