Find Available Time Slots in a Repeating Weekly Schedule
Greedy & IntervalsAuto-graded · 27 test casesLast updated
Tests interval merging with time zones, weekly recurrence, and range filtering to find deployable windows on a cyclic timeline.
You're building a scheduler that operates on a repeating 10,080-minute week (7 days × 24 hours × 60 minutes). Time is measured in minutes where minute 0 marks the week's beginning, and the pattern repeats indefinitely.
Your system tracks two types of time windows:
- Allowed windows: intervals when operations can occur
- Freeze windows: intervals when operations are blocked
Each window is defined in its local timezone and repeats every week. Your task is to find continuous time slots where operations are possible, considering timezone conversions, a lead time requirement, and minimum slot duration.
Input Format
You receive:
current_utc: the current UTC minute within the week (0 ≤ current_utc < 10,080)lead_minutes: required advance notice before an operation can startmin_duration: minimum acceptable slot length in minutesmax_results: maximum number of slots to returnwindows: list of tuples(start, end, window_type, tz_offset)where:start,end: half-open interval[start, end)in the window's local timezonewindow_type: either"allowed"or"freeze"tz_offset: timezone offset in minutes (local time - UTC)
Rules
Timezone Conversion: A window defined as [start, end) with offset tz_offset actually covers [start - tz_offset, end - tz_offset) in UTC. For example, a UTC-5 timezone has offset -300; a UTC+2 timezone has offset +120.
Weekly Recurrence: Each window repeats every 10,080 minutes on the UTC timeline. A window may appear at negative times or beyond minute 10,080 due to offset conversion, and these occurrences wrap around the weekly cycle.
Availability Logic: A minute is available only if:
- At least one allowed window covers it, AND
- No freeze window covers it
If freeze and allowed windows overlap, the freeze takes precedence.
Search Window: Only consider the time range [earliest, earliest + 10,080) where earliest = current_utc + lead_minutes. This represents the next reachable week of scheduling opportunities.
Output Format: Return available slots as [slot_start, slot_end] representing half-open intervals. Slots are:
- Maximal continuous runs of available minutes
- Clipped to the search window boundaries
- At least
min_durationminutes long (after clipping) - Sorted by start time
- Limited to the first
max_resultsslots
Return an empty list if no qualifying slots exist.
Constraints
0 ≤ current_utc < 10,0800 ≤ lead_minutes ≤ 1,000,0001 ≤ min_duration ≤ 10,0801 ≤ max_results ≤ 10,0000 ≤ len(windows) ≤ 10,000- For each window:
0 ≤ start < end ≤ 10,080 -720 ≤ tz_offset ≤ 840window_typeis exactly"allowed"or"freeze"
Examples
Example 1
Input: find_available_slots(0, 0, 1, 10, [[600, 1200, 'allowed', 0], [1000, 1080, 'freeze', 0], [1150, 1500, 'allowed', 0], [9900, 10080, 'allowed', 0], [0, 120, 'allowed', 0]])
Output: [[0, 120], [600, 1000], [1080, 1500], [9900, 10080]]
Example 2
Input: find_available_slots(10000, 60, 90, 3, [[0, 240, 'allowed', 0], [9960, 10080, 'allowed', 0], [120, 150, 'freeze', 0], [540, 720, 'allowed', 120], [3000, 3030, 'allowed', 0]])
Output: [[10060, 10200], [10230, 10320], [10500, 10680]]
Example 3
Input: find_available_slots(50, 0, 30, 5, [[0, 180, 'allowed', 120], [30, 60, 'freeze', 120]])
Output: [[9960, 9990], [10020, 10130]]
Hints
Hint 1
Convert all windows to UTC first, then handle the weekly repetition by finding which occurrences intersect your search horizon.
Hint 2
Use difference arrays (or event sweeping) to track overlapping intervals—you need counts, not just boolean coverage, since multiple windows can overlap.
Hint 3
The search window is exactly one week long, so each converted window produces at most 2-3 relevant occurrences. Calculate which week multiples to check using floor division.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is that we only need to examine one fixed time range: the week starting at earliest = current_utc + lead_minutes. Because the schedule repeats every 10,080 minutes, this single week contains all the information we need.
The algorithm has three phases:
1. Convert windows to UTC and find relevant occurrences
Each window is defined in its local timezone. We convert it to UTC by subtracting the offset: a window [start, end) with offset tz_offset becomes [start - tz_offset, end - tz_offset) in UTC.
Since the schedule repeats weekly, each window has infinitely many occurrences at positions [utc_start + k×10080, utc_end + k×10080) for integer k. We only care about occurrences that overlap our search horizon [earliest, earliest + 10080).
Using floor division k = (earliest - utc_end) // 10080, we find the first potentially overlapping occurrence, then increment k until occurrences no longer overlap the horizon. Each window produces at most 2-3 relevant occurrences.
2. Track coverage with difference arrays
We maintain two difference arrays indexed by position within the search horizon: one for allowed windows, one for freeze windows. When a clipped occurrence spans [lo, hi), we add +1 at index lo - earliest and -1 at index hi - earliest.
This technique handles overlapping windows correctly by counting coverage depth rather than treating it as boolean. Multiple allowed windows can overlap, and we need to track when the last one ends.
3. Sweep to identify available slots
We scan through the horizon, maintaining running sums of allowed and freeze coverage. A minute is available when allowed_count > 0 and freeze_count == 0 (freeze takes precedence).
We track the start of the current continuous available run. When availability ends, we check if the run meets the minimum duration requirement. We include a sentinel at the horizon end to ensure any final slot gets closed properly.
We stop as soon as we've collected max_results slots.
Why It Works
- Overlapping windows are handled correctly because we count coverage depth, not just boolean presence
- Freeze precedence is enforced by the condition
freeze_count == 0 - Cross-boundary continuity happens automatically—consecutive available minutes form one slot regardless of which window(s) provided them
- Duration filtering after clipping ensures we measure slot length within the search horizon, as required
- Chronological ordering is guaranteed since we sweep forward through time
Complexity
- Time: O(n + W) where n is the number of windows and W = 10,080. Each window requires constant work to find its occurrences, and the sweep is linear in the week length.
- Space: O(W) for the two difference arrays, plus O(max_results) for the output.
Solution Code
def find_available_slots(current_utc: int, lead_minutes: int, min_duration: int, max_results: int, windows: list[tuple[int, int, str, int]]) -> list[list[int]]:
WEEK_MINUTES = 10080
earliest = current_utc + lead_minutes
horizon_end = earliest + WEEK_MINUTES
# Difference arrays for tracking coverage
allowed_diff = [0] * (WEEK_MINUTES + 1)
freeze_diff = [0] * (WEEK_MINUTES + 1)
# Process each window
for start, end, window_type, tz_offset in windows:
# Convert to UTC
utc_start = start - tz_offset
utc_end = end - tz_offset
# Choose the appropriate difference array
diff_array = allowed_diff if window_type == "allowed" else freeze_diff
# Find weekly occurrences that overlap our search horizon
week_multiplier = (earliest - utc_end) // WEEK_MINUTES
while utc_start + week_multiplier * WEEK_MINUTES < horizon_end:
# Clip occurrence to search horizon
occurrence_start = max(utc_start + week_multiplier * WEEK_MINUTES, earliest)
occurrence_end = min(utc_end + week_multiplier * WEEK_MINUTES, horizon_end)
if occurrence_start < occurrence_end:
# Record in difference array (relative to earliest)
diff_array[occurrence_start - earliest] += 1
diff_array[occurrence_end - earliest] -= 1
week_multiplier += 1
# Sweep through the horizon to find available slots
result = []
allowed_count = 0
freeze_count = 0
slot_start = -1
for minute_offset in range(WEEK_MINUTES + 1):
# Update coverage counts
if minute_offset < WEEK_MINUTES:
allowed_count += allowed_diff[minute_offset]
freeze_count += freeze_diff[minute_offset]
is_available = allowed_count > 0 and freeze_count == 0
else:
# Sentinel: force close any open slot at horizon end
is_available = False
if is_available:
# Start or continue a slot
if slot_start < 0:
slot_start = minute_offset
else:
# End a slot if one is open
if slot_start >= 0:
slot_length = minute_offset - slot_start
if slot_length >= min_duration:
result.append([slot_start + earliest, minute_offset + earliest])
if len(result) >= max_results:
return result
slot_start = -1
return result