Quick 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.

Find the maximum count of mutually nonoverlapping intervals

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

Arrays `left` and `right` describe intervals `[left[i], right[i]]`. Return the largest number of intervals you can select such that no two selected intervals overlap. ### Function Signature ```python def maximum_disjoint_intervals(left: list[int], right: list[int]) -> int: ``` ### Rules - For this practice version, intervals are closed: sharing an endpoint counts as overlap. - Each input position describes one interval. Duplicate intervals are separate choices but cannot both be selected. - Return the maximum count only. Do not return the selected intervals. ### Constraints These bounds and endpoint semantics define this practice version. - `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 ```text Input: left = [3, 5, 8], right = [4, 6, 9] Output: 3 ``` The intervals `[3,4]`, `[5,6]`, and `[8,9]` are all disjoint. ```text Input: left = [1, 2, 4], right = [3, 2, 5] Output: 2 ``` Choose `[2,2]` and `[4,5]`. ```text Input: left = [1, 2], right = [2, 3] Output: 1 ``` These closed intervals share endpoint 2.

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

Arrays `left` and `right` describe intervals `[left[i], right[i]]`. Return the largest number of intervals you can select such that no two selected intervals overlap. ### Function Signature ```python def maximum_disjoint_intervals(left: list[int], right: list[int]) -> int: ``` ### Rules - For this practice version, intervals are closed: sharing an endpoint counts as overlap. - Each input position describes one interval. Duplicate intervals are separate choices but cannot both be selected. - Return the maximum count only. Do not return the selected intervals. ### Constraints These bounds and endpoint semantics define this practice version. - `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 ```text Input: left = [3, 5, 8], right = [4, 6, 9] Output: 3 ``` The intervals `[3,4]`, `[5,6]`, and `[8,9]` are all disjoint. ```text Input: left = [1, 2, 4], right = [3, 2, 5] Output: 2 ``` Choose `[2,2]` and `[4,5]`. ```text Input: left = [1, 2], right = [2, 3] Output: 1 ``` These closed intervals share endpoint 2.

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

  1. Choosing an interval that finishes early leaves as much room as possible for what follows.
  2. With closed intervals, the next start must be strictly greater than the previous end.

Find the maximum count of mutually nonoverlapping intervals

easySalesforceSoftware Engineer

Arrays left and right describe intervals [left[i], right[i]]. Return the largest number of intervals you can select such that no two selected intervals overlap.

Function Signature

def maximum_disjoint_intervals(left: list[int], right: list[int]) -> int:

Rules

  • For this practice version, intervals are closed: sharing an endpoint counts as overlap.
  • Each input position describes one interval. Duplicate intervals are separate choices but cannot both be selected.
  • Return the maximum count only. Do not return the selected intervals.

Constraints

These bounds and endpoint semantics define this practice version.

  • 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: left = [3, 5, 8], right = [4, 6, 9]
Output: 3

The intervals [3,4], [5,6], and [8,9] are all disjoint.

Input: left = [1, 2, 4], right = [3, 2, 5]
Output: 2

Choose [2,2] and [4,5].

Input: left = [1, 2], right = [2, 3]
Output: 1

These closed intervals share endpoint 2.

Examples

Example 1
Input
([3, 5, 8], [4, 6, 9])
Output
3
Notes
All three closed intervals are separated.
Example 2
Input
([1, 2, 4], [3, 2, 5])
Output
2
Notes
A zero-length interval can finish earlier than a containing interval.

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def maximum_disjoint_intervals(left: list[int], right: list[int]) -> int:
Sample call
maximum_disjoint_intervals([3, 5, 8], [4, 6, 9])
Language
Case 1
Input
([3, 5, 8], [4, 6, 9])
Expected
3
Case 2
Input
([1, 2, 4], [3, 2, 5])
Expected
2

Browse More Questions

Show the approach

Approach

Sort the intervals by increasing right endpoint. Take the first available interval whose left endpoint is strictly greater than the last selected right endpoint. For the initial choice and after each previous selection, consider an optimal remaining schedule. Its first interval ends no earlier than the greedy choice, so exchanging its first interval for the greedy one preserves every later interval. Repeat this argument after each choice to prove the maximum count. Closed intervals require the strict inequality, including for zero-length intervals.

Time complexity:
O(n log n)
Space complexity:
O(n)