Problem · Simulation

Time Slot Generator from Weekly Working Hours

Learn this problem
MediumStripe logoStripeFULLTIMEPHONE SCREEN
See Stripe hiring insights

Problem statement

Generate every 30-minute appointment slot that falls inside a datetime range and inside that day's working hours.

Inputs:

  • startDate and endDate: datetime strings "YYYY-MM-DD HH:MM" sharing one timezone.
  • workingHours: a String[] where each entry is "day_of_week,start_time,end_time"; day_of_week is 0=Monday ... 6=Sunday and the times are "HH:MM". This table repeats every week.

Output: a String[] of slots sorted by start time, each formatted as "YYYY-MM-DD HH:MM|YYYY-MM-DD HH:MM" (start and end joined by |).

Slot rules:

  • Each slot is exactly 30 minutes, generated in 30-minute increments counted from a working interval's start time.
  • A slot must be fully inside a working interval for that weekday: slot_start >= work_start and slot_end <= work_end.
  • A slot start must be >= startDate and a slot end must be <= endDate. A slot ending exactly at endDate is included; compare full datetimes, not just the date, on the boundary days.
  • If startDate >= endDate, return an empty list.
  • Overlapping or touching working intervals on the same weekday are merged so no duplicate slot is emitted (treat touching intervals like ...12:00 then 12:00... as one).

Function

generateTimeSlots(startDate: String, endDate: String, workingHours: String[]) → String[]

Examples

Example 1

startDate = "2026-03-02 10:15"endDate = "2026-03-04 11:45"workingHours = ["0,09:00,12:00", "1,13:00,14:00", "2,10:00,12:00"]return = ["2026-03-02 10:30|2026-03-02 11:00", "2026-03-02 11:00|2026-03-02 11:30", "2026-03-02 11:30|2026-03-02 12:00", "2026-03-03 13:00|2026-03-03 13:30", "2026-03-03 13:30|2026-03-03 14:00", "2026-03-04 10:00|2026-03-04 10:30", "2026-03-04 10:30|2026-03-04 11:00", "2026-03-04 11:00|2026-03-04 11:30"]
2026-03-02 is a Monday (dow 0, 09:00-12:00). The 10:00->10:30 slot is dropped because it starts before startDate 10:15; the first emitted Monday slot is 10:30->11:00. Tuesday (dow 1) gives 13:00-14:00 slots. Wednesday (dow 2, 10:00-12:00): the 11:30->12:00 slot is dropped because it ends after endDate 11:45, so the last emitted slot is 11:00->11:30.

Example 2

startDate = "2026-03-02 09:01"endDate = "2026-03-02 09:59"workingHours = ["0,09:00,10:00"]return = []
Monday interval 09:00-10:00. The 09:00->09:30 slot starts before startDate 09:01, so it is excluded. The 09:30->10:00 slot ends at 10:00 which is after endDate 09:59, so it is also excluded. No slot fully fits the range.

Constraints

  • startDate and endDate are "YYYY-MM-DD HH:MM" in one shared timezone.
  • Each workingHours row is "day_of_week,start_time,end_time" with day_of_week in 0..6 and start_time strictly before end_time.
  • Each slot is exactly 30 minutes and fully contained in a working interval.
  • Slot start >= startDate; slot end <= endDate (a slot ending exactly at endDate is included).
  • Return [] if startDate >= endDate.
  • Merge overlapping/touching same-weekday intervals before generating slots.

More Stripe problems

drafts saved locally
public String[] generateTimeSlots(String startDate, String endDate, String[] workingHours) {
  // write your code here
}
startDate"2026-03-02 10:15"
endDate"2026-03-04 11:45"
workingHours["0,09:00,12:00", "1,13:00,14:00", "2,10:00,12:00"]
expected["2026-03-02 10:30|2026-03-02 11:00", "2026-03-02 11:00|2026-03-02 11:30", "2026-03-02 11:30|2026-03-02 12:00", "2026-03-03 13:00|2026-03-03 13:30", "2026-03-03 13:30|2026-03-03 14:00", "2026-03-04 10:00|2026-03-04 10:30", "2026-03-04 10:30|2026-03-04 11:00", "2026-03-04 11:00|2026-03-04 11:30"]
checking account