← All Foundations

Intervals

Foundation drill for this topic

Drill #114 — Meeting Rooms

Easy Sort + Check

In plain English: Check if any meetings in your schedule overlap with each other.

Sort by start time, then check each consecutive pair. If any meeting starts before the previous one ends, there's a conflict. Sorting makes the check O(n).

Prompt

Given an array of meeting time intervals [[start, end], ...], determine if a person could attend all meetings (i.e., no two meetings overlap).

Try to write it from scratch before scrolling down.

Solution

def can_attend_meetings(intervals):
    intervals.sort(key=lambda x: x[0])
    for i in range(1, len(intervals)):
        if intervals[i][0] < intervals[i-1][1]:  # overlap
            return False
    return True

# Test: can_attend_meetings([[0,30],[5,10],[15,20]]) == False
# Test: can_attend_meetings([[7,10],[2,4]]) == True
O(n log n) time · O(1) space

Related Micro Drills

Quick recall drills to reinforce this pattern.

73 Sort with key function Divide & Conquer 5s

Custom key sorting is Python's primary sort mechanism. Foundation for interval and custom-order problems.

a.sort(key=lambda x: x[1])
74 Sort by multiple keys Divide & Conquer 10s

Multi-key sorting handles tiebreakers. Used in leaderboard and event scheduling.

a.sort(key=lambda x: (x[0], -x[1]))
87 Move zeros to end Pointer Manipulation 10s

Move-zeroes is remove-element followed by a fill. Classic two-pointer warm-up.

w = 0
for r in range(len(a)):
    if a[r] != 0:
        a[w] = a[r]
        w += 1
while w < len(a):
    a[w] = 0
    w += 1
See all drills in Intervals →