KatInterview

Find the Maximum Size Connected Interval Component

Greedy & IntervalsAuto-graded · 23 test cases
MediumAuto-gradedGreedy & Intervalsintervalssortinggreedy

Last updated

Find the maximum number of intervals that belong to a single connected component where edges represent inclusive interval overlaps.

Given a collection of inclusive integer intervals where each interval is represented as a list or tuple [start, end], determine the size of the largest connected group of intervals.

Two intervals [start_a, end_a] and [start_b, end_b] directly connect if they overlap, meaning: $$\max(start_a, start_b) \le \min(end_a, end_b)$$

A set of intervals forms a connected component if every interval in the component can reach any other interval in the component through a chain of overlapping intervals.

Return the maximum number of intervals contained within any single connected component. If the input list is empty, return 0.

Constraints

  • 0 <= len(intervals) <= 2 * 10^5
  • Each interval contains exactly two integers: [start, end] with start <= end.
  • start and end values can be negative, zero, or positive.

Examples

Example 1

Input: largest_overlapping_group([[1, 3], [2, 5], [6, 8]])

Output: 2

Example 2

Input: largest_overlapping_group([[1, 2], [2, 4], [4, 6]])

Output: 3

Example 3

Input: largest_overlapping_group([[1, 10], [2, 3], [4, 5], [11, 12]])

Output: 3

Hints

Hint 1

Building an explicit adjacency list and running BFS/DFS will be too slow ($O(n^2)$ edges in the worst case).

Hint 2

Sort the intervals by their start time. What does this reveal about how connected components map to contiguous segments of the sorted array?

Hint 3

Maintain a running maximum end coordinate for the current group. If a new interval starts after this maximum end coordinate, a component boundary has been reached.