FastPrepMinimum Cost to Purchase Servers
Problems/Minimum Cost to Purchase Servers

Minimum Cost to Purchase Servers

Amazon logoAmazon● MediumFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

AWS sells servers to its clients. One client wants to buy servers to run an application.

There are n servers. power[i] is the efficiency of server i: a whole number that measures its computing power. cost[i] is the number of AWS credits that server i costs. Every cost is either 1 or 2.

You want a set of servers whose total power is at least target. Each server can be bought only once. Return the smallest total cost of such a set. If all the servers together have less power than target, return -1.

For example, let power = [3, 5], cost = [1, 2] and target = 5. Server 0 alone has power 3, which is not enough. Server 1 alone has power 5 and costs 2. Buying both costs 3. The answer is 2.

Function

minCostToPurchaseServers(power: int[], cost: int[], target: int) → int

Complete the function minCostToPurchaseServers in the editor.

minCostToPurchaseServers has the following parameters:

  • int power[n]: the efficiency of each server
  • int cost[n]: the cost of each server, 1 or 2
  • int target: the total power you need

Returns int: the smallest total cost, or -1 if no set of servers is enough

Examples

Example 1

power = [4, 4, 6, 7]cost = [1, 1, 2, 2]target = 7return = 2

A cost of 1 buys only one server with power 4, and 4 is less than 7. Servers 0 and 1 have power 4 + 4 = 8, which is at least 7. They cost 1 + 1 = 2. The answer is 2.

Constraints

  • 1 <= n <= 10^5, where n is the length of power and of cost
  • 1 <= power[i] <= 10^4
  • cost[i] is 1 or 2
  • 1 <= target <= 10^9

More Amazon interview questions

  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Maximal SquareONSITE INTERVIEW · Seen Nov 2026
  • Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
  • Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
  • Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026

Similar questions at other companies

  • Find Minimum TimeAlphaSense · OA
  • Find Minimum TimeBlackRock · OABlackRock interview questions
  • Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
  • Minimum Cost K-Capable ModelsMicrosoft · OAMicrosoft interview questions
  • Minimum Cost of Every K-Capable Model SetTikTok · OATikTok interview questions
  • Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA
  • Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
  • Minimum Distinct Prefix CostGoldman Sachs · OAGoldman Sachs interview questions
  • Maximum Alternating-Index Packet ValueInfosys · OAInfosys interview questions
  • Maximize Minimum Deployment DifficultyJP Morgan Chase · OAJP Morgan Chase interview questions

Interview reports that mention this question

  • Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)

Amazon interview experiences

  • Amazon Experienced SDE Onsite Questions: 1 Experience
  • Amazon SDE Onsite Interview Questions: 9 Experiences
  • Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
  • Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
  • Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)
See Amazon hiring insights
Saved
class Solution {
    public int minCostToPurchaseServers(int[] power, int[] cost, int target) {
      // write your code here
    }

}
Checking account…