FrontendInterviews.dev

Loading problem…

65. Merge Intervals - Calendar Scheduler

Medium•
Acceptance: 70.00%
•
🔓3/3 Pro unlocks today

You're building a calendar scheduling feature for a meeting room booking app. Users can book time slots, and you need to merge overlapping time slots to show consolidated availability and prevent double-booking.

Given an array of intervals where intervals[i] = [start_i, end_i], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.

Requirements

1. Basic Functionality

  • Merge overlapping intervals
  • Return non-overlapping intervals
  • Handle edge cases (empty array, single interval)
  • Intervals are inclusive on both ends

Example Usage

Example 1

merge([[1,3],[2,6],[8,10],[15,18]]); // [[1,6],[8,10],[15,18]]
// Explanation: Intervals [1,3] and [2,6] overlap (2 ≤ 3), so they merge into [1,6]. 
// Intervals [8,10] and [15,18] don't overlap with any others, so they remain separate.

Example 2

merge([[1,4],[4,5]]); // [[1,5]]
// Explanation: Intervals [1,4] and [4,5] are considered overlapping because the end 
// of the first (4) equals the start of the second (4). Since intervals are inclusive 
// on both ends, they merge into [1,5].

Example 3

merge([[1,4],[2,3]]); // [[1,4]]
// Explanation: Interval [2,3] is completely contained within [1,4]. When one interval 
// is fully inside another, we merge by keeping the larger interval, resulting in [1,4].

Real-World Context

This problem models real calendar and scheduling systems:

  • Meeting room booking: Merge overlapping bookings
  • Calendar apps: Consolidate overlapping events
  • Time slot management: Optimize time slot allocation
  • Event scheduling: Merge overlapping event times

Constraints

  • 1 <= intervals.length <= 10^4
  • intervals[i].length == 2
  • 0 <= start_i <= end_i <= 10^4
  • Intervals are inclusive on both ends