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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

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
Min-Heap
Offline Queries
Sweep Line
Intervals
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: intervals [l, r] inclusive, list of query integers.
  • Output: list where entry i is the minimum r - l + 1 over intervals containing queries[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 ...


Submit Your Answer
Markdown supported