KatInterview

Growing Worker Pool Sized by Peak Load Readings

Math & Bit ManipulationAuto-graded · 18 test cases
MediumAuto-gradedMath & Bit Manipulationsimulationprefix-maximumceiling-division

Last updated

Track a pool that only grows, sized to cover each load reading with fixed-capacity workers; tests ceiling division and running-maximum reasoning with large values.

Growing Worker Pool Sized by Peak Load Readings

A job-processing service runs on a pool of identical workers. Each worker can absorb at most capacity_per_worker units of load. Load is measured at regular intervals, giving a chronological list loads, where loads[i] is the total load recorded in the i-th interval.

The pool begins with exactly 1 worker. Go through the loads in order. After looking at each load value:

  • If the value is at most the pool's total capacity (workers * capacity_per_worker), leave the pool as is.
  • Otherwise, add the smallest number of workers that makes the total capacity at least as large as the value. Added workers are usable immediately.

Workers are never removed, so the pool size never shrinks.

Return a list sizes with the same length as loads, where sizes[i] is the number of workers in the pool after handling loads[i].

Constraints

  • 1 <= len(loads) <= 10^5
  • 1 <= capacity_per_worker <= 10^9
  • 0 <= loads[i] <= 10^13 (values can exceed the 32-bit integer range)
  • Every value in the returned list fits in a 64-bit integer.

Examples

Example 1

Input: scale_worker_pool(10, [5, 25, 18, 40, 32])

Output: [1, 3, 3, 4, 4]

Example 2

Input: scale_worker_pool(5, [0, 1, 5, 6, 10, 11])

Output: [1, 1, 1, 2, 2, 3]

Example 3

Input: scale_worker_pool(10, [100, 1, 99, 101])

Output: [10, 10, 10, 11]

Hints

Hint 1

For a single load value on its own, how many workers are needed at minimum? Express it as a division that rounds up.

Hint 2

Adding workers one at a time can take far too long when the pool needs to grow to billions of workers. Jump directly to the required size instead.

Hint 3

Because the pool never shrinks and starts at 1, each answer is a running maximum of the per-load requirements (and 1). Use integer arithmetic, (x + c - 1) // c, for the rounding up.