Coding interview problems โบ Intervals
๐ Intervals Easy
Meeting Rooms
Meeting Rooms explained: can one person attend every meeting? Sort by start and check neighbours, O(n log n) tested Python and a live visual.
Also searched as: can a person attend all meetings ยท check overlapping meetings ยท meeting rooms I
Try it live Free
Merge Intervals & Meeting Rooms
Lay the meetings on the Merge Intervals timeline: if any two fuse into one busy block, the person cannot attend them all.
Also useful: Sorting Race: Insertion vs Merge vs Quick
The problem
Given a list of meeting times [start, end], decide whether one person could attend all of them, meaning no two meetings overlap. A meeting ending at 10 and another starting at 10 do not clash.
The key insight
Sort the meetings by start time. If any two meetings overlap, then some meeting overlaps the one right after it in sorted order. So you only need to compare neighbours: does the next meeting start before the current one ends?
Step-by-step approach
- Sort the meetings by start.
- For each adjacent pair, check whether
next.start < current.end. - If any pair overlaps, return
False. - Otherwise return
True.
Python solution
def can_attend_meetings(intervals):
intervals = sorted(intervals)
for (s1, e1), (s2, e2) in zip(intervals, intervals[1:]):
if s2 < e1: # next starts before this one ends
return False
return TrueComplexity
Sorting costs O(n log n); the neighbour check is O(n). sorted makes a copy, which is O(n) space.
Common mistakes
- Using
<=and treating back-to-back meetings as a clash. - Checking only consecutive meetings in the original, unsorted order.
- Sorting in place when the caller did not expect the input to be modified.
Frequently asked questions
What is the time and space complexity of Meeting Rooms?
O(n log n) time for the sort and O(n) or O(1) extra space depending on whether you sort a copy or in place.
Why is checking adjacent meetings enough?
After sorting by start, if meeting A overlaps a later meeting C, then the meeting B just after A starts no later than C, so it starts before A ends too. An overlap therefore always shows up between neighbours.
What if I need the minimum number of rooms?
That is Meeting Rooms II. Count +1 at each start and -1 at each end in time order, or keep a min-heap of end times; the peak count is the answer.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Intervals Merge Intervals & Meeting Rooms lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.