Skip to main content

Minimum Interval to Include Each Query

Time Range Finder You're building a calendar app that finds the shortest time slot that includes each meeting. Given intervals [start, end] and query times, find the smallest interval containing each query. Return -1 if no interval contains the query. Example: intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5] → [3,3,1,4]

Binary search is the tool here, but the real test is whether your invariant stays crisp. It's a hard one for a reason: it stresses boundary precision when the rules pile up. Interviewers care more about airtight boundaries than speed here.

Examples

You write

min_interval(intervals, queries)

  1. Example 1

    Input
    intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5]
    returns
    [3,3,1,4]
  2. Example 2

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

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Heaps

I reach for it when

  • You need the k largest or k smallest items.
  • You repeatedly take the best remaining item.

Say this before you type. Say what the heap holds, how big it stays, and what sits on top.

All 22 Heaps problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Minimum Interval Sizehard