# Autoscale Policy Utilization Check Amazon OA 2023

## Autoscale Policy Utilization Check Solution

A scaling computing system checks its average utilization every second while it monitors. It implements an autoscale policy to increase or reduce instances depending on the current load as described below. Once an action of increasing or reducing the number of instances is performed, the system will stop monitoring for 10 seconds. During that time, the number of instances does not change.

• If the average utilization < 25%, then an action is instantiated to reduce the number of instances by half if the number of instances is greater than 1. Take the ceiling if the number is not an integer. If the number of instances is 1, take no action.
• If 25% s average utilization s 60%, take no action.
• If the average utilization > 60%, then an action is instantiated to double the number of instances if the doubled value does not exceed 2* 108. If the number of instances exceeds this limit upon doubling, take no action.

Given an array of integers that represent the average utilization at each second, determine the number of instances at the end of the time frame.

Example

instances = 2

averageUtil = [25, 23, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 76, 80]

• At second 1. the average utilization averageUtil[0] = 25 < 25 instances at the end of the time frame.

Example

instances = 2

averageUtil = [25, 23, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 76, 80]

• At second 1, the average utilization averageUtil[0] =25 s 25, so take no action. . At second 2, the average utilization averageUtil[1] = 23 < 25, so reduce the number of instances by half to get 2/2 – 1.
• Since an action was taken the system will stop checking for 10 seconds, from averageUtil[2] through averageUtil[11].
• At averageUtil[12] = 76, 76 > 60, so the number of instances is doubled from 1 to 2. 21 1 22 23 24 25 26

There are no more readings to consider and 2 is the final answer. 27 28

Function Description

Complete the function finalinstances in the editor below.

finalinstances has the following parameter(s):

int instances: the original number of instances running

int averageUtil[n]: the average utilization at each second of the time frame 33 > pub

Returns:

int: the final number of instances running

Constraints .

1 < instances < 10^5

1 < n < 10^5

0 < averageUtil[i] < 100

Input Format For Custom Testing
Sample Case 0
Sample Input For Custom Testing

1
3
5
10
80

Sample Output

2

### SOLUTION

Program: Autoscale Policy Utilization Check Solution in C++

First, we declare the function finalInstances which takes two arguments – int instances and vector averageUtil. instances is the original number of instances running and averageUtil is a vector containing the average utilization at each second of the time frame.

In the function, we initialize a variable i to 0 which will be used to iterate over the elements of the averageUtil vector.

Then we start a while loop which will run until i is less than the size of the averageUtil vector. Within the while loop, we check the current average utilization value at index i of the averageUtil vector.

If the average utilization value is less than 25%, we check if the number of instances is greater than 1. If it is, we divide the number of instances by 2 and round up using the ceil function from the cmath library. We also increment i by 11 since we don’t need to check the next 10 values of averageUtil since we are not allowed to perform any action during that time. If the number of instances is less than or equal to 1, we don’t perform any action.

If the average utilization value is between 25% and 60%, we don’t perform any action and simply increment i by 1.

If the average utilization value is greater than 60%, we check if doubling the number of instances will result in a value less than or equal to 2*10^8. If it will, we double the number of instances. Otherwise, we don’t perform any action. We also increment i by 11 since we don’t need to check the next 10 values of averageUtil since we are not allowed to perform any action during that time.

Finally, we return the number of instances as the output.

``````#include <cmath>

using namespace std;

int finalInstances(int instances, int averageUtil[], int n) {
int i = 0;
while (i < n) {
if (averageUtil[i] < 25) {
if (instances > 1) {
instances = ceil(instances / 2.0);
i += 10;
}
} else if (averageUtil[i] > 60) {
int newInstances = 2 * instances;
if (newInstances <= 2 * pow(10, 8)) {
instances = newInstances;
i += 10;
}
}
i++;
}
return instances;
}
``````

Program: Autoscale Policy Utilization Check Solution in Java

The Java solution is similar to the Python solution, but uses a while loop instead of a for loop. The while loop continues to iterate over the elements of the input array avg until the variable i reaches the end of the array.

Inside the while loop, the current element avg[i] is checked against the conditions specified in the problem statement.

If the current element avg[i] is less than 25, and the current number of instance is greater than 1, then the number of instance is reduced by half and rounded up using the Math.ceil function. The variable i is then incremented by 11 to skip the next 10 elements in the array, as specified in the problem statement.

If the current element avg[i] is greater than 60, and doubling the number of instance does not exceed the limit of 2 * 10^8, then the number of instance is doubled. The variable i is then incremented by 11 to skip the next 10 elements in the array.

Finally, the function returns the final value of instance.

``````import java.lang.Math;

public class Solution {
public static int finalInstances(int instances, int[] averageUtil) {
int i = 0;
while (i < averageUtil.length) {
if (averageUtil[i] < 25) {
if (instances > 1) {
instances = (int) Math.ceil(instances / 2.0);
i += 10;
}
} else if (averageUtil[i] > 60) {
int newInstances = 2 * instances;
if (newInstances <= 2 * Math.pow(10, 8)) {
instances = newInstances;
i += 10;
}
}
i++;
}
return instances;
}
}
``````

Program: Autoscale Policy Utilization Check Solution in Python

The function takes in the initial number of instances (instances) and an array of average utilizations (average_util). It iterates through the array, checking each utilization value and performing the appropriate action based on the instructions given in the problem.

If the utilization is less than 25%, and the number of instances is greater than 1, the number of instances is reduced by half (rounded up) using integer division (//). If the utilization is greater than 60%, and the number of instances doubled does not exceed 2*10^8, the number of instances is doubled. Otherwise, no action is taken.

The function skips monitoring for 10 seconds after each scaling action by continuing the loop when i % 10 == 0. At the end of the loop, it returns the final number of instances.

``````def final_instances(instances, average_util):
for i in range(len(average_util)):
# Check if it's time to resume monitoring
if i % 10 == 0:
continue

# Check if utilization is less than 25%
if average_util[i] < 25 and instances > 1:
instances = (instances + 1) // 2
# Check if utilization is greater than 60%
elif average_util[i] > 60 and instances * 2 <= 2 * 10**8:
instances *= 2

return instances
``````