Maximum Barbell Weight
Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
A barbell, a pile of plates, and a hard cap on weight. That's the Salesforce OA question reported in July 2025, and it's a subset sum in a gym costume. Pick any plates, each once, and get as close to maxCapacity as you can without going over. The example hands you 7 three different ways, which is a hint that you're not hunting for one answer, just the best sum. The catch is in the constraints: weights and capacity go up to 10^9, so the classic DP table is dead on arrival. If you blank on that, StealthCoder is the safety net running invisibly during the live OA.
The problem
An athlete is loading plates onto a barbell with maximum capacity maxCapacity. The weight of each available plate is given by weights[i]. Choose any subset of the plates, using each plate at most once. Return the maximum total plate weight that does not exceed maxCapacity. Function weightCapacity(weights: int[], maxCapacity: int) → int Examples Example 1 weights = [7,1,5,6,2] maxCapacity = 7 return = 7 There are three optimal ways to reach total weight 7: choose [7], [1, 6], or [2, 5]. Return 7. Constraints 1 <= weights.length <= 42 1 <= maxCapacity <= 10^9 1 <= weights[i] <= 10^9
Reported by candidates. Source: FastPrep
Pattern and pitfall
Look at the constraints first. Length is at most 42 and values hit 10^9. A knapsack array indexed by capacity would need a billion cells, so that's the trap. The trick is meet in the middle. Split the weights into two halves of about 21 each. Generate all subset sums for each half, which is about 2 million sums per side. Sort the right list. For every left sum that's within capacity, binary search or two-pointer the largest right sum that fits in the remainder. Track the best total. Total work is around 2^21 times a log factor. Common pitfalls: brute forcing 2^42, forgetting the empty subset, and overflow in languages with 32-bit ints, since two sums near 10^9 can overflow when added. If the split idea won't come, StealthCoder can hand you the structure live during the OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Maximum Barbell Weight 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Salesforce's OA.
Salesforce 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 Barbell Weight FAQ
What's the trick in the Maximum Barbell Weight problem?+
Meet in the middle. Split the plates into two halves, enumerate every subset sum of each half, sort one list, then for each sum in the other list find the largest partner that keeps the total at or under maxCapacity. It turns 2^42 into roughly 2^21 times a log.
Why can't I just use the standard knapsack DP?+
Because maxCapacity goes up to 10^9. A DP array indexed by weight would need a billion entries, which blows memory and time. The small limit of 42 plates is the signal that you should enumerate subsets instead of indexing by capacity.
How hard is this one really?+
Medium-hard if you haven't seen meet in the middle, easy once you have. The logic is short. The difficulty is spotting that 42 plates and huge weights rule out both brute force and DP. Code is about 30 lines.
What edge cases should I test?+
Test a single plate heavier than the capacity, where the answer is 0. Test all plates fitting, where you return their sum. Test an exact match like the example's 7. Also check sums near 10^9 so you don't overflow a 32-bit integer.
How do I prep this in 48 hours?+
Write subset sum generation for a half array, then practice the sorted two-pointer or binary search lookup for the best complement. Do it once from scratch with n around 40. If you've done that, you've covered everything this Salesforce question needs.