Find the maximum count of mutually nonoverlapping intervals
Company: Salesforce
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Overview: Select the largest possible number of pairwise disjoint intervals from parallel boundary arrays. Use explicit closed-endpoint semantics and handle overlaps, duplicate intervals, negative coordinates, and point intervals.
Read the full Salesforce Software Engineer interview experience this question came from
Constraints
- `0 <= len(left) == len(right) <= 100000`.
- `-1000000000 <= left[i] <= right[i] <= 1000000000`.
- Endpoints fit in signed 32-bit integers. Zero-length intervals are permitted.
Examples
Input: ([3, 5, 8], [4, 6, 9])
Expected Output: 3
Explanation: All three closed intervals are separated.
Input: ([1, 2, 4], [3, 2, 5])
Expected Output: 2
Explanation: A zero-length interval can finish earlier than a containing interval.
Hints
- Choosing an interval that finishes early leaves as much room as possible for what follows.
- With closed intervals, the next start must be strictly greater than the previous end.