Reported September 2026
Amazongreedy

Maximize K-Element AND with an Increment Budget

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

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

The data structure this Amazon problem hinges on is a bit-by-bit greedy answer, not a fancy container. Amazon candidates reported it in September 2026, and it looks scary because it mixes increments, a budget, and a bitwise AND over exactly k picks. Strip it down and it's a greedy build of the answer from the high bit down, with a sort and a cost check at each bit. If you've got the OA in a day or two, learn that loop cold. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment.

The problem

You are given an array nums, an integer k, and a nonnegative increment budget budget. You may increase array elements by nonnegative integer amounts whose total is at most budget.
After applying the increments, choose exactly k distinct array positions. Return the maximum possible bitwise AND of the chosen values.

Examples
Example 1
nums = [5,4,1,7,2]
k = 3
budget = 3
return = 6
Increase 4 to 6 and 5 to 6. Choosing 6, 6, 7 gives 6 & 6 & 7 = 6.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: build the answer bit by bit from the highest bit down. For a candidate mask (answer so far plus the new bit), compute the cost for each element to become a supermask of it. That cost is the smallest value v >= nums[i] with (v & mask) == mask, minus nums[i]. Sort the costs, sum the k smallest, and if the total is within budget, keep the bit. Otherwise drop it. Greedy works because a higher bit outweighs all lower bits combined. The pitfall is computing the per-element cost wrong. Raising a number to a supermask isn't just adding the mask. You may need to carry into a higher bit, so check from the top bit down. Also watch overflow, since the budget can be large. If the cost helper trips you up live, StealthCoder is the hedge that gives you a working version fast.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Maximize K-Element AND with an Increment 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Amazon reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximize K-Element AND with an Increment Budget FAQ

What's the trick for this Amazon AND-with-budget problem?+

Build the answer greedily from the highest bit. For each candidate mask, compute every element's cost to become a supermask of it, sort, and sum the k cheapest. If that sum fits the budget, keep the bit. Higher bits always beat lower ones combined.

How hard is this really?+

Harder than it reads. The greedy idea is short, but the cost to lift a number to a supermask of the mask is where people lose time. Expect to spend most of your effort there and on edge cases like k equal to n.

Can I just binary search the answer?+

Not cleanly. Feasibility isn't monotonic in the numeric value of the AND, since a smaller target doesn't always mean a cheaper mask. Per-bit greedy checks a specific mask each time, which is why it works and plain binary search on value doesn't.

What's the time complexity I should aim for?+

Roughly O(B * n log n), where B is the number of bits, about 30 or 31 for typical ints. Each bit needs a cost pass over n elements plus a sort. You could use a selection step instead of a full sort to shave the log factor.

How do I prepare in 48 hours?+

Practice bitwise AND greedy problems and the supermask cost function until you can write it without looking. Test your helper on small arrays like the sample: [5,4,1,7,2], k=3, budget=3 should give 6. Check overflow and the case where no bit fits.

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

OA at Amazon?
Invisible during screen share
Get it