N-server problem on Amazon OA

Question

Suppose you are given n servers.

Each server has a price and a maximum load capacity.

You are allowed to buy at most 2 servers.

You have a budget max_price (you cannot exceed this budget).

Your task is to choose up to two servers (0, 1, or 2) such that:

The total cost of the chosen servers does not exceed max_price.

The total load capacity is maximized.

You need to return the maximum possible load capacity under these constraints.

Constraints:

1 ≤ n ≤ 2 * 10^5

1 ≤ price[i],load[i] ≤ 10^9

price.size = load.size

Example 1: Suppose:

n = 4 prices = [4, 8, 5, 3] loads = [10, 20, 15, 7] max_price = 8 Options: Pick only one server within budget ≤ 8:

Server 1: price 4, load 10

Server 2: price 8, load 20

Server 3: price 5, load 15

Server 4: price 3, load 7

→ Best single choice = Server 2 (load = 20).

Pick two servers, total price ≤ 8:

Server 1 (4,10) + Server 4 (3,7) → total price = 7, total load = 17

Server 3 (5,15) + Server 4 (3,7) → total price = 8, total load = 22

Server 1 (4,10) + Server 3 (5,15) → total price = 9 (exceeds budget , not possible)

→ Best pair choice = Server 3 + Server 4 → load = 22.

Answer: The maximum load achievable is 22.

Example case 2: n = 4 prices = [4, 8, 5, 3] loads = [10, 20, 15, 7] max_price = 2

Ans: 0 Explanation: no server could be bought within limit of max_price

Can you solve ?

Posted Sep 3, 2025, 6:17 PM UTC

12
Loading comments…