Minimum Digit Changes Between String Halves
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 September 2026 gives you an even-length digit string and one move: change any digit in the first half to any other digit. Your job is to make the first half an anagram of the second. The examples show 123212 needs 1 change and 123456 needs 3. It's a counting problem dressed up as a string problem, and it's quick if you see it. If you blank under the clock, StealthCoder runs invisibly on your desktop and hands you the solution as a safety net. Here's the pattern so you probably won't need it.
The problem
You are given an even-length string s that contains digits only. Split it into two equal halves. In one operation, choose any position in the first half and replace its digit with any other digit from 0 through 9. Return the minimum number of operations needed to make the first half an anagram of the second half. Function getAnagram(s: String) → int Examples Example 1 s = "123212" return = 1 The halves are 123 and 212. Replace digit 3 in the first half with 2, producing 122, which is an anagram of 212. Example 2 s = "123456" return = 3 The halves 123 and 456 share no digit. Every position in the first half must be replaced, so the minimum is 3. Constraints 1 <= s.length <= 10^5 s.length is a multiple of 2. s contains digits only.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Count digit frequencies in each half using two arrays of size 10. Anagram means matching counts. For each digit d, if the first half has more of d than the second, the surplus has to be changed. If it has fewer, that's a gap you fill by changing something else. Each change fixes one surplus and one gap at once, so the answer is the sum of max(0, countFirst[d] - countSecond[d]) over all 10 digits. Equivalently, half the sum of absolute differences. The common pitfall is sorting both halves and comparing position by position, which overcounts. Another is thinking you can edit the second half. You can't. It runs in O(n) time with O(1) space, which matters at 10^5 length. StealthCoder is your hedge in the live OA if the counting logic slips, but the whole solution is about ten lines.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Minimum Digit Changes Between String Halves 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.
Minimum Digit Changes Between String Halves FAQ
What's the trick to the JP Morgan minimum digit changes problem?+
Count digit frequencies in both halves. Anagram means equal counts per digit. Every digit where the first half has a surplus over the second needs at least one change. Sum those surpluses across digits 0 through 9 and that's your answer. No sorting or brute force needed.
How hard is this problem really?+
Easy to low-medium. The idea is a frequency count and a single pass over 10 digits. The only real difficulty is seeing that changes only go one direction, first half to match second, so you sum positive surpluses instead of all differences.
Why does example 2 return 3?+
The halves are 123 and 456 and share no digits. Every first-half digit is a surplus, since the second half has zero of each. That gives 1 + 1 + 1 = 3 surplus digits, so you change all three positions.
What complexity should I aim for with 10^5 length?+
O(n) time and O(1) extra space. One pass to fill two arrays of size 10, then one loop over 10 digits. Anything with sorting is O(n log n), which still passes, but the counting approach is cleaner and what interviewers expect.
How do I prepare for this in 48 hours?+
Write the frequency count solution from scratch twice. Test it on both examples and on an edge case like a string of length 2 with identical digits, which returns 0. Then practice a few other anagram and counting problems so the pattern is automatic.