Designing a Delivery Slot Booking System
Difficulty: Intermediate Patterns: Template Method, Observer, Factory Asked at: Licious, BigBasket, Blinkit, Zepto
Functional Requirements
- Create time slots - define delivery windows with start time, end time, and max capacity
- Book slots with capacity - customers book a slot; booking succeeds only if capacity available
- Cancel bookings - customers can cancel; freed capacity becomes available again
- Check availability - query available slots for a given date/time range
- Conflict detection - prevent double-booking by same customer in overlapping slots
- Waitlist - when a slot is full, customers can join a waitlist and get auto-promoted on cancellation
Non-Functional Requirements
- Thread-safety - concurrent bookings must not exceed slot capacity
💡 Thread-safe = correct even when several requests execute the same code at once. Two customers taking the last spot in the same millisecond is exactly the failure being prevented. Learn more → - Fairness - waitlist is FIFO; first to join gets promoted first
💡 FIFO = first in, first out: the queue returns people in the order they joined, so nobody who waited longer gets jumped. - Extensibility - new slot types (express, scheduled, same-day) without changing booking logic
💡 This is the open-closed principle, the O in SOLID: add a new slot type by adding code, not by editing the booking flow that already works. Learn more →
Core Entities
| Entity | Description |
|---|---|
TimeSlot |
Delivery window: start, end, capacity, current bookings |
SlotType |
Enum: STANDARD, EXPRESS, SAME_DAY |
Booking |
Links a customer to a slot with status (CONFIRMED, WAITLISTED, CANCELLED) |
BookingStatus |
Enum: CONFIRMED, WAITLISTED, CANCELLED, PROMOTED |
Customer |
ID, name, address |
WaitlistEntry |
Customer + timestamp in waitlist queue |
SlotManager |
Creates and manages all time slots |
BookingService |
Orchestrates booking, cancellation, waitlist promotion |
BookingObserver |
Notified on booking events (confirmed, cancelled, promoted) |
SlotFactory |
Creates different types of slots with appropriate defaults |
💡 SlotType and BookingStatus are enums (enumerations) — types whose values are a fixed named set. A booking can therefore only ever hold one of the four statuses listed; an invalid status is not representable.
💡 “Orchestrates” means BookingService holds no data of its own worth keeping — it coordinates the slots, bookings and observers in the right order. Putting that sequencing in one class is what keeps TimeSlot and Booking simple.
Class Diagram
classDiagram
class SlotType {
<<enumeration>>
STANDARD
EXPRESS
SAME_DAY
}
class BookingStatus {
<<enumeration>>
CONFIRMED
WAITLISTED
CANCELLED
PROMOTED
}
class TimeSlot {
-String id
-LocalDateTime startTime
-LocalDateTime endTime
-int maxCapacity
-int currentBookings
-SlotType type
-Queue~WaitlistEntry~ waitlist
+isAvailable() boolean
+book() boolean
+release()
+addToWaitlist(Customer)
+promoteFromWaitlist() Customer
}
class Customer {
-String id
-String name
-String address
}
class Booking {
-String id
-Customer customer
-TimeSlot slot
-BookingStatus status
-LocalDateTime bookedAt
+confirm()
+cancel()
+waitlist()
}
class WaitlistEntry {
-Customer customer
-long timestamp
}
class SlotFactory {
+createSlot(SlotType, LocalDateTime start, LocalDateTime end) TimeSlot
}
class BookingObserver {
<<interface>>
+onBookingConfirmed(Booking)
+onBookingCancelled(Booking)
+onCustomerPromoted(Booking)
+onWaitlisted(Booking)
}
class BookingService {
-Map~String, TimeSlot~ slots
-Map~String, Booking~ bookings
-Map~String, List~Booking~~ customerBookings
-List~BookingObserver~ observers
+bookSlot(Customer, String slotId) Booking
+cancelBooking(String bookingId)
+getAvailableSlots(LocalDateTime from, LocalDateTime to) List
+getWaitlistPosition(String bookingId) int
}
BookingService --> TimeSlot
BookingService --> Booking
BookingService --> BookingObserver
Booking --> Customer
Booking --> TimeSlot
Booking --> BookingStatus
TimeSlot --> SlotType
TimeSlot --> WaitlistEntry
SlotFactory --> TimeSlot
💡 Reading the arrows: --> means “holds a reference to”, so BookingService --> BookingObserver is the list of listeners it will call. <<interface>> marks a contract with no implementation of its own, and <<enumeration>> marks a fixed set of named values. Learn more →
Key classes:
TimeSlot— one delivery window and the single source of truth for its own capacity. It ownsmaxCapacity,currentBookingsand its waitlist queue, andbook()/release()are the only ways that count moves.Booking— the link between a customer and a slot, plus its status. It exists so that “waitlisted” and “confirmed” are the same object in different states rather than two separate records.BookingService— the coordinator. It keeps the slot and booking lookups, the per-customer booking lists used for conflict detection, and the observer list; every customer-facing action enters here.WaitlistEntry— a customer plus the timestamp they joined. The timestamp is what makes first-in-first-out promotion possible.SlotFactory— the one place slots are constructed, so each slot type gets its own capacity defaults without callers remembering them.BookingObserver— the notification contract (onBookingConfirmed,onBookingCancelled,onCustomerPromoted,onWaitlisted). SMS, email and push each become one implementation, and the booking code calls none of them by name.Customer— id, name, address. Deliberately thin; it carries no booking state so there’s nothing to keep in sync.
Design Patterns
| Pattern | Where | Why |
|---|---|---|
| Template Method | Booking flow: validate → reserve → notify (subclass can override validation) | Common booking structure with customizable steps per slot type |
| Observer | BookingObserver notified on confirm/cancel/promote |
Decouple notifications (SMS, email, push) from booking logic |
| Factory | SlotFactory creates STANDARD/EXPRESS/SAME_DAY slots |
Encapsulate creation logic; each type has different capacity defaults |
💡 Template Method pattern = a base class fixes the order of the steps and lets subclasses override individual steps. The validate → reserve → notify sequence is the template; only validation differs per slot type. Learn more →
💡 Observer pattern = listeners register themselves and get called back when something happens, so the booking code never imports an SMS client. Adding email is one new class and one register() call. Learn more →
💡 Factory pattern = move object construction into one method so callers ask for “an express slot” instead of remembering its capacity and defaults. Learn more →
Data Structures
| Component | Structure | Why |
|---|---|---|
| Slots | ConcurrentHashMap<String, TimeSlot> |
O(1) slot lookup by ID |
| Bookings | ConcurrentHashMap<String, Booking> |
O(1) booking lookup |
| Customer bookings | Map<String, List<Booking>> |
Track all bookings per customer for conflict detection |
| Waitlist per slot | LinkedList<WaitlistEntry> (FIFO queue) |
Fair ordering; O(1) add/remove from ends |
| Available slots | Filtered from all slots by time range | Linear scan acceptable for bounded slot count |
💡 ConcurrentHashMap is a map built for many threads at once: readers never block, and writes lock only the affected bucket rather than the whole map. A plain HashMap can corrupt its internal structure under concurrent writes.
Discussion
Newest first