Peak Overlap Point Across Closed Intervals
Greedy & IntervalsAuto-graded · 22 test casesLast updated
Tests whether a candidate can use sweep-line event counting to locate the smallest integer covered by the greatest number of closed intervals.
Peak Overlap Point Across Closed Intervals
You are given a list of N closed integer intervals. Each interval [lo, hi] covers every integer from lo to hi, inclusive. Find the integer that is covered by the greatest number of intervals at once.
Define the coverage of an integer x as the count of intervals [lo, hi] for which lo ≤ x ≤ hi. Return the integer with the highest coverage. When multiple integers share the highest coverage, return the smallest one.
Input
intervals: a list ofNpairs[lo, hi]withlo ≤ hi. All endpoint values are positive integers.
Output
A single integer x whose coverage is maximum. Break ties by choosing the smallest such x.
How coverage is defined
xis covered by[lo, hi]if and only iflo ≤ x ≤ hi(both endpoints are included).- Two intervals that share exactly one endpoint — for example
[1, 3]and[3, 5]— both cover3, so3has coverage at least 2. - A point interval
[x, x]contributes 1 to the coverage of exactly the integerx.
Tie-breaking rule
When several integers all achieve the maximum coverage, return the smallest one. This rule is strict: there is exactly one correct answer for every input.
Constraints
1 ≤ N ≤ 10^51 ≤ lo ≤ hi ≤ 10^9for every interval- Intervals may overlap, be nested, be identical, touch at endpoints, or be entirely disjoint
Examples
Example 1
Input: peak_overlap_point([[1, 3], [2, 5], [4, 6]])
Output: 2
Example 2
Input: peak_overlap_point([[1, 2], [4, 5], [7, 8]])
Output: 1
Example 3
Input: peak_overlap_point([[3, 10], [1, 4], [6, 8], [2, 7]])
Output: 3
Hints
Hint 1
Coverage only changes at interval boundaries. You do not need to examine every integer between the global min and max — think about what happens at the exact coordinates where intervals start and stop.
Hint 2
Model each closed interval [lo, hi] as two events: +1 at lo and -1 at hi+1. The -1 lands at hi+1 (not hi) because hi itself is still inside the interval. Summing these events at each coordinate and sweeping in sorted order gives you the coverage at every event point.
Hint 3
Walk the sorted event coordinates while keeping a running total. After applying all deltas at coordinate v, that total equals the coverage of v. Record the first coordinate where the running total reaches its all-time high — that is your answer.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Why brute force is impractical
The direct approach scans every integer from the global minimum to the global maximum and counts how many intervals contain it. With endpoints up to 10^9 that range can be enormous, making the cost O(N · range) completely infeasible.
Sweep line over boundary events
Coverage is piecewise constant: it only changes at the exact integer where some interval opens or closes. Between two consecutive boundary coordinates, the count is flat. So rather than examining billions of integers, it is enough to inspect only the coordinates where something actually changes.
Convert each closed interval [lo, hi] into two events:
+1atlo— coverage increases as the interval begins.−1athi + 1— because the interval is closed,hiis still covered; coverage drops only at the next integer afterhi.
Store these in a dictionary, summing deltas that land on the same coordinate. Sort the coordinates and walk them in ascending order, maintaining a running total active. After absorbing all deltas at coordinate v, active equals the number of intervals that contain v. We record the first coordinate where active reaches a new all-time high.
Why it is correct
After processing coordinate v, active equals:
- the count of intervals with
lo ≤ v(each contributed+1at or beforev), minus - the count of intervals with
hi + 1 ≤ v, i.e.hi < v(each contributed−1at or beforev).
What remains is precisely the intervals satisfying lo ≤ v ≤ hi — exactly those that cover v. Between consecutive event coordinates v and v′, no delta fires, so coverage is the same for every integer in that gap; checking only event coordinates loses nothing.
All deltas at the same coordinate are merged before active is read, so adjacent intervals like [1, 3] and [4, 6] (one ends, another starts at the very next integer) never produce a false overlap.
Using strict > for the update means the first (smallest) coordinate with maximum coverage is retained, which is the required tie-breaking behaviour.
Edge cases
- Single interval: returns
lo. - Point interval
[x, x]:+1atxand−1atx+1, soxgets coverage 1 correctly. - Touching intervals like
[1, 3]and[3, 5]: both cover3, producing coverage 2 there. - Disjoint adjacent intervals like
[1, 2]and[3, 4]: the−1for the first and+1for the second both land at3, cancel, and no false overlap appears. - Nested or duplicate intervals: deltas accumulate naturally with no special handling.
Complexity
- Time:
O(N log N)— building the event map isO(N); sorting up to2Ncoordinates isO(N log N); the sweep isO(N). - Space:
O(N)for the event dictionary.
Follow-up: small coordinate bound M
When every endpoint is smaller than some modest value M, replace the hash map and sort with a difference array of size M + 1. Apply diff[lo] += 1 and diff[hi + 1] -= 1 for each interval, then compute a running prefix sum from index 0 to M − 1. The first index where the prefix sum peaks is the answer.
- Time:
O(N + M)— no logarithmic factor. - Space:
O(M).
This beats the sort-based approach whenever M = O(N \log N) or smaller.
Full solution
from collections import defaultdict
from typing import List
def peak_overlap_point(intervals: List[List[int]]) -> int:
# Accumulate net coverage changes at each coordinate.
events = defaultdict(int)
for lo, hi in intervals:
events[lo] += 1 # interval starts covering at lo
events[hi + 1] -= 1 # closed interval: stops covering after hi
active = 0
best_count = -1
best_value = None
for coord in sorted(events):
active += events[coord] # coverage at this coordinate
if active > best_count: # strict >: keep the first (smallest) maximum
best_count = active
best_value = coord
return best_value