Coding questions · Apple
Compute conflicts and minimum meeting rooms
This question evaluates the ability to reason about interval overlaps and resource allocation, testing algorithmic thinking and efficient handling of time-interval data in scheduling contexts and belongs to the Coding & Algorithms domain.
You are given a list of meeting room booking requests. Each request is a half-open time interval [start, end), meaning it uses the room starting at start and releases it at end (so a meeting ending at time t does not overlap one starting at time t).
A single meeting room can host at most one request at any time.
Tasks
- Feasibility with one room: Determine whether all requests can be satisfied using only one meeting room (i.e., no time overlaps).
- Minimum rooms needed: If one room is not enough, compute the minimum number of meeting rooms required so that all requests can be satisfied.
Input / Output
- Input: An array/list
intervalsof lengthn, where each element is[start, end)withstart < end. - Output:
- A boolean
canScheduleInOneRoom. - An integer
minRooms(the minimum number of rooms needed).
- A boolean
Constraints (assume)
1 ≤ n ≤ 10^5startandendare integers (or comparable timestamps) within a reasonable range.
Example
- Input:
[[0, 30], [5, 10], [15, 20]] - Output:
canScheduleInOneRoom = false,minRooms = 2
Part 1: Can All Meetings Fit in One Room?
You are given a list of meeting requests, where each request is a half-open interval [start, end). A meeting uses the room starting at time start and releases it at time end, so a meeting ending at time t does not conflict with one starting at time t.
Determine whether all meetings can be scheduled using only one meeting room.
Return True if no two meetings overlap in time; otherwise return False.
Constraints
- 0 <= n <= 10^5, where n is the number of intervals
- Each interval has exactly two integers: start and end
- For every interval, start < end
- Intervals are half-open: [start, end)
Examples
Example 1
Input: [[0, 30], [5, 10], [15, 20]]
Output: False
Explanation: The meeting [0, 30) overlaps with both [5, 10) and [15, 20), so one room is not enough.
Example 2
Input: [[1, 3], [3, 5], [5, 8]]
Output: True
Explanation: These meetings only touch at boundaries. Because the intervals are half-open, they do not overlap.
Hints
- Try sorting the meetings by their start time first.
- After sorting, it is enough to compare each meeting with the one that ends most recently before it.
Part 2: Minimum Number of Meeting Rooms
You are given a list of meeting requests, where each request is a half-open interval [start, end). A meeting ending at time t does not overlap with a meeting starting at time t.
Compute the minimum number of meeting rooms required so that all meetings can be scheduled.
Constraints
- 0 <= n <= 10^5, where n is the number of intervals
- Each interval has exactly two integers: start and end
- For every interval, start < end
- Intervals are half-open: [start, end)
Examples
Example 1
Input: [[0, 30], [5, 10], [15, 20]]
Output: 2
Explanation: At most two meetings overlap at once, so two rooms are required.
Example 2
Input: [[1, 3], [3, 5], [5, 8]]
Output: 1
Explanation: Each meeting starts exactly when the previous one ends, so one room can be reused.
Hints
- If you process meetings in start-time order, you only need to know which scheduled meeting ends earliest.
- A min-heap of end times is a good way to reuse rooms as soon as they become free.