Bitwise XOR Subsequences
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 July 2026 hands you a subsequence problem with a catch hiding in the rules: a length-1 subsequence is invalid, no matter what k is. Adjacent elements in your pick must XOR to exactly k, and n goes up to 10^5, so brute force is dead on arrival. This is a bit-manipulation plus hash map problem dressed up as a subsequence puzzle. If you've got an invite and 48 hours, learn the one identity that cracks it. StealthCoder sits invisibly as a safety net on the live OA if your mind goes blank on the day.
The problem
A subsequence of an array is formed by removing zero or more elements without changing the order of the remaining elements. A subsequence is valid when the bitwise XOR of every pair of adjacent elements equals k. A subsequence of length 1 is invalid, regardless of the value of k, because it contains no adjacent pair. Given an integer array arr of size n and an integer k, return the length of the longest valid subsequence. Function maxSubsequenceLength(n: int, arr: int[], k: int) → int Examples Example 1 n = 5 arr = [2, 1, 3, 5, 2] k = 2 return = 2 The subsequence [1, 3] is valid because 1 XOR 3 = 2. No valid subsequence is longer than 2. For example, [2, 1, 3] is invalid because 2 XOR 1 is not 2. Example 2 n = 3 arr = [1, 1, 1] k = 0 return = 3 The entire array is valid because every adjacent pair is (1, 1) and 1 XOR 1 = 0. Constraints 1 <= n <= 10^5 0 <= arr[i] <= 10^6 0 <= k <= 10^6
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that a XOR b = k means b = a XOR k. So for each element x, the previous element in the chain must be x XOR k. Keep a hash map best[v] = length of the longest valid subsequence ending in value v. Walk the array. For x, look up best[x XOR k]. If it exists, candidate = that + 1. Then update best[x] = max(best[x], candidate). Track the global max of candidates. Answer is 0 if no pair ever formed, since length 1 is invalid. The pitfall is seeding best[x] = 1 for a lone element, then returning that as the answer. Seeding with 1 is fine for chaining, but you must only record answers from real extensions. Also watch k = 0, where x XOR k is x itself, so read the map before you write it. This runs in O(n). StealthCoder is your hedge on the live OA if the map update order trips you.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Bitwise XOR Subsequences 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass JP Morgan's OA.
JP Morgan reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Bitwise XOR Subsequences FAQ
What's the trick in Bitwise XOR Subsequences?+
XOR is its own inverse. If a XOR b = k, then b = a XOR k. So for each element you only need the best chain ending at x XOR k. A hash map from value to longest chain length turns an exponential search into one O(n) pass.
How hard is this JP Morgan OA question really?+
Medium. The code is short, about ten lines. The difficulty is spotting that it's a DP over values instead of indices. If you've seen longest chain problems with a hash map, you'll get it quickly. If you try pairs or recursion, you'll time out at n = 10^5.
What should I return when no valid subsequence exists?+
Return 0. A single element is invalid by definition, so a chain only counts once it has at least two elements. In Example 1 the answer is 2, not 1 or 3. Only update your answer when you extend an existing chain, never from a lone element.
Why does k = 0 need special care?+
When k is 0, x XOR k equals x, so the previous value you look for is the same as the current one. Read the map first, compute the candidate, then write. Example 2 with [1, 1, 1] and k = 0 should give 3, which tests exactly this order.
How do I prepare for this pattern in 48 hours?+
Practice the pattern of storing best result per value in a hash map, then looking up a derived partner like x XOR k or target minus x. Write this one from scratch twice. Test k = 0, all-distinct arrays, and arrays with no valid pair so you know the 0 case.