Desired Replicas from CPU Utilization
Problem Statement
The Horizontal Pod Autoscaler scales replicas so average CPU utilization returns to a target. Compute the desired replica count.
Background
The HPA formula is
desired = ceil(current_replicas * current_util / target_util)
where utilizations are percentages. The result is clamped to [min_replicas, max_replicas].
Your Task
def desired_replicas(current_replicas, current_util, target_util, min_r, max_r):
Return the clamped desired replica count (int).
Input Format
- current_replicas (int), current_util, target_util (float, percent), min_r, max_r (int).
Output Format
- A single int.
Sample
print(desired_replicas(3, 90.0, 60.0, 1, 10))
Output:
5
Example:
print(desired_replicas(3, 90.0, 60.0, 1, 10))
5
- Calculate the raw desired replica count by scaling the current replicas according to the ratio of current to target CPU utilization: 3×60.090.0=3×1.5=4.5
- Apply the ceiling function to the result to ensure the replica count is an integer and sufficient to meet the target, since we cannot have a fraction of a pod: ⌈4.5⌉=5
- Clamp the calculated value to the allowed range by taking the maximum of the minimum replicas and the minimum of the maximum replicas and the calculated value: max(1,min(10,5))=5
- The final output is 5
Constraints:
desired = ceil(current_replicas * current_util / target_util).- Clamp to
[min_r, max_r]. - Return an int.
1. Background Knowledge
The Horizontal Pod Autoscaler (HPA) in Kubernetes automatically adjusts the number of pod replicas to maintain a target CPU utilization. It operates on a proportional control principle: if the current average CPU utilization exceeds the target, the system scales out (increases replicas); if it falls below, it scales in. The core formula is:
desired=⌈target_utilcurrent_replicas×current_util⌉Here, current_util and target_util are expressed as percentages (e.g., 90.0 means 90%). The ceiling function ensures that even a slight overshoot triggers an additional replica, preventing sustained over-utilization.
In practice, the computed desired count is clamped to a user-defined range [min_replicas, max_replicas]. This prevents the autoscaler from scaling below a minimum service level or exceeding resource quotas. Clamping is a standard safety mechanism in control systems to keep outputs within physically meaningful bounds.
2. Algorithm Approach
This is a direct formula evaluation problem with a post-processing clamp step. The algorithm pattern is:
- Compute the raw desired replica count using the HPA formula.
- Round up using the ceiling function to ensure integer replicas.
- Clamp the result to the valid range using max(min_r, min(max_r, value)).
No iteration, sorting, or data structure is needed. The solution is a single arithmetic expression followed by a bounded projection.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.