Katkat

Generate Available 30-Minute Time Slots

StripeSoftware Engineer · Technical Screen

Last updated

This problem tests your ability to manipulate time intervals, handle recurring weekly schedules, and perform intersection logic between availability windows and existing booked appointments.

Problem Statement

Given a recurring weekly working schedule, a list of existing booked appointments, and a requested date range, generate all available 30-minute time slots.

Definitions

  • Working Schedule: A dictionary where keys are days of the week (0=Monday, 6=Sunday) and values are lists of [start_time, end_time] strings in HH:MM format (e.g., {"0": [["09:00", "17:00"]]}).
  • Booked Appointments: A list of datetime strings in ISO format: YYYY-MM-DDTHH:MM:SS representing the start and end of each booking.
  • Requested Range: A [start_date, end_date] pair as ISO date strings.

Function Signature

def get_available_slots(working_schedule, booked_appointments, start_date, end_date):
    # Return a list of tuples: (slot_start_iso, slot_end_iso)

Constraints

  • Time slots must be exactly 30 minutes long.
  • Time slots must be aligned to the hour (e.g., :00 or :30).
  • start_date and end_date are inclusive.
  • If a booking overlaps with a working hour, that time is unavailable.

Example

  • Schedule: {"0": [["09:00", "10:00"]]} (Mondays 9-10 AM only).
  • Bookings: [["2023-10-02T09:30:00", "2023-10-02T10:00:00"]].
  • Range: ["2023-10-02", "2023-10-02"].
  • Output: [["2023-10-02T09:00:00", "2023-10-02T09:30:00"]].

Edge Cases

  • Range spans multiple weeks.
  • Working hours end at midnight.
  • Bookings that span across the boundary of working hours.

Hints

Hint 1

Convert the weekly working hours into a list of absolute datetime intervals for every day within the requested start and end range.

Hint 2

Normalize all bookings into a unified structure that allows you to subtract them from your absolute availability intervals.

Hint 3

Generate all potential 30-minute segments within the remaining free blocks, ensuring each segment starts at either :00 or :30.

Follow-up questions

  • How would you modify your solution to support variable slot durations (e.g., 15, 30, or 60 minutes) requested by the user?
  • What changes are required if the business introduces time zones for both the user and the office?
  • How would you optimize the look-up process if the number of booked appointments grows into the millions over several years?