Minimum Interval to Include Each Query - Problem

Imagine you're managing a library with time-sensitive book reservations. You have multiple reservation intervals, and patrons are making queries about when they can access books. Your job is to find the shortest reservation period that covers each patron's requested time.

You are given a 2D integer array intervals, where intervals[i] = [lefti, righti] represents the i-th time interval starting at lefti and ending at righti (inclusive). The size of an interval is defined as the number of time units it contains: righti - lefti + 1.

You are also given an integer array queries. For each query queries[j], you need to find the smallest interval that contains this query point. If no such interval exists, return -1.

Goal: Return an array containing the sizes of the smallest intervals for each query, or -1 if no interval contains the query.

Input & Output

example_1.py — Basic Case
$ Input: intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5]
Output: [3,3,1,4]
💡 Note: Query 2: contained in [1,4] (size 3) and [2,4] (size 3), minimum is 3. Query 3: contained in [1,4], [2,4], [3,6], minimum size is 3. Query 4: contained in [1,4], [2,4], [3,6], [4,4], minimum size is 1 from [4,4]. Query 5: only contained in [3,6] (size 4).
example_2.py — No Valid Intervals
$ Input: intervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22]
Output: [2,-1,4,6]
💡 Note: Query 2: contained in [2,3] (size 2), [2,5] (size 4), [1,8] (size 8), minimum is 2. Query 19: not contained in any interval, return -1. Query 5: contained in [2,5] (size 4) and [1,8] (size 8), minimum is 4. Query 22: only contained in [20,25] (size 6).
example_3.py — Single Point Intervals
$ Input: intervals = [[1,1],[2,2],[3,3]], queries = [1,2,3,4]
Output: [1,1,1,-1]
💡 Note: Each interval contains exactly one point and has size 1. Query 4 is not contained in any interval, so returns -1.

Constraints

  • 1 ≤ intervals.length ≤ 105
  • 1 ≤ queries.length ≤ 105
  • intervals[i].length == 2
  • 1 ≤ lefti ≤ righti ≤ 107
  • 1 ≤ queries[j] ≤ 107
  • Large input sizes require efficient O(n log n) solution

Visualization

Tap to expand
Minimum Interval to Include Each Query INPUT Intervals (time reservations): 1 2 3 4 5 6 [1,4] size=4 [2,4] size=3 [3,6] size=4 [4,4] s=1 Queries (patron requests): 2 3 4 5 intervals=[[1,4],[2,4], [3,6],[4,4]] queries=[2,3,4,5] ALGORITHM STEPS 1 Sort Intervals By left endpoint ascending 2 Sort Queries Keep original indices 3 Min-Heap by Size Track (size, right) pairs 4 Process Each Query Add valid, remove expired Min-Heap State (query=4): (1,4) (3,4) (4,4) Top = smallest interval containing query point FINAL RESULT Query Results: Query 2: [1,4],[2,4] contain 2 --> min=3 Query 3: [1,4],[2,4],[3,6] --> min=3 Query 4: All 4 contain 4 --> min=1 Query 5: Only [3,6] contains 5 --> 4 Output Array: 3 3 1 4 [3, 3, 1, 4] Key Insight: Sort both intervals and queries. Use a min-heap ordered by interval size to efficiently track the smallest valid interval. Add intervals when their start <= query, remove when their end < query. Time: O((n+q) log n) where n=intervals, q=queries. TutorialsPoint - Minimum Interval to Include Each Query | Optimal Solution (Sorting + Min-Heap)
Asked in
Google 42 Amazon 38 Meta 31 Microsoft 28 Apple 19
78.4K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen