The Maximum Number of Alloys coding problem gives you k machines, each capable of producing alloys using n metals in different compositions. You have an initial stock of each metal and a budget to buy additional metal. Given the cost of each metal and a budget, find the maximum number of alloys you can produce using exactly one machine (the machine you choose to use for all production). The problem requires binary searching on the answer.
Microsoft and MathWorks use this problem to test binary search on the answer — a powerful technique for optimization problems where you can efficiently check feasibility. Candidates who recognize "maximize X subject to budget constraint" as a binary search candidate, and implement the feasibility check correctly across multiple machines, demonstrate strong algorithmic thinking.
Binary search on the answer + greedy feasibility check: Binary search on the number of alloys t produced. For a given t and machine k, check if you can produce t alloys within budget: for each metal i, you need composition[k][i] * t units total; you have stock[i] already; the deficit is max(0, composition[k][i]*t - stock[i]); total cost is sum(deficit * cost[i]). If total cost ≤ budget, t is feasible for machine k. Binary search finds the maximum t where at least one machine can feasibly produce t alloys.
Time: O(k * n * log(max_answer)) where max_answer is bounded by the minimum stock + budget/cost.
k=1 machine, n=2 metals. Composition: [[1, 1]]. Stock: [0, 0]. Budget: 3. Costs: [2, 1].
With stock: [1, 0]. Budget: 3. Costs: [2, 1].
composition * t * cost can be large. Use 64-bit integers or clamp to budget+1 early.For the Array Binary Search interview pattern, "maximize X subject to resource constraint" is a classic binary search on answer template. The feasibility function must be monotone: if you can produce t alloys, you can produce any t' < t. Practice identifying this monotonicity before binary searching, as it's the prerequisite for the technique to be valid.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Earliest Second to Mark Indices I | Medium | Solve | |
| Minimum Time to Repair Cars | Medium | Solve | |
| Maximum Candies Allocated to K Children | Medium | Solve | |
| Minimum Limit of Balls in a Bag | Medium | Solve | |
| Minimum Number of Days to Make m Bouquets | Medium | Solve |