Reported October 2026
Microsoftbinary search

Maximum Alloy Production Within Budget

Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Microsoft OA. Under 2s to a working solution.
Founder's read

Microsoft reportedly put this one in front of candidates in October 2026, and the detail that matters is the budget of up to 10^18 against n up to 100000 metals. That rules out any simulation. It's binary search on the answer: guess a number of alloy units, compute the purchase cost, and check it against the budget. If you've got the OA in a day or two, this is a clean pattern to lock in. And if you blank mid-assessment, StealthCoder runs invisibly on your desktop and hands you the structure in real time.

The problem

A foundry produces one alloy using n different metals. For each metal i:
composition[i] is the quantity required to produce one unit of alloy.
stock[i] is the quantity already available.
cost[i] is the purchase price per additional unit.
You may buy any nonnegative integer quantity of each metal, spending at most budget in total. Return the maximum whole number of alloy units that can be produced using the initial stock plus the purchased metals.

Function
maxAlloyUnits(composition: int[], stock: int[], cost: int[], budget: long) → long

Examples
Example 1
composition = [1,2]
stock = [0,1]
cost = [1,1]
budget = 3
return = 1
One unit needs purchases [1,1] costing 2. Two units need purchases [2,3] costing 5, which exceeds the budget.
Example 2
composition = [2,1]
stock = [4,0]
cost = [3,2]
budget = 4
return = 2
The existing first metal covers two alloy units, and buying two units of the second metal costs exactly 4.
Example 3
composition = [3]
stock = [10]
cost = [5]
budget = 0
return = 3
The stock alone produces three complete units, with one unit of metal left over.

Constraints
1 <= composition.length == stock.length == cost.length <= 100000.
1 <= composition[i] <= 10^9.
0 <= stock[i] <= 10^9.
1 <= cost[i] <= 10^9.
0 <= budget <= 10^18.
The answer fits in a signed 64-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is monotonicity. If you can make k units, you can make k-1, so feasibility flips once and binary search works. For a guess k, each metal needs composition[i]*k, and you only pay for the shortfall: max(0, composition[i]*k - stock[i]) * cost[i]. Sum those and compare to budget. The pitfall is overflow. With k near 10^18 and composition up to 10^9, the product blows past 64 bits. Cap your upper bound sensibly, something like 2*10^18 / min composition, or bail out early as soon as the running cost exceeds budget. Use 128-bit math or early exit checks in languages without big ints. Set low to 0 and find the largest feasible k. Complexity is O(n log(range)), around 60 iterations times 100000. StealthCoder is the hedge if the overflow guards slip your mind live, but the logic itself is short.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Maximum Alloy Production Within Budget cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as maximum number of robots within budget. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Microsoft's OA.

Microsoft reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximum Alloy Production Within Budget FAQ

What's the trick in Maximum Alloy Production Within Budget?+

Binary search on the number of alloy units. For a guess k, sum the cost of each metal's shortfall, which is max(0, composition[i]*k - stock[i]) * cost[i]. If the total is within budget, k is feasible. Feasibility is monotonic, so you search for the largest feasible k.

How do I avoid overflow in this problem?+

Products like composition[i]*k and shortfall*cost can exceed 64 bits. Break out of the cost loop the moment the running total passes budget. Also pick a tight upper bound for k, or use 128-bit integers or a division check before multiplying.

What should the binary search bounds be?+

Low is 0, which is always feasible. High must be above any possible answer. Stock alone gives at most about 10^9 units, and budget adds at most 10^18 divided by the cheapest per-unit cost, so a high around 2*10^18 works if your check is overflow-safe.

Is a greedy approach possible here?+

Not cleanly. Every metal is needed in fixed proportion, so there's no choice about which metal to buy. The only decision is how many units to target, which is why you search over the answer instead of picking items greedily.

How do I prepare for this in 48 hours?+

Practice the binary-search-on-answer template until it's automatic. Write the feasibility function first, then wrap it in the search loop. Test against the three examples, especially the zero-budget case where stock alone gives floor(stock/composition) units.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Microsoft.

OA at Microsoft?
Invisible during screen share
Get it