Katkat

Autoscale a Server Fleet from Throughput Readings

Two SigmaSoftware Engineer · Technical Screen

Last updated

This question evaluates algorithmic implementation skills and numeric reasoning, focusing on stateful simulation of autoscaling behavior and capacity arithmetic under streaming throughput readings.

You run a service on a fleet of identical servers behind a load balancer. Each server can handle at most limit units of throughput. A monitoring system samples the service's total throughput at regular intervals, producing a chronological array readings, where readings[i] is the total observed throughput during the i-th interval.

The fleet starts with exactly 1 server. Process the readings in order. After observing each reading:

  • If the reading is less than or equal to the fleet's current total capacity (number of servers * limit), do nothing.
  • Otherwise, open the minimum number of new servers needed so that the total capacity becomes greater than or equal to the reading. New servers are available immediately.

Servers are never shut down once opened, so the fleet size never decreases.

Return an array fleet of the same length as readings, where fleet[i] is the number of servers in the fleet after processing readings[i].

Example

Input: limit = 10, readings = [5, 25, 18, 40, 32]

StepReadingCapacity beforeActionServers after
051 × 10 = 10none (5 ≤ 10)1
1251 × 10 = 10open 2 servers (capacity 30 ≥ 25)3
2183 × 10 = 30none (18 ≤ 30)3
3403 × 10 = 30open 1 server (capacity 40 ≥ 40)4
4324 × 10 = 40none (32 ≤ 40)4

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

Constraints

  • 1 <= readings.length <= 10^5
  • 1 <= limit <= 10^9
  • 0 <= readings[i] <= 10^13 (values may exceed 32-bit integer range)
  • The answer array's values fit in a 64-bit integer.