Nextdoor logoNextdoor
CodingFree preview

Merge Weekday Time Intervals (LC 56 Variant)

Variant of LC 56 (Merge Intervals) where each endpoint is a weekday plus clock time, e.g. `["Mon 9:00", "Mon 13:00"]`. Parse the strings, normalize every endpoint to minutes since the start of the week, then sort and merge as in LC 56. Some interviewers let you define your own input format or `Interval` class.

SWE
MLE
medium
interval
parsing
sorting
string-processing
Frequency
Medium
Last asked
2026-09-11
Stage
phone-screen

Problem

Given a list of time intervals, merge every overlapping pair and return the merged list. This is LC 56 (Merge Intervals), except each endpoint is a weekday plus a clock time rather than an integer.

  • Input endpoints are strings such as "Mon 9:00" or "Sat 13:00".
  • Intervals can span or touch across times on the same day, and merging has to account for the weekday as well as the hour and minute.
  • An interval may cross midnight into the next day ("Tue 15:00 -> Wed 1:00") or run past Sunday into the next week, so the end can be "earlier" than the start on the weekly clock.
  • Touching intervals count as overlapping ([1,4] and [4,5] become [1,5]).
  • The input format is not always fixed: some interviewers hand you nested string pairs, others let you choose the representation or define your own Interval class.
def merge_intervals(input_intervals: list[str]) -> list[str]:
    # each item looks like "Mon 9:00 -> Mon 17:00"
    ...

Examples

Input as given in one screen:

[["Mon 9:00", "Mon 13:00"], ["Mon 11:00", "Mon 16:00"]]

Other formats candidates were shown or chose:

"Mon 9:00 -> Mon 17:00"
["Mon 09:00", "Mon 17:00"]

A fuller test list in the arrow format (9 lines), including "Tue 15:00 -> Wed 1:00", "Thu 3:00 -> Fri 2:00" and "Fri 14:00 -> Sun 12:00", should come back as:

["Mon 9:00 -> Mon 20:00", "Tue 9:02 -> Wed 1:00", "Thu 3:00 -> Fri 2:00", "Fri 14:00 -> Sun 12:00"]

Variants

Plain-integer form (base LC 56 with stdin/stdout)

Each interval is [start, end] with start <= end; two intervals overlap when a.start <= b.end and b.start <= a.end, and merge into [min(start), max(end)].

  • Input: first line n, then n lines start end.
  • Output: first the count m of merged intervals, then m lines start end.
  • Constraints: 1 <= n <= 2e5, -1e9 <= start <= end <= 1e9.
  • Example: [1,3] [2,6] [8,10] [15,18] gives 3 intervals [1,6] [8,10] [15,18].

The large n makes fast input matter (see Pitfalls). The weekday-string form above is the main variant.

Hints

Pitfalls

  • The parsing is the real work. Handle single-digit hours (9:00 vs 09:00) and map weekday names consistently.
  • Merging only on clock time and ignoring the weekday gives wrong answers when intervals on different days share the same hours.
  • Zero-pad output minutes (9:05, not 9:5) and map the day index back to its name.
  • Use <= for the overlap test, not <; touching intervals merge.
  • Take the max of ends, not the new end, so full containment ([1,10] then [3,5]) stays correct.
  • For the n = 2e5 form, read all input at once with sys.stdin.buffer.read().split(); calling input() 400k times can time out in Python.
  • When you get to choose the format, say so explicitly and pick something easy to parse. A small Interval class keeps the merge logic clean.
  • Solving it is not enough on its own. Candidates who finished both phone-screen problems with passing tests were still rejected, so speed and clean code count.

Talking Points

  • Sorting by start is the whole trick. Once sorted you only compare with the last merged interval, which shows you understand the problem instead of pattern-matching.
  • Say the <= condition out loud. [1,4] and [4,5] merge because 4 <= 4; it shows you read the examples.
  • Walk the minute conversion through a concrete case. "Thu 3:00 -> Fri 2:00" becomes 4500 -> 5880; an end that lands before its start (past Sunday) gets +10080, and that wrapped group may then absorb Monday-morning intervals.
  • State the complexity. The merge pass is linear, the sort dominates, so the total is O(n log n), with no way below that for this problem.
  • Trace the given input by hand against the expected output to catch off-by-one errors in parsing.

Follow-ups

Preparation

  • Write LC 56 from memory in under 10 minutes.
  • Write a to_minutes("Mon 9:00") helper and its inverse, and test them on every weekday.
  • Run the full solution on the example above and on intervals that share hours but fall on different days.
  • Run the wrap-around case (end < start), including one that overlaps a Monday-morning interval, and check the zero-padded output format.
Was this article helpful?

Comments

Sign in to join the discussion
Loading...