Problemworkload scheduler

Session: Sign in to solve

Solution.txt

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. place may 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, place returns 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 false and changes no state.
  • Inspect finished work. isFinished returns false while a placed workload is active and true after it completes.

API

SignatureReturnsBehavior
WorkloadScheduler(machineIds: string[], cpuCapacities: integer[], memoryCapacitiesMb: integer[])Not applicableCreates one empty machine from each aligned array position.
place(jobId: string, cpuUnits: integer, memoryMb: integer)stringAttempts to place a new workload and returns its machine ID or "".
complete(jobId: string)booleanAttempts to finish the named workload.
isFinished(jobId: string)booleanReports whether a successfully placed workload has finished.

Examples

With WorkloadScheduler(["m1", "m2"], [4, 8], [8000, 4000]):

StepOperationResult
1place("job-a", 4, 6000)"m1"
2isFinished("job-a")false
3place("job-b", 1, 3000)"m2"
4place("job-c", 1, 3000)""
5complete("job-a")true
6isFinished("job-a")true
7place("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 1 through 1,000.
  • Machine IDs are unique non-empty strings of at most 64 characters.
  • Every CPU or memory capacity is from 1 through 1,000,000,000,000.
  • Job IDs are unique non-empty strings of at most 64 characters.
  • cpuUnits and memoryMb are each from 1 through 1,000,000,000,000.
  • isFinished is called only for a workload whose place call succeeded.
  • At most 10,000 public 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.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.