Reported March 2024
Amazontwo pointers

Maximum Number of Balanced Shipments

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

Amazon's March OA threw this balancing problem at candidates, and it's designed to catch people who jump straight to brute force. You've got shipments with weights, and you need to pair them so each pair sums to the same total. The trick isn't iterating through every combo. It's recognizing that you can sort, use a two-pointer approach, and count valid pairings in linear time. StealthCoder can scaffold the pairing logic if you blank on the greedy choice during the live assessment.

Pattern and pitfall

The pattern here is two-pointers with a greedy constraint. Sort the shipments by weight, then use left and right pointers to find pairs that sum to a target. The catch is figuring out what the target should be, or whether multiple targets are allowed. Most candidates either try to brute-force all pairs (time limit hit) or overthink which target to pick. The real insight is that once you fix a target sum, the greedy pairing strategy works: pair the lightest with the heaviest to hit that sum, then move inward. If that pair works, continue; if not, the answer is zero or you need to try a different strategy. StealthCoder's role here is to keep you from rewriting the two-pointer loop three times under pressure.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Maximum Number of Balanced Shipments 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximum Number of Balanced Shipments FAQ

Do all shipments have to be paired?+

The problem asks for the maximum number of balanced shipments, so you want to maximize pairs. Not all shipments may pair if the weights don't align to a consistent sum. Check the problem statement for whether unpaired shipments are allowed or penalized.

What if there's no valid target sum?+

If no two shipments can form a pair with the same sum as any other pair, the answer is 0. Some solutions return 0, others return the count of pairs formed before a mismatch. Re-read the problem definition of 'balanced.'

Is brute force fast enough?+

Likely no. O(n^2) checking all pairs will TLE on large shipment counts. Two-pointers after sorting is O(n log n), which scales. Focus on the greedy pairing strategy, not iteration.

Can shipments have the same weight?+

Yes, duplicates are common. That affects how you handle the two-pointer movement. If two shipments have equal weight and they pair together, you may need special logic to avoid double-counting or invalid pairings.

Should I try all possible target sums?+

Maybe. If the problem allows, you could iterate over candidate sums and count valid pairings for each, returning the max. But often a single target is implied by the problem structure. Read carefully before you code.

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