Find the Maximum Overlap of Closed Intervals

Quick Overview

Find maximum simultaneous coverage of closed intervals with a sweep line. Preserve overlaps at touching endpoints, zero-length intervals, and duplicate intervals through correct event ordering.

Find the Maximum Overlap of Closed Intervals

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given a collection of closed intervals [start,end], return the largest number of intervals containing the same point. Both endpoints count: [1,2] and [2,3] overlap at 2. ### Constraints and Clarifications Assume start <= end for every interval. No numeric or collection-size bounds are reported. As interface conventions, an empty collection returns zero, repeated intervals count separately, and a zero-length interval [t,t] contains t. Endpoints support an exact total ordering; do not add an arbitrary epsilon. ### Example For [[1,2],[2,3],[2,2]], return 3 because all three contain 2. ### What a Strong Answer Covers Use a sweep line with an endpoint rule that counts starts before removing ends at the same coordinate. Explain why the usual half-open scheduling convention would be incorrect here. ### Follow-up Questions - How would event ordering change for half-open intervals [start,end)? - Why is replacing an end with end+1 unsafe without an integer-domain contract?

Overview: Find maximum simultaneous coverage of closed intervals with a sweep line. Preserve overlaps at touching endpoints, zero-length intervals, and duplicate intervals through correct event ordering.

Amazon logo
Amazon
Oct 24, 2026
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
2
0

Given a collection of closed intervals [start,end], return the largest number of intervals containing the same point. Both endpoints count: [1,2] and [2,3] overlap at 2.

Constraints and Clarifications

Assume start <= end for every interval. No numeric or collection-size bounds are reported. As interface conventions, an empty collection returns zero, repeated intervals count separately, and a zero-length interval [t,t] contains t. Endpoints support an exact total ordering; do not add an arbitrary epsilon.

Example

For [[1,2],[2,3],[2,2]], return 3 because all three contain 2.

What a Strong Answer Covers Guidance

Use a sweep line with an endpoint rule that counts starts before removing ends at the same coordinate. Explain why the usual half-open scheduling convention would be incorrect here.

Follow-up Questions Guidance

  • How would event ordering change for half-open intervals [start,end)?
  • Why is replacing an end with end+1 unsafe without an integer-domain contract?