progress

~/problems / Intervals

The first gap everyone shares

On a phone? Coding is easier on a laptop: email this problem to yourself . Meanwhile: quiz this topic or fight a boss.

easy ~15 min Citadel

You're writing the "find a time" button of a calendar app. events is a log of edits, in order, each a string

"<name> <action> <start> <end>"      e.g. "maya busy 09:00 10:29"
  • Everyone named in the log must attend (names are case-sensitive).
  • <start> and <end> are "HH:MM" times with start <= end, covering every minute from start through end, both included.
  • <action> is busy or free (e.g. a cancelled meeting). Later edits override earlier ones.

Implement earliest_slot(events: list[str], k: int) -> str | None: the start of the earliest run of k consecutive minutes free for everyone, ending by 23:59, or None.

events = [
    "maya busy 09:00 10:29",
    "raj busy 10:45 11:59",
    "maya busy 00:00 08:59",
    "raj free 11:00 11:59",
]
earliest_slot(events, 15)    # "10:30"   (10:30-10:44 is free for both)
earliest_slot(events, 16)    # "11:00"   (raj freed 11:00-11:59)
earliest_slot(["ana busy 00:00 23:58"], 1)   # "23:59"
earliest_slot(["ana busy 00:00 23:58"], 2)   # None
earliest_slot([], 1440)      # "00:00"

Up to 2,000 events; 1 <= k <= 10**6.

Show hint

The day has only 1440 minutes. Keep a boolean array per person, apply the edits in order, then scan the day once counting the current run of minutes free for all.

Topic: Intervals / sweep line. Sort by start, merge; sweep events for overlaps.

Read the visual guide
0:00
Ctrl ' run · Ctrl ↵ submit
esc