Smallest set of integers containing at least two points from every interval

Quick Overview

Given a list of integer intervals, find the smallest set of integers that contains at least two members of every interval. It tests greedy reasoning on intervals, careful handling of ties, and arguing that a greedy choice is optimal.

Smallest set of integers containing at least two points from every interval

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

You are given a list `intervals` where `intervals[i] = [start_i, end_i]` stands for every integer from `start_i` to `end_i`, inclusive. A containing set is a set of integers such that every interval contains at least two of its members. Return the minimum possible size of a containing set. ### Function Signature ```python def intersection_size_two(intervals: list[list[int]]) -> int: ``` ### Rules - Each interval is a pair `[start, end]` with `start < end`, so every interval holds at least two integers and a containing set always exists. - The set holds distinct integers. One integer counts toward every interval that contains it. - The input is in no particular order, and intervals may overlap or nest. ### Constraints - `1 <= len(intervals) <= 3000` - `len(intervals[i]) == 2` - `0 <= start_i < end_i <= 10^8` ### Examples ```text Input: intervals = [[1,3],[1,4],[2,5],[3,5]] Output: 3 ``` The set {2, 3, 4} works: [1,3] holds 2 and 3, [1,4] and [2,5] hold all three, and [3,5] holds 3 and 4. Two integers cannot be enough, because [1,3] and [3,5] share only the integer 3. ```text Input: intervals = [[1,2],[2,3],[2,4],[4,5]] Output: 5 ``` [1,2] forces both 1 and 2 into the set, and [4,5] forces both 4 and 5. [2,3] then still needs 3, so the answer is {1, 2, 3, 4, 5}, which also covers [2,4]. ```text Input: intervals = [[1,2],[2,5],[4,5],[5,6]] Output: 5 ``` [1,2] forces 1 and 2, [4,5] forces 4 and 5, and [5,6] then needs 6. The set {1, 2, 4, 5, 6} also holds 2, 4 and 5 from [2,5].

Overview: Given a list of integer intervals, find the smallest set of integers that contains at least two members of every interval. It tests greedy reasoning on intervals, careful handling of ties, and arguing that a greedy choice is optimal.

Uber logo
Uber
Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

You are given a list intervals where intervals[i] = [start_i, end_i] stands for every integer from start_i to end_i, inclusive.

A containing set is a set of integers such that every interval contains at least two of its members.

Return the minimum possible size of a containing set.

Function Signature

def intersection_size_two(intervals: list[list[int]]) -> int:

Rules

  • Each interval is a pair [start, end] with start < end , so every interval holds at least two integers and a containing set always exists.
  • The set holds distinct integers. One integer counts toward every interval that contains it.
  • The input is in no particular order, and intervals may overlap or nest.

Constraints

  • 1 <= len(intervals) <= 3000
  • len(intervals[i]) == 2
  • 0 <= start_i < end_i <= 10^8

Examples

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

The set {2, 3, 4} works: [1,3] holds 2 and 3, [1,4] and [2,5] hold all three, and [3,5] holds 3 and 4. Two integers cannot be enough, because [1,3] and [3,5] share only the integer 3.

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

[1,2] forces both 1 and 2 into the set, and [4,5] forces both 4 and 5. [2,3] then still needs 3, so the answer is {1, 2, 3, 4, 5}, which also covers [2,4].

Input: intervals = [[1,2],[2,5],[4,5],[5,6]]
Output: 5

[1,2] forces 1 and 2, [4,5] forces 4 and 5, and [5,6] then needs 6. The set {1, 2, 4, 5, 6} also holds 2, 4 and 5 from [2,5].