Design a truck tracking system with driver assignment and trip history
Company: Uber
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a truck tracking system that manages a fleet of trucks, their drivers and trip records. The system should support registering trucks and drivers, assigning and unassigning drivers to trucks, and tracking trips with start and end locations.
Each truck can have at most one assigned driver and at most one active trip at a time. Each driver can be assigned to at most one truck at a time. When a trip ends, the driver's status is automatically set to `'available'`.
The system should support real-time driver status updates, querying trip history per truck in chronological order, and retrieving detailed trip information.
Implement the following methods:
- `registerTruck(truckId: str) -> bool` registers a new truck. Returns `True` if successful, `False` if the truck ID is already registered.
- `registerDriver(driverId: str) -> bool` registers a new driver with initial status `'available'`. Returns `True` if successful, `False` if the driver ID is already registered.
- `assignDriver(truckId: str, driverId: str) -> bool` assigns a driver to a truck. Both must be registered, the driver must not already be assigned to another truck, and the truck must not already have a driver. Returns `True` if successful.
- `unassignDriver(truckId: str) -> bool` removes the current driver from a truck. Fails if the truck has no driver or has an active trip. Returns `True` if successful.
- `startTrip(tripId: str, truckId: str, startLocation: str) -> bool` starts a new trip for a truck. The truck must have an assigned driver and no active trip, and the trip ID must be unique. The driver's status is set to `'on_trip'`. Returns `True` if successful.
- `endTrip(tripId: str, endLocation: str) -> bool` ends an active trip by recording the end location. The trip must exist and be in progress. The driver's status is automatically set to `'available'`. Returns `True` if successful.
- `updateDriverStatus(driverId: str, status: str) -> bool` sets a driver's status to the given string. Returns `True` if the driver exists, `False` otherwise.
- `getDriverStatus(driverId: str) -> str` returns the current status of a driver, or an empty string if the driver is not found.
- `getTripsByTruck(truckId: str) -> List[str]` returns all trip IDs for the given truck in chronological order. Returns an empty list if the truck is not found or has no trips.
- `getTripInfo(tripId: str) -> str` returns the trip details as the string `'tripId:truckId:driverId:startLocation:endLocation:status'`. The end location is `'None'` while the trip is in progress, and the status is `'in_progress'` until the trip ends and `'completed'` after. Returns an empty string if the trip is not found.
- `getTruckDriver(truckId: str) -> str` returns the ID of the driver currently assigned to the truck, or an empty string if the truck is not found or has no assigned driver.
A method that returns `False` has found a broken condition and changes nothing.
Example 1, starting from an empty system:
```text
registerTruck("T1") -> True
registerDriver("D1") -> True
assignDriver("T1", "D1") -> True
startTrip("trip1", "T1", "Oakland") -> True
getDriverStatus("D1") -> "on_trip"
getTripInfo("trip1") -> "trip1:T1:D1:Oakland:None:in_progress"
endTrip("trip1", "Fresno") -> True
getTripInfo("trip1") -> "trip1:T1:D1:Oakland:Fresno:completed"
getDriverStatus("D1") -> "available"
getTruckDriver("T1") -> "D1"
updateDriverStatus("D1", "off_duty") -> True
getDriverStatus("D1") -> "off_duty"
```
Starting the trip sets D1 to `on_trip`, and the trip has no end location yet, so its info shows `None` and `in_progress`. Ending it records Fresno, marks the trip completed and sets D1 back to `available`. D1 stays assigned to T1 after the trip, and a status update accepts any string.
Example 2, starting from an empty system:
```text
registerTruck("T1") -> True
registerTruck("T1") -> False
registerDriver("D1") -> True
registerDriver("D2") -> True
startTrip("trip1", "T1", "Reno") -> False
assignDriver("T1", "D1") -> True
assignDriver("T1", "D2") -> False
startTrip("trip1", "T1", "Reno") -> True
unassignDriver("T1") -> False
endTrip("trip1", "Boise") -> True
endTrip("trip1", "Boise") -> False
startTrip("trip1", "T1", "Boise") -> False
startTrip("trip2", "T1", "Boise") -> True
getTripsByTruck("T1") -> ["trip1", "trip2"]
getTruckDriver("T1") -> "D1"
```
T1 cannot be registered twice. The first `startTrip` fails because T1 has no driver yet, and because it failed, the ID trip1 is still free for the later call. T1 cannot take a second driver, and D1 cannot be unassigned while trip1 is active. A trip can be ended only once, and trip1 cannot be reused as an ID after it ends. The history lists the trips in the order they started.
```hint Answering "is this driver taken?"
`assignDriver` must reject a driver who already drives another truck. Think about what to store so that this check does not require looking through every truck.
```
```hint Failed calls
Check every precondition before you write anything. Watch for a call that records part of its work (a new trip ID, a status change) and then returns `False`.
```
### Constraints & Assumptions
- `1 <= truckId.length, driverId.length, tripId.length <= 50`
- All IDs consist of alphanumeric characters and underscores.
- A truck or driver must be registered before any operation involving it.
- Trip IDs are globally unique.
- At most `10^4` operations in total.
### Clarifying Questions to Ask
- Should `startTrip` refuse when the assigned driver's status is something other than `'available'`, such as `'off_duty'`?
- May `updateDriverStatus` overwrite `'on_trip'` while the driver's trip is still active, and is any string allowed?
- Can a location contain a colon, which would make the `getTripInfo` string ambiguous to parse?
- Does chronological order mean the order in which trips were started, or do trips carry timestamps?
- Will calls arrive from many dispatchers at once, or is the system single-threaded?
### What a Strong Answer Covers
- The entities and the state each one holds, including the links between a truck, its driver and its active trip.
- How each operation checks its preconditions before changing anything, so a failed call leaves no partial update.
- How the rules (one driver per truck, one truck per driver, one active trip per truck, no unassigning during a trip) stay true after every call.
- The exact return values for unknown IDs and the `getTripInfo` format, including `None` and the two trip statuses.
- Time and space complexity of the operations.
### Follow-up Questions
- How would you record a trip's route if trucks report GPS positions every few seconds?
- How would you make the system safe when many dispatchers call it at the same time?
- How would you add a query for all trips by a driver, or all trips that started in a time range?
- How would you persist this state in a relational database so the rules still hold, and which tables and constraints would you create?
Overview: Implement an in-memory truck tracking system that registers trucks and drivers, assigns drivers to trucks, and starts and ends trips with a per-truck history. It tests object modeling, enforcing one-to-one assignment rules, precondition checks that leave no partial state, and exact return formats.