Design a Train Platform Management System with Best-Fit Platform Assignment
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a train platform management system that assigns trains to the platforms of a railway station. Each platform has a maximum capacity, which is the longest train it can hold. When a train is scheduled, the system picks the most suitable free platform for it.
Assignment uses a best-fit rule: among the platforms that are free for the train's whole stay and long enough for it, choose the one with the smallest capacity. If several of those platforms share that capacity, choose the one with the smallest platform ID. A train occupies its platform during the half-open interval `[arrival_time, departure_time)`, from its arrival up to but not including its departure.
The system also removes trains, reports which trains are at a platform at a given time, finds the platform a train is assigned to, and returns a platform's full schedule.
Implement a class `TrainPlatformManager` with these methods:
- `add_platform(platform_id: int, capacity: int) -> bool` adds a platform with the given ID and capacity. It returns `True` on success and `False` if the platform ID already exists.
- `assign_train(train_id: int, train_length: int, arrival_time: int, departure_time: int) -> int` assigns the train to the best-fit platform that has no train overlapping `[arrival_time, departure_time)`. It returns the platform ID, or `-1` if no platform can take the train or the train is already assigned.
- `remove_train(train_id: int) -> bool` removes the train from its platform. It returns `True` if the train was found and removed, `False` otherwise.
- `trains_at_platform(platform_id: int, time: int) -> List[int]` returns the sorted IDs of the trains present at the platform at `time`. A train is present when `arrival_time <= time < departure_time`. An unknown platform gives an empty list.
- `find_train(train_id: int) -> int` returns the ID of the platform the train is assigned to, or `-1` if the train is not found.
- `platform_schedule(platform_id: int) -> List[List[int]]` returns every assignment on the platform as `[train_id, arrival_time, departure_time]`, sorted by arrival time and then by train ID. It returns an empty list if the platform does not exist.
Example 1 shows the best-fit rule, the tie-break on platform ID, and a train that arrives at the moment another one departs.
```text
Input:
["TrainPlatformManager", "add_platform", "add_platform", "add_platform", "add_platform", "assign_train", "assign_train", "assign_train", "assign_train", "assign_train", "trains_at_platform", "platform_schedule", "find_train"]
[[], [1, 100], [2, 60], [3, 60], [2, 80], [101, 50, 10, 20], [102, 55, 15, 25], [103, 40, 12, 18], [104, 120, 0, 5], [105, 30, 20, 30], [2, 20], [2], [104]]
Output: [null, true, true, true, false, 2, 3, 1, -1, 2, [105], [[101, 10, 20], [105, 20, 30]], -1]
Explanation:
mgr = TrainPlatformManager()
mgr.add_platform(1, 100) # True
mgr.add_platform(2, 60) # True
mgr.add_platform(3, 60) # True
mgr.add_platform(2, 80) # False (platform 2 already exists)
mgr.assign_train(101, 50, 10, 20) # 2 (platforms 2 and 3 tie at capacity 60, and 2 has the smaller ID)
mgr.assign_train(102, 55, 15, 25) # 3 (platform 2 is busy during [10, 20))
mgr.assign_train(103, 40, 12, 18) # 1 (platforms 2 and 3 are both busy, so the next fit is capacity 100)
mgr.assign_train(104, 120, 0, 5) # -1 (no platform can hold a train of length 120)
mgr.assign_train(105, 30, 20, 30) # 2 (train 101 leaves platform 2 at time 20, so [20, 30) is free)
mgr.trains_at_platform(2, 20) # [105] (train 101 is no longer present at time 20)
mgr.platform_schedule(2) # [[101, 10, 20], [105, 20, 30]]
mgr.find_train(104) # -1 (train 104 was never assigned)
```
Example 2 shows removal freeing a platform for a later train.
```text
Input:
["TrainPlatformManager", "add_platform", "assign_train", "assign_train", "remove_train", "assign_train", "find_train", "trains_at_platform"]
[[], [1, 50], [10, 40, 0, 100], [20, 40, 50, 150], [10], [30, 40, 0, 80], [30], [1, 60]]
Output: [null, true, 1, -1, true, 1, 1, [30]]
Explanation:
mgr = TrainPlatformManager()
mgr.add_platform(1, 50) # True (platform 1 with capacity 50)
mgr.assign_train(10, 40, 0, 100) # 1 (fits on platform 1)
mgr.assign_train(20, 40, 50, 150) # -1 (platform 1 is occupied during [0, 100), which overlaps [50, 150))
mgr.remove_train(10) # True (train 10 removed from platform 1)
mgr.assign_train(30, 40, 0, 80) # 1 (platform 1 is free again)
mgr.find_train(30) # 1 (train 30 is on platform 1)
mgr.trains_at_platform(1, 60) # [30] (train 30 is present at time 60)
```
```hint Overlap at the boundaries
Write the conflict test for two half-open intervals, then try it on a train that leaves at time 20 and another that arrives at time 20. The definition of occupancy says those two may share a platform.
```
```hint Scan order
Look for an order of visiting platforms in which the first free platform you reach is already the best fit, tie-break included.
```
### Constraints & Assumptions
- `1 <= platform_id, train_id <= 10000`
- `1 <= capacity, train_length <= 1000`
- `0 <= arrival_time < departure_time <= 100000`
- Each platform ID names one platform and each train ID names one train. Repeated IDs are handled by the return values of `add_platform` and `assign_train`.
- At most 1000 calls are made in total.
### Clarifying Questions to Ask
- After a train is removed, can the same train ID be assigned again, possibly with a different time window?
- Can platforms be removed or have their capacity changed, or does the set of platforms only grow?
- Should a request that finds no free platform be queued for later, or is returning -1 the whole behavior?
- Beyond the stated limits, how many platforms and trains per platform should the design handle, and does assignment need to be faster than a linear scan?
### What a Strong Answer Covers
- A data model that stores each platform's capacity and assigned intervals plus a train-to-platform index, and keeps the two consistent on assignment and removal.
- A correct overlap test for half-open intervals, including trains that depart and arrive at the same instant.
- Best-fit selection that moves past busy platforms, applies the platform ID tie-break deterministically, and returns -1 without changing state when no platform fits or the train is already assigned.
- Query methods that return results in the stated order and handle unknown platforms and trains.
- The time and space cost of each operation, and which structure to change if the limits grow.
### Follow-up Questions
- With thousands of platforms and long schedules, how would you make `assign_train` faster than scanning every interval on every candidate platform?
- A train is delayed and its departure moves later. How would you update its assignment, keeping it on the same platform when that platform is still free?
- When no platform is free for the requested window, how would you report the earliest arrival time at which some platform could take the train?
- How would you keep assignments correct when many requests call `assign_train` at the same time?
Overview: Design a class that assigns trains to station platforms by best fit on capacity, breaks ties by platform ID, and treats each stay as a half-open time interval. It tests object modeling, interval overlap checks, deterministic tie-breaking and keeping several indexes consistent when trains are removed.