Maximum Team Size with Overlapping Intervals - Solution & Explanation
Problem Statement
You are given two integer arrays startTime and endTime of length n.
startTime[i]represents the start time of theithemployee.endTime[i]represents the end time of theithemployee.
Two employees i and j can interact if their time intervals overlap. Two intervals are considered overlapping if they share at least one common time point.
A team is valid if there exists at least one employee in the team who can interact with every other member of the team.
Return an integer denoting the maximum possible size of such a team.
Example 1:
Input: startTime = [1,2,3], endTime = [4,5,6]
Output: 3
Explanation:
- For
i = 0with interval[1, 4]. - It overlaps with
i = 1having interval[2, 5]andi = 2having interval[3, 6]. - Thus, index 0 can interact with all other indices, so the team size is 3.
Example 2:
Input: startTime = [2,5,8], endTime = [3,7,9]
Output: 1
Explanation:
- For
i = 0, interval[2, 3]does not overlap with[5, 7]or[8, 9]. - For
i = 1, interval[5, 7]does not overlap with[2, 3]or[8, 9]. - For
i = 2, interval[8, 9]does not overlap with[2, 3]or[5, 7]. - Thus, no index can interact with others, so the maximum team size is 1.
Example 3:
Input: startTime = [3,4,6], endTime = [8,5,7]
Output: 3
Explanation:
- For
i = 0with interval[3, 8]. - It overlaps with
i = 1having interval[4, 5]andi = 2having interval[6, 7]. - Thus, index 0 can interact with all other indices, so the team size is 3.
Constraints:
1 <= n == startTime.length == endTime.length <= 1050 <= startTime[i] <= endTime[i] <= 109
Approach Overview
Problem Overview: You are given multiple time intervals representing when each member is available. The task is to determine the largest number of intervals that overlap at any moment. That value represents the maximum possible team size that can work together simultaneously.
Approach 1: Brute Force Interval Comparison (O(n²) time, O(1) space)
The straightforward method checks every interval against all others and counts how many overlap with it. For each interval i, iterate through the remaining intervals and test overlap conditions such as start_j ≤ end_i and end_j ≥ start_i. Track the maximum overlap count encountered. This approach is easy to reason about but becomes slow when the number of intervals grows, since every pair is compared.
Approach 2: Sweep Line with Sorted Events (O(n log n) time, O(n) space)
Transform each interval into two events: a start event (time, +1) and an end event (time, -1). Sort all events by time. Then iterate through the events while maintaining a running counter of active intervals. Each start increases the counter and each end decreases it. The maximum value reached during the scan is the largest overlapping group. This pattern is known as the sweep line technique and is commonly used in interval problems.
Approach 3: Two Sorted Arrays (Starts and Ends) (O(n log n) time, O(n) space)
Extract all start times into one array and all end times into another. Sort both arrays. Use two pointers: one iterating through starts and one through ends. When the next start time is less than or equal to the current end time, a new interval begins before the previous one ends, so increment the active team count and move the start pointer. Otherwise move the end pointer to close an interval. Track the maximum active count during the scan. This approach is conceptually similar to the sweep line but avoids building explicit event objects and relies only on sorting.
Recommended for interviews: The sweep line or two‑pointer sorted approach is what interviewers usually expect. Brute force shows you understand how overlaps work, but the optimal solution demonstrates knowledge of event ordering and efficient interval processing. Both optimal approaches run in O(n log n) due to sorting and handle large datasets comfortably.
Solution
We first combine each employee's start and end times into an interval array, intervals, and sort all start times and end times separately.
For each employee i, we use binary search to compute how many employees have end times not earlier than employee i's start time, and how many employees have start times not later than employee i's end time. The difference between these two counts is the number of employees whose intervals overlap with employee i. We iterate through all employees, compute the overlap count for each one, and take the maximum as the answer.
The time complexity is O(n times log n), and the space complexity is O(n), where n is the number of employees.
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Interval Comparison | O(n²) | O(1) | Small input sizes or when explaining the overlap concept first |
| Sweep Line with Event Sorting | O(n log n) | O(n) | General case; standard solution for maximum overlapping intervals |
| Two Pointers on Sorted Start/End Arrays | O(n log n) | O(n) | Cleaner implementation when only counts of overlaps are required |
Frequently Asked Questions
Is Maximum Team Size with Overlapping Intervals easy or hard?
Maximum Team Size with Overlapping Intervals Python/Java solution
How to solve Maximum Team Size with Overlapping Intervals in O(n)?
What is the best approach for Maximum Team Size with Overlapping Intervals?
Is Maximum Team Size with Overlapping Intervals asked at Google/Amazon/Meta?
What data structure is used in Maximum Team Size with Overlapping Intervals?
What is the time complexity of Maximum Team Size with Overlapping Intervals?
Ready to solve this problem?
Practice Maximum Team Size with Overlapping Intervals with our built-in code editor and test cases.
Practice on FleetCode