Quick Overview

A coding question that asks whether one person can attend every meeting in a list of half-open time intervals. It tests interval reasoning, correct treatment of back-to-back and duplicate meetings, and an efficient overlap check that scales to a hundred thousand meetings.

Check Whether One Person Can Attend Every Meeting

Company: ByteDance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

A person has a list of meetings, each with a start time and an end time. Return whether they can attend every meeting, which is possible exactly when no two meetings overlap. ### Function Signature ```python def can_attend_all(meetings: list[list[int]]) -> bool: ``` ### Rules - `meetings[i] = [start, end]` occupies the half-open interval `[start, end)`: the meeting is in progress at every time `t` with `start <= t < end`. - Two meetings overlap when some time `t` lies in both intervals. A meeting that ends at `t` and a meeting that starts at `t` do not overlap. - Identical meetings listed twice are separate meetings, and they overlap each other. - Return `True` when there are zero meetings or one meeting. ### Constraints - `0 <= len(meetings) <= 10^5` - `0 <= start < end <= 10^9` for every meeting - Times are integers, so every value fits in a 32-bit signed integer. - `meetings` is in no particular order. ### Examples **Example 1** ```text Input: meetings = [[9, 12], [1, 4], [4, 9]] Output: True ``` In time order the meetings are `[1, 4)`, `[4, 9)` and `[9, 12)`. Each one starts exactly when the previous one ends, so none overlap. **Example 2** ```text Input: meetings = [[2, 7], [8, 10], [6, 9]] Output: False ``` At time `6`, both `[2, 7)` and `[6, 9)` are in progress. **Example 3** ```text Input: meetings = [[3, 5], [3, 5]] Output: False ``` The two copies of `[3, 5)` are both in progress at time `3`.

Overview: A coding question that asks whether one person can attend every meeting in a list of half-open time intervals. It tests interval reasoning, correct treatment of back-to-back and duplicate meetings, and an efficient overlap check that scales to a hundred thousand meetings.

A person has a list of meetings, each with a start time and an end time. Return whether they can attend every meeting, which is possible exactly when no two meetings overlap. Implement `can_attend_all(meetings)`, where `meetings[i] = [start, end]`. **Rules** - Meeting `[start, end]` occupies the half-open interval `[start, end)`: it is in progress at every time `t` with `start <= t < end`. - Two meetings overlap when some time `t` lies in both intervals. A meeting that ends at time `t` and a meeting that starts at time `t` do not overlap. - Identical meetings listed twice are separate meetings, and they overlap each other. - Return `True` (`true` in JavaScript, Java and C++) when no two meetings overlap, otherwise `False` (`false`). With zero meetings or one meeting, return `True`. **Constraints** - `0 <= len(meetings) <= 10^5` - `0 <= start < end <= 10^9` for every meeting - Times are integers, so every value fits in a 32-bit signed integer (no value exceeds 2^31 - 1). - `meetings` is in no particular order. **Example 1** ```text Input: meetings = [[9, 12], [1, 4], [4, 9]] Output: True ``` In time order the meetings are `[1, 4)`, `[4, 9)` and `[9, 12)`. Each one starts exactly when the previous one ends, so none overlap. **Example 2** ```text Input: meetings = [[2, 7], [8, 10], [6, 9]] Output: False ``` At time `6`, both `[2, 7)` and `[6, 9)` are in progress.

Constraints

  • 0 <= len(meetings) <= 10^5
  • meetings[i] = [start, end] with 0 <= start < end <= 10^9 for every meeting
  • Times are integers, so every value fits in a 32-bit signed integer
  • meetings is in no particular order; identical meetings may be listed more than once

Examples

Input: ([],)

Expected Output: True

Explanation: Empty list: zero meetings can always be attended.

Input: ([[5, 10]],)

Expected Output: True

Explanation: A single meeting has nothing to overlap with.

Hints

  1. Two meetings [a, b) and [c, d) share a time exactly when a < d and c < b; a meeting ending at t and one starting at t do not.
  2. The two meetings that overlap need not be next to each other in the input, and comparing every pair is too slow for 10^5 meetings.
  3. Do not merge or de-duplicate identical meetings: two copies of the same meeting overlap each other.

Loading coding console...

Show the approach

Approach

Sort a copy of the meetings numerically by start time (ties by end time) and compare each meeting with the one just before it in that order. If some meeting starts before its predecessor ends (start_i < end_{i-1}), then time start_i lies in both, because the predecessor started no later, so the answer is False. Conversely, if every meeting starts at or after its predecessor's end, then end_j <= start_{j+1} < end_{j+1} <= start_{j+2} and so on, so every earlier meeting has ended by the time each meeting starts; no two meetings share a time and the answer is True. The strict < comparison implements the half-open rule: a meeting that starts exactly when another ends does not overlap it. Identical meetings sort next to each other and are caught because start < end, so they must not be merged or de-duplicated. With zero or one meeting there is nothing to compare and the result is True. The sort must be numeric (JavaScript's default sort compares strings and would misorder [10, 20] before [2, 9]). All values stay within 32-bit signed integers and no sums are formed, so plain int is safe.

Time complexity:
O(n log n)
Space complexity:
O(n)