About Shipping
Reported by candidates from JP Morgan's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The JP Morgan OA reported in June 2026 hands you "About Shipping", and the mistake that sinks a first attempt is trying every split with a fresh sum each time. That's O(n^2) on an array up to 200000 long, and it times out. The real task is a prefix sum scan. Split the array at j, compare the two sums, and the gap is your cost. You can read it in two minutes and still botch the one detail that matters: positivity. If you blank on it live, StealthCoder runs invisibly as a safety net.
The problem
A shop has n item types. The quantity of the i-th item type is quantity[i], using 1-based indexing in the description. The items must be shipped in two consecutive consignments split by an index j: the first consignment contains item types [1, 2,..., j], and the second contains item types [j + 1,..., n]. Choose j such that 1 <= j < n, so both consignments are non-empty. In one operation, increase or decrease any quantity[i] by 1. Quantities must remain positive. Return the minimum number of operations needed to make the total quantities in the two consignments equal, choosing the split optimally. Function getMinimumOperations(quantity: int[]) → long Complete the function getMinimumOperations in the editor with the following parameter: int quantity[n]: the quantities of each item type Returns long: the minimum number of operations required to make the sums equal in an optimal division Examples Example 1 quantity = [1, 4, 4] return = 1 Increase quantity[3] by 1 to get [1, 4, 5]. Splitting at j = 2 gives consignments with sums 5 and 5, so the answer is 1. Example 2 quantity = [2, 2] return = 0 The only split already has equal sums, so no operation is needed. Constraints 2 <= quantity.length <= 200000 1 <= quantity[i] <= 1000000000 The answer can be large, so use 64-bit arithmetic in fixed-width languages.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build the total sum once, then walk j from 1 to n-1 while keeping a running prefix. For each split the difference is |prefix - (total - prefix)|. Each operation changes one quantity by 1, which changes one side's sum by 1 and shifts the difference by 1. So the cost for that split is the absolute difference, and you take the minimum across all j. The pitfall is the positivity rule. Check it carefully: the side that needs to grow can always grow. The side that needs to shrink has at least one element that can shrink by the needed amount, since the shrinking side is the larger sum, and you can instead increase the smaller side. So you never have to decrease. Use 64-bit integers, because sums reach 2e14. If you freeze on the OA, StealthCoder is the hedge that reads the screen and hands you this scan.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill About Shipping 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 JP Morgan's OA.
JP Morgan 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.
About Shipping FAQ
What's the trick in About Shipping?+
Prefix sums. Compute the total once, then for each split j from 1 to n-1 take the absolute difference between the left prefix and the right remainder. The answer is the smallest difference. One pass, O(n) time, O(1) extra space.
Why is the cost just the absolute difference?+
One operation moves one side's sum by exactly 1, so closing a gap of d takes d operations. You can always raise the smaller side, so the positivity rule never blocks you. Example 1 shows it: split at 2 gives 5 and 4, gap 1, answer 1.
Do I need 64-bit integers?+
Yes. With 200000 items at up to 1000000000 each, the total reaches about 2e14, which overflows 32-bit ints. Use long in Java or C++. Python handles it natively. The problem statement warns about this explicitly.
How hard is this JP Morgan OA question really?+
Easy to medium. The algorithm is a single prefix sum loop. People lose points on the brute-force O(n^2) approach timing out, or on overflow. If you know prefix sums, you can finish it quickly.
How do I prepare for this in 48 hours?+
Write the prefix sum scan from memory twice, test it on [1,4,4] and [2,2], and check the edge case of n = 2. Then skim a few other split-point array problems. That covers this question's pattern without overstudying.