894. Autoscale replicas for a day of traffic
Why it matters Autoscaling decides most of an inference bill. Target utilisation below 100 percent buys headroom for bursts; a max replica cap protects the budget but can overload.
Plan model-server replicas hour by hour. For each hour with request rate rps:
- replicas = ceil(rps / (rps_per_replica * target_util)), then clamp to [min_replicas, max_replicas]
- the hour is overloaded if rps > replicas * rps_per_replica (more traffic than the replicas can serve even at 100 percent)
- the hour costs replicas * hourly_usd
Write replica_plan(hourly_rps, rps_per_replica, target_util, min_replicas, max_replicas, hourly_usd).
Inputs: hourly_rps is a list of non-negative numbers (can be empty). rps_per_replica > 0, target_util in (0, 1], min_replicas <= max_replicas are ints >= 0, hourly_usd >= 0. Use math.ceil on the ratio after subtracting 1e-9, so exact multiples are not rounded up by float error.
Return a dict {"replicas": list of ints, "cost": total cost rounded to 2 decimals, "overloaded_hours": list of hour indices}.
def replica_plan(hourly_rps, rps_per_replica, target_util, min_replicas, max_replicas, hourly_usd)Example 1
Input: replica_plan([10, 80, 200, 35], 50, 0.8, 1, 4, 2.5)
Output: {'replicas': [1, 2, 4, 1], 'cost': 20.0, 'overloaded_hours': []}
Example 2
Input: hourly_rps = [0, 0], rps_per_replica = 10, target_util = 0.5, min_replicas = 0, max_replicas = 3, hourly_usd = 1.0
Output: {'replicas': [0, 0], 'cost': 0.0, 'overloaded_hours': []}
Constraints
- Python 3 standard library. Code runs in your browser.
- Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1
ceil(rps / (cap * util) - 1e-9) gives the raw replica count.
Hint 2
Clamp with min and max.
Hint 3
Overloaded means rps > replicas * cap, at 100 percent.
Common mistakes
- Checking overload at the target utilisation instead of 100 percent.
- Float error making 30 / 10 round up to 4.
- Forgetting the minimum replicas.
Understand the idea
Dividing by rps_per_replica * target_util sizes the fleet so each replica runs at the target load. Clamping enforces a warm minimum and a budget cap. Hours where even the capped fleet at 100 percent cannot keep up are flagged. In the example, 200 rps would need 5 replicas at 80 percent, but the cap is 4, so hour 2 runs at exactly 100 percent with no headroom. One more request per second would make it overloaded.
Shapes and data flow
List in, dict out.