</> MAANG.io
coding interview ยท 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

Merge and Intervals

What is this?

Once things are already in order, problems get much easier. Merging two sorted lists is like shuffling two pre-sorted stacks of cards into one, always taking the smaller top card. And once time slots are lined up by start time, you can spot any overlap with a single left-to-right pass instead of comparing every pair.

flowchart TD A["Merge and Intervals"] --> B["Merge two sorted lists"] A --> C["Sort intervals by start"] C --> D["Sweep left to right"] D --> E["Flag any overlap"]

๐Ÿ’ก Fun fact: The "merge" step here is the exact same move at the heart of merge sort, and it powers how databases combine sorted files too big to fit in memory.

๐Ÿ”“ The 2 problems in this chapter are free. Sign in with Google or Microsoft to start solving.


Core idea: Two recurring payoffs of sorted order. First, the merge step from Merge Sort stands on its own: combine two already-sorted sequences with a linear pass โ€” and when one has spare room, fill it from the back to avoid overwriting. Second, once items are sorted by start time, overlaps become a simple left-to-right sweep: anything that begins before the previous one ends is a conflict.


The pattern

When inputs are already sorted, you rarely need to sort again โ€” you walk them in order. Merging two sorted arrays is a two-pointer scan taking the smaller front each step; the only twist for an in-place merge is to write from the largest end backward so you never clobber a value you still need to read. For intervals, sorting by start lines events up on a timeline: scan once, and the moment a new interval's start falls before the running end, you have an overlap. Sorting turns "compare every pair" into "compare each to its neighbour."


The problems

  • Merge Sorted Array โ€” merge two sorted arrays into the first, which has trailing space. Walk both from the back, writing the larger element into the tail; O(m+n) time, O(1) extra space, no risk of overwriting unread values.
  • Meeting Rooms โ€” given meeting intervals, decide if a person can attend all of them. Sort by start time, then sweep: if any meeting begins before the previous one ends, they overlap and the answer is false. O(n log n) for the sort, O(1) afterward.

Key takeaways

  • Merging two sorted runs is linear โ€” the same step that powers Merge Sort.
  • Merge in place from the back when the destination has trailing room, so writes never overwrite unread reads.
  • Sort intervals by start, then a single sweep detects every overlap by comparing each start to the running end.
  • Why it matters: "sort, then sweep" is one of the most reused interview patterns โ€” the foundation for harder interval problems like merging and meeting-rooms-II.

Order: Merge Sorted Array โ†’ Meeting Rooms.

Meeting Rooms

Core idea: You're given a pile of meetings, each a [start, end] interval, and asked whether one person could attend every one of them โ€” i.e. do any two overlap? The trick is to stop thinking about the pile as unordered. Sort the meetings by start time, and then a conflict can only ever happen between a meeting and the one immediately before it. So you make a single pass over adjacent pairs: if any meeting starts before its predecessor ends, that's a clash and the answer is False. No clash anywhere โ†’ True. Sorting dominates: O(n log n) time, O(1) extra space (or O(n) for the sort's scratch).


The problem, rephrased

You keep one calendar. Someone hands you the day's bookings as a list of time intervals intervals[i] = [start, end], where the meeting occupies the time start โ‰ค t < end (the end is exclusive โ€” a meeting that ends at 10 frees you up at 10). Return True if you personally could sit in every meeting, and False if even one pair collides.

This is LeetCode 252 โ€” Meeting Rooms.

Input intervals What it means Output
[[0,30],[5,10],[15,20]] [5,10] and [15,20] both fall inside [0,30] False
[[7,10],[2,4]] 2โ€“4 then 7โ€“10, a clean gap between True
[[1,2],[2,3]] first ends exactly when the second begins True
[] no meetings at all True

The function returns a boolean โ€” there's nothing to mutate.


Sign in to continue reading

The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.

Sign in to read the full lesson

Sign in to MAANG.io

Use your Google or Microsoft account โ€” no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.