FastPrepRearrange Server Load

Rearrange Server Load

IBM logoIBM● MediumNEW GRADOA

Updated

Problem statement

Given two arrays, serverCapacity, and serverLoad, each of size n, they represent the resource requirements or capacities of servers and the workload distribution across the servers. The total resource consumption is the sum of serverCapacity[i] * serverLoad[i] (0 ≤ i < n).

The task is to rearrange the array serverLoad, redistributing the load across servers to minimize the total resource consumption, ensuring their efficient utilization. Return this optimal arrangement. In scenarios with multiple solutions, return the lexicographically smallest array of serverLoad.

Function

rearrangeServerLoad(serverCapacity: int[], serverLoad: int[]) → int[]

Complete the function rearrangeServerLoad in the editor.

rearrangeServerLoad has the following parameters:

  1. 1. int[] serverCapacity: an array of integers representing server capacities
  2. 2. int[] serverLoad: an array of integers representing server loads

Returns int[]: the rearranged array of server loads that minimizes the total resource consumption

Examples

Example 1

serverCapacity = [4, 5, 6]serverLoad = [1, 2, 3]return = [3, 2, 1]

The task is to rearrange the elements in the array serverLoad to minimize the sum, serverCapacity[i] * serverLoad[i]. If serverLoad = [3, 2, 1], the total resources are calculated as follows: 4 * 3 + 5 * 2 + 6 * 1 = 28. This arrangement yields the lowest sum of resources.

Example 2

serverCapacity = [1, 2, 3, 3, 3]serverLoad = [2, 2, 4, 5, 6]return = [6, 5, 2, 2, 4]

For the rearranged serverLoad = [6, 5, 2, 2, 4], the total resource requirement is calculated as follows: = 1 * 6 + 2 * 5 + 3 * 2 + 3 * 2 + 3 * 4 = 40. 40 is the minimum possible sum after rearranging the serverLoad array, hence the answer.

More IBM interview questions

Similar questions at other companies

IBM interview experiences

All IBM interview questionsSee IBM hiring insights
Saved
class Solution {
    public int[] rearrangeServerLoad(int[] serverCapacity, int[] serverLoad) {
      // write your code here
    }

}
Checking account…