The Problem

Given an array of meeting time intervals where intervals[i] = [start_i, end_i], determine if a person could attend all meetings.

Two meetings overlap if one starts before the other ends. A person cannot attend overlapping meetings, so the answer is false if any overlap exists, and true otherwise.

Interactive Visualization

Speed:
Meeting Timeline
Code
fun canAttendMeetings(intervals: Array<IntArray>): Boolean {    intervals.sortBy { it[0] }    for (i in 1 until intervals.size) {        if (intervals[i][0] < intervals[i - 1][1]) {            return false  // overlap found        }    }    return true}
Current Step
Press Step or Play to begin.

How It Works

Key Insight

If meetings are sorted by start time, two meetings can only overlap if a meeting starts before the previous one ends. After sorting, a single pass comparing consecutive pairs is enough to detect any conflict.

The Algorithm

  • Sort the intervals by their start time.
  • Scan through consecutive pairs: for each i from 1 to n-1, check if intervals[i][0] < intervals[i-1][1].
  • If the condition is true, the current meeting starts before the previous one ends — an overlap exists, return False.
  • If no overlap is found after scanning all pairs, return True.

Why Sorting Works

Once sorted by start time, overlaps can only occur between adjacent intervals. If interval A ends before interval B starts (and B starts before C), then A also ends before C starts. So we never need to compare non-adjacent pairs.

Complexity

Time O(n log n)
Space O(1)

Sorting takes O(n log n) and the single-pass scan takes O(n), giving O(n log n) overall. The sort is in-place (or uses O(log n) stack space), so auxiliary space is O(1).