Simulate round-robin package assignment to servers
Company: Capital One
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
You are simulating a round-robin dispatcher across **m servers** labeled `0..m-1`.
- Each server `i` has an initial remaining capacity `cap[i]` (a non-negative integer).
- Some servers are **shut down** and must be skipped; this is given as a boolean array `down[i]`.
- A sequence of **n packages** arrives in order; package `k` has size `pkg[k]` (a positive integer).
Dispatch rule (round-robin with skipping and wrap-around):
1. Maintain a pointer `p` that indicates where scanning starts. Initially `p = 0`.
2. For each arriving package `k`, starting from server `p`, scan servers `p, p+1, ..., m-1, 0, 1, ...` (wrapping around) to find the **first** server `j` such that:
- `down[j] == false`, and
- `cap[j] >= pkg[k]`.
3. If such a server `j` exists, assign the package to `j`, decrement `cap[j] -= pkg[k]`, and increment `handled[j]` by 1. Then set `p = (j + 1) mod m` for the next package.
4. If **no** server can take the package, the package is dropped and `p` does not change.
Return the **highest-index** server that handled the **maximum** number of packages. If all servers handled 0 packages, return `-1`.
**Input:** `m`, arrays `cap[0..m-1]`, `down[0..m-1]`, and `pkg[0..n-1]`.
**Output:** an integer server index.
Include typical edge cases in your reasoning (wrap-around behavior, all servers down, large package sizes, ties).
Quick Answer: This question evaluates array-based simulation and control-flow skills for load distribution, including pointer management, modular wrap-around scanning, capacity checks, and handling disabled servers.