3893. Maximum Team Size with Overlapping Intervals
Asked at Salesforce
Difficulty
Medium
Time
O(n log n)
Space
O(n)
Pattern
Array
Companies
1
Problem
You are given two arrays, startTime and endTime, both of length n. Employee i is available during the closed interval [startTime[i], endTime[i]].
Two employees can interact if their intervals share at least one common time point. Touching at a single endpoint counts as sharing, so [1, 4] and [4, 6] can interact.
A team is valid if at least one member of the team can interact with every other member of that team. That one member does not need to be unique. Return the largest possible size of a valid team.
Note: a team of size 1 is always valid (the single member trivially interacts with everyone else in the team, since there is nobody else).
Who can be on the same team?
Constraints
- ●
1 <= n == startTime.length == endTime.length <= 10^5 - ●
0 <= startTime[i] <= endTime[i] <= 10^9
Examples
startTime = [1,2,3], endTime = [4,5,6]3startTime = [2,5,8], endTime = [3,7,9]1startTime = [3,4,6], endTime = [8,5,7]3Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Salesforce | Medium | View all Salesforce questions → |
How to Think About It
Start by considering all possible pairs of intervals and checking if they overlap — this gives O(n²) brute force.
Sort intervals by start time so overlapping intervals group together naturally.
Use a sweep line approach: treat each start and end as events, sort them, and track active intervals.
Optimize by using a min-heap to remove intervals that end before new ones start.
The optimal approach is O(n log n) using sorting with sweep-line event processing.
Optimal Approach
Sort all intervals by their start time. Create a list of events where each interval start is +1 and each interval end is -1. Sort events by time (and by end before start for ties if endpoints don't count as overlap). Sweep through events, maintaining a running count of active intervals. Track the maximum count encountered during the sweep. This gives the maximum number of overlapping intervals at any point.
Sweep line over start and end events
What Trips People Up in Real Interviews
Clarify whether touching endpoints (e.g., [1,2] and [2,3]) count as overlapping.
Ask if intervals are guaranteed to be valid (start ≤ end) or need validation.
Mention that after sorting, you only need a single pass — don't suggest nested loops.
If asked about streaming intervals, discuss how a balanced BST or heap would maintain the sweep.
Discuss time complexity proactively: O(n log n) from sorting dominates the linear scan.
Solution Code
O(n log n) timedef max_team_size(intervals):
events = []
for start, end in intervals:
events.append((start, 1))
events.append((end, -1))
events.sort()
max_overlap = 0
current = 0
for time, change in events:
current += change
max_overlap = max(max_overlap, current)
return max_overlapFrequently Asked Questions
What is the Maximum Team Size with Overlapping Intervals problem?
Given a list of intervals where each interval represents a team member's availability window, find the maximum number of team members who can form a team by selecting overlapping time windows. This problem reduces to finding the maximum overlap of intervals at any point. Consider edge cases where intervals just touch at endpoints.
How do you solve Maximum Team Size with Overlapping Intervals?
The optimal approach runs in O(n log n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Maximum Team Size with Overlapping Intervals?
Maximum Team Size with Overlapping Intervals is asked at Salesforce. It is a medium difficulty problem.
What are common mistakes on Maximum Team Size with Overlapping Intervals?
- Clarify whether touching endpoints (e.g., [1,2] and [2,3]) count as overlapping.
- Ask if intervals are guaranteed to be valid (start ≤ end) or need validation.
- Mention that after sorting, you only need a single pass — don't suggest nested loops.
- If asked about streaming intervals, discuss how a balanced BST or heap would maintain the sweep.
- Discuss time complexity proactively: O(n log n) from sorting dominates the linear scan.