KatInterview

Find Available Time Slots in a Repeating Weekly Schedule

Greedy & IntervalsAuto-graded · 27 test cases
MediumAuto-gradedGreedy & Intervalsintervalsarraystime-zones

Last 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 start
  • min_duration: minimum acceptable slot length in minutes
  • max_results: maximum number of slots to return
  • windows: list of tuples (start, end, window_type, tz_offset) where:
    • start, end: half-open interval [start, end) in the window's local timezone
    • window_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_duration minutes long (after clipping)
  • Sorted by start time
  • Limited to the first max_results slots

Return an empty list if no qualifying slots exist.

Constraints

  • 0 ≤ current_utc < 10,080
  • 0 ≤ lead_minutes ≤ 1,000,000
  • 1 ≤ min_duration ≤ 10,080
  • 1 ≤ max_results ≤ 10,000
  • 0 ≤ len(windows) ≤ 10,000
  • For each window: 0 ≤ start < end ≤ 10,080
  • -720 ≤ tz_offset ≤ 840
  • window_type is 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.