Generate Available 30-Minute Time Slots
Company: Stripe
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates date/time arithmetic, interval and recurrence handling, edge-case reasoning, and test-case design within scheduling logic for generating fixed 30-minute slots from weekly working-hours and a requested datetime range.
Constraints
- start_datetime and end_datetime use the format 'YYYY-MM-DD HH:MM'.
- start_datetime <= end_datetime.
- 0 <= len(working_hours) <= 100.
- working-hours intervals do not cross midnight; start_time <= end_time.
- All times are local naive times with no timezone conversion required.
- Candidate slots are generated every 30 minutes starting from each working-hours interval's start_time.
Examples
Input: ("2024-01-01 09:00", "2024-01-03 12:00", [("Monday", "09:00", "10:00"), ("Tuesday", "13:00", "14:00"), ("Wednesday", "11:00", "12:00")])
Expected Output: [["2024-01-01 09:00", "2024-01-01 09:30"], ["2024-01-01 09:30", "2024-01-01 10:00"], ["2024-01-02 13:00", "2024-01-02 13:30"], ["2024-01-02 13:30", "2024-01-02 14:00"], ["2024-01-03 11:00", "2024-01-03 11:30"], ["2024-01-03 11:30", "2024-01-03 12:00"]]
Explanation: The requested range spans Monday through Wednesday. Each listed working interval contributes two 30-minute slots, all inside the requested range.
Input: ("2024-01-01 09:15", "2024-01-02 10:45", [("Monday", "09:00", "10:30"), ("Tuesday", "10:00", "12:00")])
Expected Output: [["2024-01-01 09:30", "2024-01-01 10:00"], ["2024-01-01 10:00", "2024-01-01 10:30"], ["2024-01-02 10:00", "2024-01-02 10:30"]]
Explanation: The Monday 09:00-09:30 slot starts before the requested start time, so it is excluded. The Tuesday 10:30-11:00 slot ends after the requested end time, so it is excluded.
Input: ("2024-01-01 00:00", "2024-01-02 23:59", [("Wednesday", "09:00", "10:00")])
Expected Output: []
Explanation: The requested range covers Monday and Tuesday only. There are no working-hours entries for those days.
Input: ("2024-01-01 08:00", "2024-01-01 18:00", [("Monday", "09:00", "09:20")])
Expected Output: []
Explanation: The only working interval is shorter than 30 minutes, so it cannot produce a valid slot.
Input: ("2024-01-01 09:30", "2024-01-01 10:30", [("Monday", "09:00", "10:30")])
Expected Output: [["2024-01-01 09:30", "2024-01-01 10:00"], ["2024-01-01 10:00", "2024-01-01 10:30"]]
Explanation: A slot may start exactly at start_datetime and may end exactly at end_datetime. The 09:00-09:30 slot is excluded because it starts before the requested range.
Hints
- Iterate one calendar day at a time, find that day's weekday name, and look up matching working-hours intervals.
- For each working interval, generate 30-minute candidates and keep only those whose start and end are inside the requested range.