PIXELBANKv9.1.0
Menu

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:

Input:
print(desired_replicas(3, 90.0, 60.0, 1, 10))
Output:
5
Reasoning:
  • Calculate the raw desired replica count by scaling the current replicas according to the ratio of current to target CPU utilization: 3×90.060.0=3×1.5=4.53 \times \frac{90.0}{60.0} = 3 \times 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\lceil 4.5 \rceil = 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\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.
🔒

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.

solution.py

Test Results

0/0
Run code to see test results.