FastPrepMinimum Racks for Server Resources

Minimum Racks for Server Resources

Google logoGoogle● HardFULLTIMEPHONE SCREEN
Learn

Problem statement

You are given n indivisible servers. Server i requires bandwidth[i] units of bandwidth and power[i] units of power.

Every rack has a bandwidth capacity of rackBandwidthCapacity and a power capacity of rackPowerCapacity. A group of servers can share one rack only when both of these conditions hold:

  • The sum of their bandwidth requirements is at most rackBandwidthCapacity.
  • The sum of their power requirements is at most rackPowerCapacity.

Assign every server to exactly one rack and return the minimum number of racks required.

Function

minimumRacks(bandwidth: int[], power: int[], rackBandwidthCapacity: int, rackPowerCapacity: int) → int

Examples

Example 1

bandwidth = [4,4,2]power = [3,2,3]rackBandwidthCapacity = 6rackPowerCapacity = 5return = 2

The server with requirements (4, 2) can share a rack with the server requiring (2, 3). Their totals are (6, 5). The remaining server uses a second rack.

Example 2

bandwidth = [3,3,3,3]power = [4,4,4,4]rackBandwidthCapacity = 6rackPowerCapacity = 8return = 2

Each rack can hold exactly two servers, so two racks are sufficient and necessary.

Example 3

bandwidth = [2,2,2]power = [6,6,6]rackBandwidthCapacity = 10rackPowerCapacity = 10return = 3

Bandwidth would allow the servers to share, but any pair needs 12 power units. Each server therefore needs its own rack.

Constraints

  • 1 <= bandwidth.length == power.length <= 15.
  • 1 <= bandwidth[i] <= rackBandwidthCapacity <= 10^6.
  • 1 <= power[i] <= rackPowerCapacity <= 10^6.
  • Every server fits in an otherwise empty rack.

More Google problems

See Google hiring insights
public int minimumRacks(int[] bandwidth, int[] power, int rackBandwidthCapacity, int rackPowerCapacity) {
    // Write your code here.
}
bandwidth[4,4,2]
power[3,2,3]
rackBandwidthCapacity6
rackPowerCapacity5
expected2
Checking account…