Maximize Consecutive XOR
Reported by candidates from IBM's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The fastest way to burn this IBM question, reported in August 2026, is to write a loop from n to x and XOR as you go. With n up to 10^12 that never finishes, and it hides the real point: this is a bit-manipulation problem with a closed-form answer. The prefix XOR from 0 to k repeats every four numbers, and n being divisible by 4 is the hint. Once you see that, the code is two lines. If you freeze when the OA clock starts, StealthCoder sits invisibly on your screen and gives you the pattern and a working solution while you finish the assessment.
The problem
Given an integer n that is divisible by 4, find an integer x satisfying all of the following: x >= n. The binary representation of x has the same number of bits as the binary representation of n. The value v = n XOR (n + 1) XOR... XOR x is as large as possible. If several values of x maximize v, return the smallest such x. Function getMaxX(n: long) → long Examples Example 1 n = 4 return = 6 The possible values are 4, 5, 6, and 7. Their consecutive XOR values are 4, 1, 7, and 0, respectively. The maximum is reached when x = 6. Example 2 n = 8 return = 14 The valid values have four binary bits. At x = 14, the range XOR equals 15, the largest four-bit value, so 14 is optimal. Example 3 n = 16 return = 30 The valid values have five binary bits. Choosing x = 30 makes the range XOR equal 31, which is the largest possible five-bit value. Constraints 4 <= n <= 10^12. n is divisible by 4.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Use the XOR of 0..k pattern. It equals k when k%4==0, 1 when k%4==1, k+1 when k%4==2, and 0 when k%4==3. Because n%4==0, n-1 has remainder 3, so the prefix XOR up to n-1 is 0. That means n XOR... XOR x just equals prefixXor(x). Now maximize it. The biggest value with b bits is 2^b - 1, and you get it when x%4==2, since the prefix is x+1. So x = 2^b - 2, where b is the bit length of n. Check: n=4 gives 6, n=8 gives 14, n=16 gives 30. Candidates with x%4==0 only reach x itself, which is smaller, so the answer is unique and the tie-break never matters. The pitfall is brute force or overflow in a 32-bit type. Use 64-bit. If you blank on the modulo-4 cycle during the OA, StealthCoder is the hedge that surfaces it fast.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Maximize Consecutive XOR 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass IBM's OA.
IBM reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximize Consecutive XOR FAQ
What's the trick in IBM's Maximize Consecutive XOR?+
The XOR of 0..k cycles with period 4. Since n is divisible by 4, the prefix XOR before n is 0, so the range XOR collapses to prefixXor(x). Maximizing that gives x = 2^b - 2, where b is the number of bits in n.
How hard is this problem really?+
The code is trivial, but the insight isn't obvious if you haven't seen the prefix XOR cycle. Medium difficulty at most. People who test small cases by hand on 4, 8 and 16 usually spot the 2^b - 2 pattern within minutes.
Why does brute force fail here?+
n can reach 10^12, so looping through every x and XORing a growing range is far too slow, even with a running XOR. You need the closed form. The examples (6, 14, 30) all fit 2^b - 2, which points straight at it.
Do I need to worry about the smallest x tie-break?+
No. Only x with remainder 2 mod 4 can reach the all-ones value 2^b - 1, and within b bits that's just 2^b - 2. Other remainders give strictly smaller XOR values, so there's never a tie to break.
How do I prepare for this in 48 hours?+
Work out the XOR of 0..k for k from 0 to 8 and write down the mod-4 cycle. Then solve three or four small cases by hand and confirm the formula. Use 64-bit integers and compute the bit length with a shift loop or built-in.