Cafe Open-Now Check: Split Shifts and Midnight-Crossing Hours
AppleSoftware Engineer · Technical ScreenLast updated
Build an 'is the cafe open right now' check across three stages of growing complexity -- one fixed daily window, then select weekdays, then multiple shifts per day including windows that cross midnight. Tests clean incremental time modeling more than any single clever algorithm.
Night Owl Cafe wants a small library function that answers one question: given a moment in time, is the cafe currently open? The requirements grow across three stages, and each stage should reuse the previous one's shape rather than becoming a separate code path. Work in Python, and assume times are always given as an hour from 0-23 and a minute from 0-59 on a 24-hour clock.
Part 1: One fixed window, every day
The cafe opens and closes at the same two times every single day, for example 7:00 to 19:00. Write is_open(hour, minute) that returns whether the cafe is open at that time of day. Out-of-range hours or minutes should raise ValueError. Decide, and apply consistently: a window is open starting at its opening minute up to but not including its closing minute.
Part 2: Only on certain weekdays
The cafe is now closed some days entirely (say it's shut Mondays), with the same single window on every day it is open. Extend the signature to is_open(day, hour, minute), where day is 0 for Monday through 6 for Sunday. A day with no window at all should simply report closed for every time on that day.
Part 3: Several shifts per day, including overnight ones
Some days now have more than one window -- for example a morning shift and a separate evening shift, with the cafe closed in between. On top of that, a late evening shift can run past midnight into the next calendar day (and a Sunday-evening shift can run into Monday morning). is_open(day, hour, minute) must handle all of this with one consistent rule, not a special case for 'wraps to tomorrow.'
Example schedule
schedule = {
0: [(420, 660), (1020, 1410)], # Mon: 07:00-11:00 and 17:00-23:30
1: [(420, 660), (1020, 1410)], # Tue: same
2: [(420, 660), (1020, 1410)], # Wed: same
3: [(420, 660), (1020, 1410)], # Thu: same
4: [(420, 660), (1020, 1560)], # Fri: 07:00-11:00, then 17:00 to Sat 02:00
5: [(600, 1560)], # Sat: 10:00 to Sun 02:00
6: [(1200, 1500)], # Sun: 20:00 to Mon 01:00
}
Times inside each tuple are minutes since that day's midnight, and a value above 1440 means the shift ends that many minutes into the next day.
is_open(schedule, day=0, hour=8, minute=0) -> True (Monday morning shift)
is_open(schedule, day=0, hour=12, minute=0) -> False (between Monday's two shifts)
is_open(schedule, day=4, hour=23, minute=0) -> True (inside Friday's overnight shift)
is_open(schedule, day=5, hour=1, minute=0) -> True (still Friday's shift, now on Saturday)
is_open(schedule, day=1, hour=11, minute=0) -> False (exactly the closing minute)
What this problem should cover
- A time representation that compares correctly without separate hour/minute branching
- Part 1 and Part 2 expressible as special cases of the Part 3 data shape, not separate logic
- One explicit, stated rule for the closing-minute boundary
- Correct handling of a shift that crosses midnight, including the Sunday-to-Monday case
Hints
Hint 1
Represent a moment as a single integer -- minutes since Monday 00:00 -- instead of comparing day, hour and minute as separate fields.
Hint 2
Model Part 1 and Part 2 as special cases of Part 3's shape: a schedule with the same window on every day, or with some days given an empty list.
Hint 3
A shift that crosses midnight is just a window whose end value is past 1440. You don't need a separate 'wraps to tomorrow' branch if you also check the query time shifted by one week in each direction.
Hint 4
Pick the closing-minute rule once (open at the start, closed at the end) and apply it uniformly -- don't special-case the boundary for overnight windows.
Follow-up questions
- This chain has thousands of locations answering millions of open-now checks a day. How would you restructure the per-location data so a query doesn't rescan every window?
- How would you layer in holiday hours that override the normal weekly pattern on specific calendar dates?
- A store manager wants a list of every opening and closing event in the next 24 hours, not just a yes/no answer -- how would you extend the model to produce that?
- How would daylight saving time changes affect this design, and where would you handle them?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.