KatInterview

Weekly Deploy Windows with Time Zones and Blackouts

Greedy & IntervalsAuto-graded · 20 test cases
MediumAuto-gradedGreedy & Intervalsintervalssweep-linetime-zones

Last updated

Convert weekly recurring allow and freeze windows from local time zones to UTC, subtract the freezes, and list the first usable slots after a notice period.

Weekly Deploy Windows

A platform team schedules database maintenance on a calendar that repeats every week. Time is counted in whole minutes, and one full cycle has W = 10080 minutes (7 * 24 * 60).

Regional teams each publish rules. A rule has one of two kinds:

  • "open": maintenance is allowed during the rule's window.
  • "blackout": maintenance is forbidden during the rule's window. A blackout always overrides any number of open rules.

A minute is usable if at least one open rule covers it and no blackout rule covers it. A slot is a maximal run of consecutive usable minutes, written as a half-open interval [start, end). Two windows that merely touch, such as [a, b) and [b, c), form one continuous run.

Rule format

Each rule is (start, end, kind, utc_offset):

  • start and end are minutes in the rule's local week, with 0 <= start < end <= W.
  • utc_offset is local time minus UTC, in minutes (UTC+2 is 120, UTC-1 is -60).
  • In UTC, the rule covers [start - utc_offset, end - utc_offset).
  • The rule repeats every W minutes in both directions, so a converted window may start before minute 0 or extend past minute W. Those parts belong to the neighbouring weeks.

Task

Implement plan_windows(now_minute, notice, min_length, limit, rules).

  • now_minute is the current minute within the current UTC week (0 <= now_minute < W).
  • Maintenance cannot begin before lo = now_minute + notice.
  • Only the horizon [lo, lo + W) is considered. Slots that cross either edge of the horizon are clipped to it.
  • Discard slots whose length (end - start) is less than min_length.
  • Return the first limit remaining slots in increasing time order, each as [start, end].
  • Times are on one continuous UTC timeline, so minute W is the start of the next week and returned values may exceed W.

If no slot qualifies, return an empty list.

Constraints

  • 1 <= len(rules) <= 10^4
  • 0 <= now_minute < 10080
  • 0 <= notice <= 10^5
  • |utc_offset| <= 1440
  • 0 <= start < end <= 10080 for every rule
  • kind is either "open" or "blackout"
  • min_length >= 1, limit >= 1

Examples

Example 1

Input: plan_windows(0, 0, 1, 99, [[540, 1020, 'open', 0], [1980, 2460, 'open', 0], [600, 660, 'blackout', 0], [2400, 2520, 'blackout', 0]])

Output: [[540, 600], [660, 1020], [1980, 2400]]

Example 2

Input: plan_windows(0, 0, 1, 99, [[9900, 10080, 'open', 0], [0, 120, 'open', 0], [480, 600, 'open', 120], [10020, 10080, 'blackout', -60]])

Output: [[60, 120], [360, 480], [9900, 10080]]

Example 3

Input: plan_windows(9950, 30, 45, 2, [[9900, 10080, 'open', 0], [0, 120, 'open', 0], [480, 600, 'open', 120], [10020, 10080, 'blackout', -60]])

Output: [[9980, 10080], [10140, 10200]]

Hints

Hint 1

First convert every rule to UTC and fix the horizon [lo, lo + W). Because each rule is at most W long, only a couple of weekly copies of it can touch the horizon. Floor division gives the first relevant copy, even for negative shifts.

Hint 2

Clip each weekly copy to the horizon and drop empty pieces. Then you only have plain intervals on a line, each labelled open or blackout.

Hint 3

Sweep over the sorted endpoints while tracking how many open and how many blackout intervals are active. A stretch is usable when open > 0 and blackout == 0. Extend the previous slot if it ends exactly where the new stretch starts, then filter by min_length and cut to limit.