Back to Practice Dashboard
Top 150 InterviewEasy

Minimum Interval to Include Each Query

Learn how to solve the 'Minimum Interval to Include Each Query' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

You are given a 2D integer array intervals, where intervals[i] = [left_i, right_i] describes the ith interval starting at left_i and ending at right_i (inclusive). The size of an interval is defined as right_i - left_i + 1. You are also given an integer array queries. The answer to the jth query is the size of the smallest interval i such that left_i <= queries[j] <= right_i. If no such interval exists, the answer is -1.

Return an array containing the answers to the queries.

Write a function minInterval(intervals: List[List[int]], queries: List[int]) -> List[int].

Constraints
  • 1 <= len(intervals) <= 10^5
  • 1 <= len(queries) <= 10^5
  • intervals[i].length == 2
  • 1 <= left_i <= right_i <= 10^7
  • 1 <= queries[j] <= 10^7

Examples

Example 1
Input
intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5]
Output
[3,3,1,4]
Explanation

Smallest interval containing 2 is [2,4] (size 3). For 3 is [2,4] (size 3). For 4 is [4,4] (size 1). For 5 is [3,6] (size 4).

Example 2
Input
intervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22]
Output
[2,-1,4,6]
Explanation

For 2: [2,3] (size 2). For 19: none (-1). For 5: [2,5] (size 4). For 22: [20,25] (size 6).

Need a Hint?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor