Workload Scheduler
Problem
A small compute cluster has fixed machines with different CPU and memory capacities. Design WorkloadScheduler to place each workload on one machine that can hold it and to reserve those resources until the workload finishes.
Requirements
- Place fitting work.
placemay choose any one machine with enough free CPU and memory; on success it returns that machine's ID and reserves both requested amounts. - Reject unavailable capacity. If no single machine fits both amounts,
placereturns the empty string and changes no capacity or workload state. - Release completed work. Completing an active workload marks it finished, releases its exact CPU and memory reservation, and returns
true. - Ignore inactive completion. Completing an unknown or already finished workload returns
falseand changes no state. - Inspect finished work.
isFinishedreturnsfalsewhile a placed workload is active andtrueafter it completes.
API
| Signature | Returns | Behavior |
|---|---|---|
WorkloadScheduler(machineIds: string[], cpuCapacities: integer[], memoryCapacitiesMb: integer[]) | Not applicable | Creates one empty machine from each aligned array position. |
place(jobId: string, cpuUnits: integer, memoryMb: integer) | string | Attempts to place a new workload and returns its machine ID or "". |
complete(jobId: string) | boolean | Attempts to finish the named workload. |
isFinished(jobId: string) | boolean | Reports whether a successfully placed workload has finished. |
Examples
With WorkloadScheduler(["m1", "m2"], [4, 8], [8000, 4000]):
| Step | Operation | Result |
|---|---|---|
| 1 | place("job-a", 4, 6000) | "m1" |
| 2 | isFinished("job-a") | false |
| 3 | place("job-b", 1, 3000) | "m2" |
| 4 | place("job-c", 1, 3000) | "" |
| 5 | complete("job-a") | true |
| 6 | isFinished("job-a") | true |
| 7 | place("job-c", 1, 3000) | "m1" |
When two machines fit, either machine ID is a valid place result.
Constraints
- The three constructor arrays have the same length from
1through1,000. - Machine IDs are unique non-empty strings of at most 64 characters.
- Every CPU or memory capacity is from
1through1,000,000,000,000. - Job IDs are unique non-empty strings of at most 64 characters.
cpuUnitsandmemoryMbare each from1through1,000,000,000,000.isFinishedis called only for a workload whoseplacecall succeeded.- At most
10,000public method calls occur in one testcase. - Inputs satisfy these constraints; behavior outside them is not judged.
Notes
Calls are sequential. Several active workloads may share a machine while both resource totals remain within capacity. No placement priority or tie order is promised.