Bird
Raised Fist0
Interview PrepintervalseasyFacebookAmazonBloomberg

Meeting Rooms I

Choose your preparation mode4 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
📋
Problem

Imagine you have a conference room and a list of meetings scheduled with start and end times. You want to know if you can hold all meetings in that single room without any overlaps.

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings without any overlaps. Return true if no meetings overlap, otherwise false.

1 ≤ n ≤ 10^50 ≤ start_i < end_i ≤ 10^9Intervals may not be sorted initially
Edge cases: Single meeting → always trueMeetings that touch but do not overlap, e.g. [1,5] and [5,10] → trueMeetings with same start and end times, e.g. [2,2] → true (zero length meeting)
</>
IDE
def can_attend_meetings(intervals: list[list[int]]) -> bool:public boolean canAttendMeetings(int[][] intervals)bool canAttendMeetings(vector<vector<int>>& intervals)function canAttendMeetings(intervals)
def can_attend_meetings(intervals):
    # Write your solution here
    pass
class Solution {
    public boolean canAttendMeetings(int[][] intervals) {
        // Write your solution here
        return false;
    }
}
#include <vector>
using namespace std;

bool canAttendMeetings(vector<vector<int>>& intervals) {
    // Write your solution here
    return false;
}
function canAttendMeetings(intervals) {
    // Write your solution here
}
Coming soon
0/9
Common Bugs to Avoid
Wrong: trueFailing to detect overlapping intervals due to missing sorting or incorrect overlap condition.Sort intervals by start time and check if intervals[i][0] < intervals[i-1][1] to detect overlaps.
Wrong: falseIncorrectly treating touching intervals as overlapping by using <= instead of < in overlap check.Use strict less-than comparison intervals[i][0] < intervals[i-1][1] to allow touching intervals.
Wrong: falseFailing on empty or single interval inputs by not handling these base cases explicitly.Return true immediately if intervals list is empty or has only one interval.
Wrong: trueUsing a greedy approach that only checks earliest start without verifying all overlaps.Sort intervals and check all adjacent pairs for overlaps, not just earliest start.
Wrong: TLEUsing nested loops (O(n^2)) for overlap detection on large inputs.Use sorting plus single pass overlap check to achieve O(n log n) time complexity.
Test Cases
t1_01basic
Input{"intervals":[[0,30],[5,10],[15,20]]}
Expectedfalse

The meeting [0,30] overlaps with [5,10], so it's impossible to attend all.

t1_02basic
Input{"intervals":[[7,10],[2,4]]}
Expectedtrue

No meetings overlap; intervals [2,4] and [7,10] are separate.

t2_01edge
Input{"intervals":[]}
Expectedtrue

Empty list means no meetings, so trivially no overlaps.

t2_02edge
Input{"intervals":[[5,10]]}
Expectedtrue

Single meeting cannot overlap with anything else.

t2_03edge
Input{"intervals":[[1,5],[5,10]]}
Expectedtrue

Meetings that touch but do not overlap are allowed.

t3_01corner
Input{"intervals":[[1,10],[2,3],[4,5],[6,7]]}
Expectedfalse

Nested intervals cause overlap; greedy approach picking earliest start only fails.

t3_02corner
Input{"intervals":[[2,2],[2,2],[2,2]]}
Expectedtrue

Zero-length meetings at same time do not overlap.

t3_03corner
Input{"intervals":[[1,4],[2,5],[5,6]]}
Expectedfalse

Overlap between [1,4] and [2,5] causes false; test catches off-by-one errors.

t4_01performance
Input{"_description":"n=100000 at constraint boundary - executor generates this"}
⏱ Performance - must finish in 2000ms

n=100000 intervals must be processed in O(n log n) time within 2 seconds.

Practice

(1/5)
1. Given multiple employees' schedules with intervals representing their busy times, which algorithmic approach guarantees finding all common free time intervals where no employee is busy?
easy
A. Greedy interval scheduling that picks earliest finishing intervals
B. Dynamic programming to find maximum non-overlapping intervals
C. Sweep line / event processing that tracks interval start and end events
D. Brute force checking every time point across all intervals

Solution

  1. Step 1: Understand problem requires identifying gaps where no intervals overlap

    Greedy scheduling or DP focus on selecting intervals, not gaps between them.
  2. Step 2: Sweep line processes all start/end events in order, tracking active intervals to find free gaps

    This approach efficiently detects when no employee is busy, yielding correct free times.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Sweep line tracks active intervals and finds free gaps [OK]
Hint: Sweep line tracks interval endpoints to find free gaps [OK]
Common Mistakes:
  • Assuming greedy or DP can find free gaps directly
2. Consider the following Python function that finds the minimum number of arrows to burst balloons represented as intervals. Given the input points = [[10,16],[2,8],[1,6],[7,12]], what is the value of arrows after processing all intervals?
easy
A. 1
B. 4
C. 3
D. 2

Solution

  1. Step 1: Sort intervals by end coordinate

    Sorted points: [[1,6],[2,8],[7,12],[10,16]]
  2. Step 2: Iterate and count arrows

    Start with arrow_pos=6, arrows=1. Next interval start=2 ≤ 6 (covered), next start=7 > 6 (new arrow), arrows=2, arrow_pos=12. Next start=10 ≤ 12 (covered).
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Two arrows suffice to burst all balloons [OK]
Hint: Sort by end, count arrows when start > current arrow position [OK]
Common Mistakes:
  • Not sorting intervals before processing
  • Using start >= arrow_pos instead of start > arrow_pos
  • Off-by-one errors in counting arrows
3. What is the time complexity of the optimal approach that inserts a new interval into a list of n intervals and merges overlapping intervals, where the approach is to append the new interval, sort all intervals by start time, then merge in one pass?
medium
A. O(n)
B. O(n^2)
C. O(n log n)
D. O(log n)

Solution

  1. Step 1: Identify sorting cost

    Appending is O(1), but sorting n+1 intervals takes O(n log n) time.
  2. Step 2: Identify merging cost

    Merging intervals in one pass is O(n) since each interval is processed once.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Sorting dominates overall time complexity [OK]
Hint: Sorting dominates time complexity -> O(n log n) [OK]
Common Mistakes:
  • Assuming linear time because merging is O(n)
  • Confusing sorting cost with merging cost
  • Thinking recursion stack adds extra complexity
4. What is the time complexity of the optimal greedy algorithm that finds the maximum number of non-overlapping intervals by sorting intervals and iterating through them once?
medium
A. O(n log n) due to sorting plus O(n) iteration, total O(n log n)
B. O(n^2) due to nested comparisons between intervals
C. O(n) since it only iterates once through the intervals
D. O(n log n) because of sorting intervals by start or end time

Solution

  1. Step 1: Identify sorting cost

    Sorting intervals by start or end time takes O(n log n) time.
  2. Step 2: Identify iteration cost

    Iterating through the sorted intervals to select non-overlapping ones takes O(n) time.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Sorting dominates, total complexity is O(n log n) [OK]
Hint: Sorting dominates time complexity in interval scheduling [OK]
Common Mistakes:
  • Assuming iteration alone is O(n) and ignoring sorting
  • Mistaking nested loops causing O(n^2)
  • Confusing sorting and iteration costs
5. Suppose the car pooling problem is modified so that trips can overlap and passengers can be picked up and dropped off multiple times (i.e., trips can reuse the same passengers). Which of the following algorithmic changes correctly adapts the solution to handle this variant efficiently?
hard
A. Use a segment tree or binary indexed tree to efficiently update and query passenger counts over intervals
B. Switch to a priority queue to process events in order and track current passengers dynamically
C. Use the same difference array approach but multiply passenger counts by the number of trips to simulate reuse
D. Modify the difference array to allow negative passenger counts and track net changes carefully

Solution

  1. Step 1: Understand reuse complexity

    Reusing passengers means overlapping intervals can increase and decrease counts multiple times, requiring efficient range updates and queries.
  2. Step 2: Identify suitable data structure

    Segment trees or binary indexed trees support efficient interval updates and queries, handling overlapping intervals with reuse.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Segment trees handle dynamic interval sums efficiently [OK]
Hint: Reuse -> need data structure for interval updates [OK]
Common Mistakes:
  • Multiplying counts breaks logic
  • Priority queue alone can't handle reuse efficiently
  • Allowing negative counts without structure causes errors