Reported June 2024
Amazondynamic programming

Optimizing Box Weights

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 Optimizing Box Weights OA question, reported June 2024, tests your ability to partition an array into two subsets with minimal weight difference. You're likely facing a subset sum variant under time pressure. This is a classic optimization problem that looks deceptively simple until you realize the greedy approach fails. StealthCoder can spot the pattern and lock in the solution if you blank on the recurrence relation during the live assessment.

Pattern and pitfall

The core trick: you need to split boxes into two shipments such that the absolute difference between total weights is minimized. Brute force (try all 2^n partitions) times out. The real solution uses dynamic programming to find all achievable sums up to half the total weight, then pick the largest one that doesn't exceed that threshold. This mirrors the balanced partition problem. Common pitfall is assuming a greedy sort-and-alternate works. It doesn't. You'll need a DP table or bitmask to track which sums are reachable. StealthCoder gives you the structure when you're stuck midway through the live OA.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Optimizing Box Weights 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as partition equal subset sum. If you have time before the OA, drill that.

⏵ The honest play

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

Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Optimizing Box Weights FAQ

Is this really a DP problem or can greedy work?+

Greedy fails. Sorting and alternating assignment doesn't minimize difference. You need DP to explore all reachable subset sums, then pick the one closest to total/2. Amazon expects the DP solution.

What's the time complexity I should target?+

O(n * S) where S is the sum of all weights. Space is O(S) for the DP array. This passes for n up to a few hundred if weights aren't massive.

Do I need to track which boxes go in which partition?+

No. The OA only asks for the minimum difference. You compute reachable sums, find the largest one <= total/2, then difference is total - 2*largest.

How do I avoid TLE if the total weight is huge?+

If total weight exceeds ~10^6, the DP table explodes. Check input constraints. Sometimes you need bitmask DP or meet-in-the-middle for smaller n. Ask yourself: are weights bounded or is n small.

Does order of boxes matter?+

No. You're partitioning a set, not arranging a sequence. Order of iteration through boxes doesn't affect the optimal difference.

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