← All Patterns

Pattern Recognition Drill

#116

Hard Intervals

The Problem

Given intervals and queries (points), for each query find the size of the smallest interval containing it.

What approach would you use?

Think about it before scrolling down.

Minimum Interval to Include Each Query

Key Signals

Heap / Priority Queue

Sort queries and intervals. For each query, push valid intervals to min-heap. Pop expired. O((n+q) log n).

Alt: Binary Search

For each query, binary search for valid intervals. Less efficient without sorting.

Common Trap

Process queries offline (sorted) to avoid redundant work. Don't scan all intervals per query.

← #115 #117 →