hardIntervalsGoogle
Shortest Covering Interval
For each query timestamp, pick the SHORTEST interval that covers it; emit -1 if none covers.
Given intervals (as [start, end] inclusive) and queries, for each query q return the size of the smallest interval that contains q, or -1.
Choose approach
1/5
Narrationstep 1 of 5
Query 2: smallest covering interval size = 3.
Variables
q=2
size=3
How to think about it
- Offline sorting lets us process queries in monotonic order.
- Heap tracks live candidates by size.
Edge cases to catch in interview
- No interval covers q -> -1.
- Multiple intervals equal size -> any one chosen.
Examples
Example 1
[2,-1,4,6]{"intervals":[[2,3],[2,5],[1,8],[20,25]],"queries":[2,19,5,22]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed