Shortest Maintenance Window Covering Each Query Time
Last updated: November 29, 2026
Quick Overview
Given maintenance windows as inclusive intervals [l, r] and a list of query times, return for each query the length (r - l + 1) of the shortest window containing it, or -1 if none does.
0
0
0 solved
Given maintenance windows as inclusive intervals [l, r] and a list of query times, return for each query the length (r - l + 1) of the shortest window containing it, or -1 if none does.
A Google onsite offline-query problem in an SRE maintenance-calendar frame. The interviewer wants queries answered offline in sorted order, with intervals added as they start and a min-heap by length with lazy removal of windows that have ended.
What the Interviewer Expects
- Sorts queries but returns answers in original order
- Sorts intervals by left endpoint
- Pushes intervals with l <= q into a heap keyed by length
- Pops intervals whose r < q
- States O((n + q) log n)
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if queries arrive online?
- Return the window itself, not its length
- What if windows can be added between queries?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: intervals
[l, r]inclusive, list of query integers. - Output: list where entry i is the minimum
r - l + 1over intervals containingqueries[i], else -1....
High-Level Design
Answer queries offline in increasing order. As the query value grows, more intervals become eligible on the left (l <= q) and some expire on the right (r < q).
Sort intervals by l. For each query in ...