Equalize Arrays with Prefix and Suffix Increments
Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Microsoft reported this one in September 2026, and the detail that trips people up is the -1 case. You get source and target arrays, and each move bumps a prefix ending at i or a suffix starting at i by one. Values go up to 10^13 in magnitude, so it's 64-bit territory. Once you see the difference array, it's a greedy scan over adjacent gaps, no DP needed. Candidates who skip the feasibility check lose the hidden tests. If you've got this OA coming up, learn the one-line formula below. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but you shouldn't need it once this clicks.
The problem
You are given two integer arrays, source and target, of equal length n. In one operation, choose an index i and perform exactly one of these actions: Add 1 to every element in the prefix source[0..i]. Add 1 to every element in the suffix source[i..n - 1]. Return the minimum number of operations required to make source equal to target. If the transformation is impossible, return -1. The source's numeric bounds require 64-bit values, so this practice contract uses long arrays and returns a long. Complete getMinOperations for the given source and target arrays. Function getMinOperations(source: long[], target: long[]) → long Examples Example 1 source = [1, 2, 2] target = [2, 2, 3] return = 2 Increment the prefix ending at index 0, producing [2, 2, 2]. Then increment the suffix starting at index 2, producing [2, 2, 3]. Example 2 source = [1, 1, 1] target = [3, 3, 3] return = 2 Increment the whole array twice. A whole-array increment may be represented as either a prefix ending at index n - 1 or a suffix starting at index 0. Example 3 source = [0, 0, 0, 0, 0] target = [1, 0, 1, 0, 1] return = -1 The required increment profile has two separate downward drops, but only one unit is required at the first position. No combination of prefix and suffix increments can create that profile. Constraints 1 <= n <= 100000 source.length = target.length = n -10^13 <= source[i], target[i] <= 10^13 The correct minimum, when it exists, fits in a signed 64-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build d[i] = target[i] - source[i]. A prefix operation adds to a non-increasing profile, a suffix operation adds to a non-decreasing profile, and d must be the sum of the two, both nonnegative. Every rise between d[i-1] and d[i] has to come from suffix ops, and every fall has to come from prefix ops. Set the suffix profile to start at 0, so the prefix profile starts at d[0]. Then the answer is d[0] plus the sum of all rises. It's feasible only if the sum of all falls is at most d[0]. Otherwise return -1. Check example 3: d is [1,0,1,0,1], the falls total 2, d[0] is 1, so it's impossible. The pitfalls are int overflow and forgetting that negative d[0] is always impossible. That's covered by the same check, since the falls total can't be negative. If the pattern slips away live, StealthCoder can hand you the scan as a hedge.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Equalize Arrays with Prefix and Suffix Increments 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 Microsoft's OA.
Microsoft 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.
Equalize Arrays with Prefix and Suffix Increments FAQ
What's the trick to the Microsoft equalize arrays problem?+
Work on the difference array d = target - source. Prefix ops make a non-increasing contribution and suffix ops make a non-decreasing one. Rises in d are paid by suffix ops, falls by prefix ops. The answer is d[0] plus the sum of positive jumps, valid only if total drops don't exceed d[0].
When do I return -1?+
Return -1 when the sum of all drops between consecutive d values is greater than d[0]. That covers any negative d[0] too, since the drop total is never negative. Example 3 shows it: d is [1,0,1,0,1], drops total 2, but d[0] is only 1.
Do I need dynamic programming here?+
No. It's a single O(n) pass after computing differences. Track the running sum of rises and the running sum of falls, then compare falls against d[0]. If you're reaching for DP or simulating operations, you're overcomplicating it and will probably time out at n = 100000.
Why does it say to use 64-bit values?+
Elements range up to 10^13 in magnitude, so differences reach 2 x 10^13 and the sums of rises can be larger. That overflows 32-bit ints. Use long in Java, long long in C++, and Python is fine. Overflow is a classic silent failure on the large hidden tests.
How do I prepare for this in 48 hours?+
Practice turning an operation on ranges into a difference array problem. Do the three examples by hand, computing d, the rises and the falls. Then code the scan with a long accumulator and test edge cases: n = 1, all equal arrays, and a negative difference. That's enough for this one.