Find the maximum count of mutually nonoverlapping intervals

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

Salesforce logo
Salesforce
Dec 2, 2026
easySoftware EngineerTechnical ScreenCoding & Algorithms
0
0

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.