Venue Hours Checker with Overnight and Multi-Window Schedules
Greedy & IntervalsAuto-graded · 27 test casesLast updated
Tests whether a venue is open at a given weekday and time, handling multiple daily windows, overnight shifts, and week-boundary wraparound.
Venue Hours Checker
A venue operates on a weekly schedule. Given that schedule and a specific day and time, determine whether the venue is currently open.
The schedule is defined by two pieces of information:
- Active days: a list of weekday numbers on which the schedule applies. Days are numbered
0through6, where0is Monday and6is Sunday. - Time windows: a list of
[open_time, close_time]pairs, where each time is[hour, minute]on a 24-hour clock. These windows apply identically on every active day.
Opening and Closing Rules
- The opening minute is inclusive: the venue is considered open at exactly the opening time.
- The closing minute is exclusive: the venue is considered closed at exactly the closing time.
- If the opening time equals the closing time, the window covers no time at all (empty window).
Overnight Windows
A window crosses midnight when its opening time is later in the day than its closing time — for example, [22, 0] to [2, 0]. Such a window begins on the active day and ends in the early hours of the following calendar day.
A time that falls in the early morning (before the closing time) counts as open if the previous day is an active day and the overnight window from that previous day has not yet closed.
The week wraps around: Sunday (6) is followed by Monday (0), so a Sunday overnight window can cover early Monday morning.
Input Validation
Raise a ValueError for any of the following:
dayis not an integer in[0, 6]houris not an integer in[0, 23]minuteis not an integer in[0, 59]- Any entry in
active_daysis not an integer in[0, 6] - Any window does not have exactly two time entries, or any time entry does not have exactly two integers within their valid ranges
Overlapping windows and windows listed in any order are both valid and must be handled correctly.
Constraints
0 <= len(active_days) <= 7; each entry is an integer in[0, 6]0 <= len(windows) <= 100; each window is[[h1, m1], [h2, m2]]0 <= h1, h2 <= 23;0 <= m1, m2 <= 590 <= day <= 6,0 <= hour <= 23,0 <= minute <= 59- Invalid inputs raise
ValueError
Examples
Example 1
Input: check_venue_open([0, 1, 2, 3, 4], [[[8, 0], [10, 0]]], 0, 9, 0)
Output: True
Example 2
Input: check_venue_open([0, 1, 2, 3, 4], [[[8, 0], [10, 0]], [[14, 0], [20, 0]]], 2, 12, 0)
Output: False
Example 3
Input: check_venue_open([4, 5], [[[22, 0], [2, 0]]], 4, 23, 0)
Output: True
Example 4
Input: check_venue_open([4, 5], [[[22, 0], [2, 0]]], 5, 1, 30)
Output: True
Example 5
Input: check_venue_open([6], [[[22, 0], [2, 0]]], 0, 1, 0)
Output: True
Hints
Hint 1
Convert every time to a single integer — total minutes since midnight — so you can compare times with a single comparison instead of juggling hours and minutes separately.
Hint 2
Handle the query day and the previous day independently. For the query day, check normal (non-overnight) windows and the start of overnight windows. For the previous day, check only overnight windows whose tail end covers the current early-morning time.
Hint 3
The week is circular: the day before Monday (0) is Sunday (6). Use modular arithmetic — `(day - 1) % 7` — to get the previous day without a special case.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key insight: flatten time to minutes
Rather than comparing (hour, minute) pairs, convert every time to a single integer — total minutes since midnight (hour * 60 + minute). This collapses two-dimensional comparisons into simple integer inequalities and eliminates a whole class of off-by-one errors.
Two independent checks
Every query resolves into at most two questions:
- Is the query day an active day? If so, scan every window and check whether the query time falls inside it.
- Is the previous day an active day? If so, scan every window and check whether an overnight tail from that previous day still covers the query time.
These two checks are independent and can both return True (they won't conflict), so running them in sequence with early returns is clean and correct.
Normal vs overnight windows
A window is normal when opening < closing — the store opens and closes within the same calendar day. The query time must satisfy opening <= query_time < closing.
A window is overnight when opening > closing — it crosses midnight. It contributes two coverage zones:
- On the opening day: any time from
openingto23:59(i.e.,query_time >= opening). - On the following day: any time from
00:00up to but not includingclosing(i.e.,query_time < closing).
A window where opening == closing covers nothing, so neither condition triggers — handled automatically.
Week wraparound
previous_day = (day - 1) % 7 gives 6 (Sunday) when the query day is 0 (Monday), correctly modeling the circular week without any special-case branch.
Validation
Input validation runs before any logic. Raising ValueError early means the rest of the function can assume all values are well-formed integers in range, keeping the logic free of defensive noise.
Complexity
- Time: O(W) per query, where W is the number of windows. Each window is examined at most twice (once for the query day, once for the previous day).
- Space: O(D) to build the set of active days from the input list, where D ≤ 7.
Reference solution
from typing import List
def check_venue_open(active_days: List[int], windows: List[List[List[int]]], day: int, hour: int, minute: int) -> bool:
"""Determine whether a venue is open at a given weekday and time."""
# Validate scalar inputs
if not isinstance(day, int) or not 0 <= day <= 6:
raise ValueError('day must be an integer from 0 to 6')
if not isinstance(hour, int) or not 0 <= hour <= 23:
raise ValueError('hour must be an integer from 0 to 23')
if not isinstance(minute, int) or not 0 <= minute <= 59:
raise ValueError('minute must be an integer from 0 to 59')
# Validate active_days entries
for d in active_days:
if not isinstance(d, int) or not 0 <= d <= 6:
raise ValueError('active_days must contain integers from 0 to 6')
# Validate windows structure and ranges
for window in windows:
if not isinstance(window, list) or len(window) != 2:
raise ValueError('each window must contain an opening and a closing time')
start, end = window
if not isinstance(start, list) or len(start) != 2:
raise ValueError('each opening time must be [hour, minute]')
if not isinstance(end, list) or len(end) != 2:
raise ValueError('each closing time must be [hour, minute]')
sh, sm = start
eh, em = end
if not isinstance(sh, int) or not 0 <= sh <= 23:
raise ValueError('opening hour must be from 0 to 23')
if not isinstance(sm, int) or not 0 <= sm <= 59:
raise ValueError('opening minute must be from 0 to 59')
if not isinstance(eh, int) or not 0 <= eh <= 23:
raise ValueError('closing hour must be from 0 to 23')
if not isinstance(em, int) or not 0 <= em <= 59:
raise ValueError('closing minute must be from 0 to 59')
query_minutes = hour * 60 + minute
active_day_set = set(active_days)
# Check windows anchored to the query day itself
if day in active_day_set:
for start, end in windows:
opening = start[0] * 60 + start[1]
closing = end[0] * 60 + end[1]
# Normal (same-day) window
if opening < closing and opening <= query_minutes < closing:
return True
# Overnight window: portion on the opening day
if opening > closing and query_minutes >= opening:
return True
# Check overnight windows from the previous day whose tail covers early morning
previous_day = (day - 1) % 7
if previous_day in active_day_set:
for start, end in windows:
opening = start[0] * 60 + start[1]
closing = end[0] * 60 + end[1]
if opening > closing and query_minutes < closing:
return True
return False