KatInterview

Consolidate Overlapping Bookings in a Weekly Schedule

Greedy & IntervalsAuto-graded · 25 test cases
MediumAuto-gradedGreedy & Intervalsintervalssortingstring-parsing

Last updated

Parse weekday and 24-hour time strings, then merge touching or overlapping same-day bookings and output them in a canonical, sorted string format.

Consolidate Overlapping Bookings in a Weekly Schedule

You are given a list of bookings for a calendar week. Each booking is a pair of strings [start, end], and each string has the form:

"<Day> <H>:<MM>"
  • <Day> is one of Mon, Tue, Wed, Thu, Fri, Sat, Sun.
  • The time uses a 24-hour clock. The hour may be written with or without a leading zero (for example 9:00 and 09:00 mean the same time), and the minutes always have two digits.

Both ends of a single booking fall on the same day, and the start is strictly earlier than the end.

Task

Combine bookings that overlap into single bookings.

  • Two bookings overlap only if they are on the same day and their time ranges intersect. Ranges that merely touch also count: 9:00-13:00 and 13:00-14:00 combine into 9:00-14:00.
  • A booking contained entirely inside another is absorbed by it.
  • Bookings on different days are never combined, even if their clock times coincide.
  • Overlap is transitive: if A overlaps B and B overlaps C, all three become one booking.
  • Duplicate bookings collapse into one.

Output format

Return a list of [start, end] string pairs for the combined bookings.

  • Order the result by day of the week (Mon first, Sun last), then by start time within a day. Do not order days alphabetically.
  • Write each time as "<Day> <H>:<MM>" with no leading zero on the hour and a two-digit minute value. For example, the input "Tue 09:30" appears in the output as "Tue 9:30", and midnight appears as 0:00.
  • If the input is empty, return an empty list.

Constraints

  • Each booking's start and end are on the same day, with start strictly before end.
  • No booking crosses midnight, and there is no wrap-around from Sunday to Monday.
  • The input may be unsorted and may be large, so a solution better than O(n²) is expected.
  • Times are valid 24-hour times.

Examples

Example 1

Input: consolidate_weekly_bookings([['Mon 9:00', 'Mon 13:00'], ['Mon 11:00', 'Mon 16:00']])

Output: [['Mon 9:00', 'Mon 16:00']]

Example 2

Input: consolidate_weekly_bookings([['Mon 9:00', 'Mon 13:00'], ['Mon 13:00', 'Mon 14:00']])

Output: [['Mon 9:00', 'Mon 14:00']]

Example 3

Input: consolidate_weekly_bookings([['Tue 09:30', 'Tue 10:30'], ['Mon 14:00', 'Mon 15:00'], ['Tue 10:00', 'Tue 12:00']])

Output: [['Mon 14:00', 'Mon 15:00'], ['Tue 9:30', 'Tue 12:00']]

Hints

Hint 1

Convert each time string to an integer (minutes since midnight) and each day to an index 0-6, so values compare numerically rather than as text.

Hint 2

If you sort the records as (day_index, start, end) tuples, bookings that could merge become neighbors, and one sort handles both the day order and the time order.

Hint 3

Sweep through the sorted list keeping the last combined booking. If the next one is on the same day and starts at or before the current end, extend the end with max(); otherwise begin a new booking.