Problem Statement
Design and implement a premium cab hailing service (like Uber Black). Focus on the cab allocation logic with clean, running code that handles concurrent ride requests.
Requirements
- Register drivers with their location and cab type (premium, standard)
- Request a ride — rider specifies pickup location, destination, cab type
- Allocate the nearest available cab matching the requested type
- Handle concurrent requests — multiple riders requesting simultaneously should not be assigned the same driver
- Trip lifecycle — requested → assigned → in-progress → completed
- Update driver availability after trip completion
Constraints
- Must be a fully running solution (not pseudocode)
- Thread-safe cab allocation (no double-assignment)
- Extensible for new cab types
- Master one language for this round (syntax matters)
What the Interviewer Expects
- Core entities:
Driver,Rider,Trip,Location,CabType(enum) - Allocation strategy — nearest available driver. Use a strategy pattern so allocation logic can be swapped (nearest, highest-rated, etc.)
- Concurrency handling:
- Lock on driver assignment to prevent double-booking
- Use synchronized blocks or concurrent data structures
- Atomic state transition (available → assigned)
- Design patterns:
- Strategy — for allocation algorithm
- State — for trip lifecycle
- Observer — for notifying rider/driver of status changes
- Clean separation —
TripManager,DriverManager,AllocationStrategyas separate classes.
Follow-ups
- How do you handle surge pricing based on demand?
- What if a driver rejects a ride? How do you reallocate quickly?
- How would you scale allocation to a city with 100K drivers?
- How do you handle the case where two riders are equidistant from the same driver?
- How would you add ride-pooling (multiple riders, one cab)?