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.

Visualization

Tap to expand
๐Ÿ“š Library Reservation SystemFind the shortest reservation period for each patron requestAvailable Books (Min-Heap by Duration)[4,4]1 day[1,4]3 days[2,4]3 days[3,6]4 daysPatron Requests (Sorted)2345โ†’ 3โ†’ 3โ†’ 1โ†’ 4๐ŸŽฏ Algorithm Steps:1. Sort queries [2,3,4,5] and intervals by start time2. For each query, add started intervals to min-heap3. Remove expired intervals, return size of heap top
Understanding the Visualization
1
Sort Everything
Sort queries by time and intervals by start time for efficient processing
2
Process in Order
For each query time, add all intervals that have started to our 'available books' heap
3
Clean Expired
Remove any intervals that have ended before the current query time
4
Find Best Option
The top of the min-heap gives us the shortest available reservation period
Key Takeaway
๐ŸŽฏ Key Insight: By processing queries in sorted order and maintaining active intervals in a min-heap, we avoid redundant work and achieve optimal O((n+m) log n) complexity.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n ร— m)

For each of the m queries, we check all n intervals

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a few variables to track minimum size

n
2n
โœ“ Linear Space

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
Asked in
Google 42 Amazon 38 Meta 31 Microsoft 28 Apple 19
78.3K 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