Remove the Fewest Overlapping Intervals
Company: TikTok
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Overview: Find the minimum number of intervals to remove so the remaining set has no overlaps, while allowing intervals to touch at endpoints. The problem covers duplicate intervals, negative coordinates, stable overlap semantics, and large-input complexity.
Read the full TikTok Software Engineer interview experience this question came from
Constraints
- 1 <= intervals.length <= 200,000
- Every interval has exactly two integer endpoints [start, end] with start < end.
- -10^9 <= start < end <= 10^9
- Duplicate intervals are allowed and occupy separate positions.
- Intervals whose end and start are equal do not overlap.
- Return one integer.
Examples
Input: ([[1, 2]],)
Expected Output: 0
Explanation: A single interval already forms a non-overlapping set.
Input: ([[1, 2], [2, 3], [3, 4], [1, 3]],)
Expected Output: 1
Explanation: The first source example removes the longer interval while retaining three endpoint-touching intervals.
Hints
- Minimizing removals is the same as maximizing how many intervals remain.
- Among intervals available to keep next, consider which ending time leaves the most room.