Store Open-Hours Query: Weekdays, Split Shifts and Overnight Windows in Java
Company: Apple
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Technical Screen
A store needs a query that reports whether it is open at a given time. The interviewer builds the problem up in steps, and each step makes the store's schedule richer. Write the solution in Java, keep the earlier queries working as the model grows, and keep the code readable: at the end, the interviewer asks how readable you think your code is.
Times are given on a 24-hour clock as an hour and a minute, for example 22:10 as `hour = 22`, `minute = 10`.
### Constraints and Clarifications
- Write the solution in Java.
- Hours are 0 to 23 and minutes are 0 to 59.
- If you want to change how a time or a day is passed in, agree on it with the interviewer first.
### Clarifying Questions
- Is the store open at exactly its closing time, for example at 10:00 for a window that ends at 10:00?
- How is the day of the week passed in, and which day starts the week?
- Can a window have the same opening and closing time, and what would that mean?
- Can windows overlap, or be given out of order?
### Part 1 — One opening window
The store has one opening time and one closing time, the same every day. Write a query that returns whether the store is open at a given time of day.
```hint Pick one time unit
Comparing hours and minutes as separate fields invites mistakes at the boundaries. Consider a single number that orders times correctly.
```
#### What This Part Should Cover
- A representation of the time of day that compares correctly
- One explicit rule for the opening and the closing minute
- Validation of out-of-range hours and minutes
### Part 2 — Open on certain days
The store is now open only on some days of the week, for example Monday through Friday, with the same window on each open day. The query now takes a day of the week and a time.
```hint Earlier parts as special cases
Think about which data structure answers "is the store open at all on this day?" directly, and how Part 1 can become a special case of the new model instead of a separate code path.
```
#### What This Part Should Cover
- A day-of-week representation that rules out invalid days
- Extending the API without duplicating the Part 1 logic
- Keeping the Part 1 query available
### Part 3 — Several windows per day
Each open day now has several opening windows, for example 08:00 to 10:00 and 14:00 to 20:00. The windows are the same on every open day. The query still takes a day and a time.
```hint Gaps and edges
Test a time inside the gap between two windows, and the exact minutes at which each window opens and closes.
```
#### What This Part Should Cover
- A collection of windows checked by each query
- Correct answers in the gaps between windows and at window boundaries
- How overlapping or unsorted windows are handled
### Part 4 — Windows that cross midnight
A window may now cross midnight. For example, the store is open on Friday and Saturday from 22:00 to 02:00, so Friday's window runs into early Saturday and Saturday's window runs into early Sunday. Make the query handle this, including a window that starts on the last day of the week and ends on the first, such as Sunday night into Monday morning.
```hint Whose window is it?
At 01:00 on some day, the window that is open may have started on a different day. Think about which day's schedule you need to consult.
```
```hint The end of the week
Check what your day arithmetic does when it steps back from the first day of the week or forward from the last.
```
#### What This Part Should Cover
- Recognizing a window that crosses midnight from its opening and closing times
- Attributing early-morning minutes to the previous day's window
- Wrapping correctly between the last and the first day of the week
- Tests for the minutes on both sides of midnight and of the week boundary
### What a Strong Answer Covers
- A working solution after every step, with the earlier queries still answered correctly
- A data model that absorbs each new requirement instead of accumulating special cases or parallel overloads
- Readability: named types for days, times and windows, small methods, no unexplained constants
- Explicit boundary rules, backed by tests at window edges, in gaps, at midnight and at the week boundary
- A candid explanation of the code's readability and of how it would change for the next requirement
### Follow-up Questions
- How readable is your code, and what would you change so that someone else could maintain it?
- Each day now has its own windows, and holidays override the weekly schedule. How does your model change?
- The query is called very often against a fixed schedule. How would you answer it in constant time?
- How would time zones and daylight-saving transitions affect the model?
Overview: A progressive Java coding exercise that asks whether a store is open at a given time as its schedule grows from one daily window to selected weekdays, several windows per day, and overnight windows that cross midnight and wrap from Sunday to Monday. It tests time modeling, boundary handling, and keeping evolving code readable.
Read the full Apple Software Engineer interview experience this question came from