Answer Queries with the Shortest Covering Interval

Quick Overview

Answer Queries with the Shortest Covering Interval. A closed interval [left,right] has size right-left+1. For each entry of queries, return the smallest size among intervals containing it, or -1 when no interval does.

Answer Queries with the Shortest Covering Interval

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem A closed interval `[left,right]` has size `right-left+1`. For each entry of `queries`, return the smallest size among intervals containing it, or -1 when no interval does. Return answers in the original query order. ```cpp class Solution { public: vector<int> minInterval(vector<vector<int>>& intervals, vector<int>& queries); }; ``` ### Examples **Example 1:** ```text intervals = [[1,2],[2,5],[4,4]], queries = [4,1,6,2] Output: [1,2,-1,2] Explanation: The singleton covers 4; [1,2] is the shortest cover for 1 and 2. ``` **Example 2:** ```text intervals = [[3,7],[5,8]], queries = [5,5,8] Output: [4,4,4] Explanation: [5,8] covers both repeated queries and its endpoint 8. ``` ### Constraints - Interval and query counts are each in `[1,10^5]`. - Each interval has two endpoints satisfying `1 <= left <= right <= 10^7`. - `1 <= queries[j] <= 10^7`. ### Clarifying Questions Endpoints are inclusive. Repeated query values have distinct output positions. ```hint Inspect the structure Does a query equal to an interval endpoint count as covered, and where must repeated-query answers be placed? ``` ### Reasoning checkpoints - Enforce inclusive containment and the interval-size definition for every query. - Return the minimum size or absence result at each original query position, including repeated queries. ### Optional extensions How might an offline disjoint-set filling strategy process intervals in increasing size?

Overview: Answer Queries with the Shortest Covering Interval. A closed interval [left,right] has size right-left+1. For each entry of queries, return the smallest size among intervals containing it, or -1 when no interval does.

Google logo
Google
Aug 11, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

A closed interval [left,right] has size right-left+1. For each entry of queries, return the smallest size among intervals containing it, or -1 when no interval does. Return answers in the original query order.

class Solution {
public:
    vector<int> minInterval(vector<vector<int>>& intervals, vector<int>& queries);
};

Examples

Example 1:

intervals = [[1,2],[2,5],[4,4]], queries = [4,1,6,2]
Output: [1,2,-1,2]
Explanation: The singleton covers 4; [1,2] is the shortest cover for 1 and 2.

Example 2:

intervals = [[3,7],[5,8]], queries = [5,5,8]
Output: [4,4,4]
Explanation: [5,8] covers both repeated queries and its endpoint 8.

Constraints

  • Interval and query counts are each in [1,10^5] .
  • Each interval has two endpoints satisfying 1 <= left <= right <= 10^7 .
  • 1 <= queries[j] <= 10^7 .

Clarifying Questions Guidance

Endpoints are inclusive. Repeated query values have distinct output positions.

Reasoning checkpoints

  • Enforce inclusive containment and the interval-size definition for every query.
  • Return the minimum size or absence result at each original query position, including repeated queries.

Optional extensions

How might an offline disjoint-set filling strategy process intervals in increasing size?