Design a container orchestrator with first-fit and best-fit placement
Company: Microsoft
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design an in-memory container orchestrator that manages a fleet of cloud server machines. Each machine has a fixed CPU and RAM capacity. The orchestrator deploys containers onto machines based on resource availability.
The orchestrator supports swappable placement strategies. In first-fit mode, a container is assigned to the first machine (by ascending machine ID) with sufficient resources. In best-fit mode, the container is assigned to the machine that would have the least remaining total resources (CPU + RAM combined) after placement, with ties broken by the smallest machine ID.
Containers can be removed to free resources. Machines can also be removed, which orphans and removes all containers running on them. You can query any machine's available resources or list its containers at any time.
Implement the following methods:
- `addMachine(machineId: int, cpu: int, ram: int)`: register a new machine with the given CPU and RAM capacity.
- `removeMachine(machineId: int) -> List[int]`: remove a machine and all its containers. Returns a sorted list of the container IDs that were running on it.
- `setStrategy(strategy: str)`: set the placement strategy to `'first-fit'` or `'best-fit'`.
- `deployContainer(containerId: int, cpu: int, ram: int) -> int`: deploy a container with the given CPU and RAM requirements. Returns the machine ID it was placed on, or -1 if no machine can fit it.
- `removeContainer(containerId: int) -> bool`: remove a container and free its resources. Returns `True` if it was found and removed, `False` otherwise.
- `getMachineStatus(machineId: int) -> List[int]`: return `[available_cpu, available_ram]` for the given machine, or `[-1, -1]` if it is not found.
- `getContainers(machineId: int) -> List[int]`: return a sorted list of the container IDs running on the given machine.
For example:
```text
addMachine(1, 8, 16)
addMachine(2, 4, 8)
addMachine(3, 16, 32)
deployContainer(101, 4, 8) -> 1
setStrategy("best-fit")
deployContainer(102, 2, 4) -> 1
deployContainer(103, 3, 3) -> 2
getMachineStatus(2) -> [1, 5]
deployContainer(104, 10, 30) -> 3
deployContainer(105, 6, 6) -> -1
getContainers(1) -> [101, 102]
removeContainer(101) -> True
removeContainer(101) -> False
removeMachine(3) -> [104]
getMachineStatus(3) -> [-1, -1]
removeContainer(104) -> False
setStrategy("first-fit")
deployContainer(106, 1, 5) -> 1
```
Container 101 goes to machine 1 under the default first-fit mode. For 102, machines 1 and 2 would both have 6 units left, so best-fit takes the smaller ID. Container 105 fails because machine 3 has 6 CPU free but only 2 RAM. Removing machine 3 also removes 104, so the later `removeContainer(104)` returns `False`. The last deploy lands on machine 1 under first-fit, while best-fit would have chosen machine 2, which it fills exactly.
```hint Bookkeeping
Each removal has to reach the other side quickly: a container must find its host, and a machine must find its containers. Decide what each record stores so neither removal scans the whole fleet.
```
```hint Tie-breaks
If you scan candidate machines in ascending ID order, ask whether the comparison should be strict when two machines leave the same remainder.
```
### Constraints & Assumptions
- `1 <= machineId, containerId <= 10^5`
- `1 <= cpu, ram <= 10^4`
- Machine IDs are unique across all `addMachine` calls, and container IDs are unique across all `deployContainer` calls.
- `setStrategy` is called with either `'first-fit'` or `'best-fit'`.
- At most `10^4` total operations.
- The orchestrator starts in first-fit mode.
- For an unknown machine ID, `removeMachine` and `getContainers` return an empty list.
### Clarifying Questions to Ask
- Which strategy is active before the first `setStrategy` call?
- What should `removeMachine` and `getContainers` return for a machine ID that does not exist?
- Does best-fit add CPU and RAM as raw numbers, even though they are measured in different units?
- Can a machine's capacity or a container's requirements change after creation?
### What a Strong Answer Covers
- A data model linked both ways: each machine tracks its free CPU and RAM and the containers it hosts, and each container records its host and footprint.
- Placement rules behind a common interface that `setStrategy` swaps, with exact first-fit and best-fit semantics, including the smallest-ID tie-break and the check that both resources fit.
- Removal paths that keep both sides consistent: freeing resources when a container goes, and dropping a removed machine's containers from the global record so later calls on them return `False`.
- Sorted outputs and the not-found return values.
- Complexity per method, and where the scan in `deployContainer` becomes the bottleneck.
### Follow-up Questions
- How would you add a third strategy, such as worst-fit, without editing `deployContainer`?
- CPU and RAM come in different units. How would you make the best-fit score meaningful?
- With 100,000 machines and frequent deploys, a linear scan per deploy is too slow. What index would speed up first-fit or best-fit?
- How would you change `removeMachine` to reschedule its containers onto the remaining machines, and in what order would you place them?
Overview: Design an in-memory container orchestrator that places containers on machines by CPU and RAM using swappable first-fit and best-fit strategies. It tests object modeling, the strategy pattern, tie-breaking rules, and keeping machine and container records consistent when either side is removed.