Skip to content

Volume ITechnology

4.27.4 — Meeting Rooms ​

LeetCode 252 · Easy

The problem ​

Given meeting times, return true if a person could attend all of them — that is, if no two meetings overlap.

[[0,30],[5,10],[15,20]]   →  false
[[7,10],[2,4]]            →  true

What the problem is really asking ​

One person must attend every meeting. That is possible only when no two meetings overlap in time. So the question is simply: does any pair of meetings overlap?

The pattern ​

Checking every pair is O(n^2). Sorting makes it O(n \log n) and reduces the check to one comparison per neighbour.

Sort by start. Then two meetings overlap if and only if some meeting begins before the previous one ends.

Why neighbours are enough: after sorting, if meeting i does not overlap meeting i+1, it cannot overlap anything after that either, because everything later starts even later. So a single pass over adjacent pairs catches every conflict.

The solution ​

python
class Solution:
    def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
        intervals.sort(key=lambda x: x[0])  # (1)

        for i in range(1, len(intervals)):  # (2)
            if intervals[i][0] < intervals[i - 1][1]:     # (3) starts before the last ends
                return False

        return True  # (4)
ts
function canAttendMeetings(intervals: number[][]): boolean {
  intervals.sort((a, b) => a[0] - b[0]);  // (1)

  for (let i = 1; i < intervals.length; i++) {  // (2)
    if (intervals[i][0] < intervals[i - 1][1]) return false;  // (3)
  }

  return true;  // (4)
}

Line by line ​

(1) Sort the meetings by start time.

(2) Compare each meeting with the one just before it.

(3) It starts before the previous one ends, so the two clash. One person cannot attend both.

(4) No neighbouring pair clashes, so no pair clashes at all, for the reason given in the pattern above.

< and not <=. A meeting ending at 10 and another starting at 10 do not conflict — you walk out of one and into the next. This is the same convention as 4.27.3 and the opposite of 4.27.2.

Ask which convention applies. Every interval problem has this ambiguity, and the problem statement often does not say.

Trace ​

Take [[0, 30], [5, 10], [15, 20]]. It is already sorted by start.

iThis meetingPrevious meeting endsStarts before that?Result
1[5, 10]305 < 30, yesreturn false

Complexity, derived ​

Time. Sorting n meetings is O(n \log n). The loop then compares n - 1 neighbouring pairs, O(n). The sort dominates, so O(n \log n).

Space. Apart from the sort's own working memory, a loop counter: O(1).

Edge cases ​

  • No meetings, or one meeting. The loop never runs, and the answer is true.
  • One meeting ends exactly when the next starts, like [7, 10] and [10, 12]. Not a clash, because the check is <.
  • One meeting inside another, like [0, 30] and [5, 10]. A clash.

Where this goes next ​

  • Meeting Rooms II — how many rooms are needed when meetings do overlap. Much more interesting, and it is 4.27.5.
  • Meeting Rooms III — assign meetings to rooms and count which room is busiest, which needs two heaps.
  • My Calendar I (LeetCode 729) — the streaming version: bookings arrive one at a time and each must be checked against everything booked so far. Sorting from scratch each time is O(n \log n) per booking; an ordered map or a balanced tree makes each check O(\log n). That is the natural follow-up when the input is a stream rather than a list.

What the interviewer will push on ​

"Why is checking neighbours enough?" After sorting by start, a non-conflict with the next meeting implies a non-conflict with every later one.

"Does a meeting ending exactly when another starts count as a conflict?" No here — ask on every interval problem, because the convention flips between them.

"Can you do better than O(n \log n)?" Not in general — detecting any overlap is at least as hard as sorting. If the times were small bounded integers you could bucket them, which is the 4.4.5 idea again.

"What if bookings arrive one at a time?" An ordered map for O(\log n) per check.

One thing to volunteer: state the sorted-neighbour argument. It is one sentence, and it is the reason the solution is a single pass rather than a nested loop.

Next: 4.27.5 Meeting Rooms II — counting how many overlap at once, and the sweep line that generalises far beyond meetings.

Combine chapters into one PDF

Pick the chapters you want. They print in the order you pick them.

Volume I — Technology
Part 0 — Retention & Notation
Part 1 — Foundations of Computation
Part 2 — Operating Systems
2.1 The Kernel
2.2 Processes
2.3 Threads & Scheduling
2.4 Concurrency Under the Hood
2.5 Memory Management
2.6 File Systems
2.7 Input/Output
2.8 Linux for Engineers
2.9 Windows & macOS in Practice
Part 3 — Languages, Compilers & Runtimes
3.6 JavaScript & V8
3.7 TypeScript
3.8 Node.js Internals
3.13 Python & CPython
3.14 FastAPI & Production Python
Part 4 — Data Structures & Algorithms
4.0 Classic Problems — Before NeetCode
4.4 Arrays & Hashing — Problems
4.5 Two Pointers
4.6 Sliding Window
4.8 Stack — Problems
4.9 Linked List — Problems
4.11 Binary Search — Problems
4.13 Trees
4.14 Tree — Problems
4.15 Trie — Problems
4.17 Heap — Problems
4.18 Backtracking — Problems
4.19 Graphs
4.20 Graph — Problems
4.21 Advanced Graph — Problems
4.23 1-D DP — Problems
4.24 2-D DP — Problems
4.26 Greedy — Problems
4.27 Interval — Problems
4.28 Math & Geometry — Problems
4.30 Bit Manipulation — Problems
Part 5 — Networking & the Web
5.3 IP
5.4 Transport
5.6 HTTP
Part 6 — Frontend Engineering
6.1 Browser Internals
6.2 HTML & CSS Mastery
6.3 JavaScript in the Browser
6.4 React, from First Component to Full Shop
6.5 Frameworks & Rendering
6.8 UI Engineering & Patterns
Part 7 — Databases & Storage
7.2 SQL Mastery
7.3 Storage Engine Internals
7.4 Transactions
7.5 NoSQL Taxonomy
7.8 Analytics & Big Data
Part 8 — Security, Identity & Cryptography
8.2 Cryptography
8.3 Protocols of Trust
8.4 Identity: AuthN & AuthZ
8.5 Application Security
8.6 Infrastructure Security
Part 9 — Software Design (LLD)
9.2 OOP Foundations
9.3 Design Principles
9.4 Design Patterns
9.5 Concurrency
9.6 API Design
9.7 LLD Interview Machines
9.9 Node/Express Production Playbook
Part 10 — Distributed Systems & HLD
10.7 Consistency & Consensus
10.8 Messaging & Microservices
10.14 Caching
Building blocks from the design seat
Part 11 — System Design Case Studies
Part 12 — AI & ML Engineering
12.1 What AI Engineering Is
12.2 The Math You Need
12.3 Classical Machine Learning
12.4 Data for ML
12.5 Deep Learning
12.6 Embeddings & Vector Search
12.7 Transformers & LLMs
12.8 Building with LLMs
12.9 Retrieval-Augmented Generation
12.10 Agents
12.11 Adapting Models
12.12 Production AI
12.13 Multimodal AI
12.14 AI Security & Governance
12.15 Case Studies & Capstone
Part 13 — Cloud, DevOps & Infrastructure
13.11 Git, from First Commit to Team Workflow
Part 14 — Containers: Docker & Kubernetes
14.1 Foundations
14.2 Docker
14.3 Kubernetes
14.4 Kubernetes on Azure
Part 15 — The Craft & Career
Nothing picked yet