First Adjacent Same-Parity Pair
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure here is just an array and one pass over it, and that's the whole ZipRecruiter question reported in September 2022. You scan adjacent pairs and return the left index of the first pair where both values share parity. If none match, you return -1. It looks too easy, which is exactly why people lose points on it. Negative odd numbers and empty arrays are where the sloppy solutions break. If you blank during the live OA, StealthCoder runs invisibly on your desktop and gives you a clean solution, but this one is short enough to write from memory.
The problem
Return the zero-based left index of the first adjacent pair whose two values have the same parity. Return -1 when every adjacent pair has opposite parity. Negative odd values are treated as odd. Function firstSameParityPair(values: int[]) → int Examples Example 1 values = [1,2,4,5] return = 1 The first same-parity pair begins at index one. Example 2 values = [1,2,3,4] return = -1 Every adjacent pair has opposite parity. Constraints 0 <= values.length <= 100000 -1000000000 <= values[i] <= 1000000000
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that you don't need extra storage. Walk i from 0 to length minus 2 and compare values[i] % 2 with values[i+1] % 2. The pitfall is negatives. In many languages, -3 % 2 returns -1, not 1, so comparing remainders directly can fail when one number is -3 and another is 3. Use (values[i] & 1) or compare the parity of the sum, since two numbers share parity when their sum is even. Check abs or the bitwise AND and you're safe. Also handle length 0 and 1, where the loop never runs and you return -1. Time is O(n), space is O(1). With up to 100000 elements, nothing fancy is needed. If your mind goes blank on the negative modulo behavior mid-assessment, StealthCoder is the safety net that catches it.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill First Adjacent Same-Parity Pair 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 ZipRecruiter's OA.
ZipRecruiter 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.
First Adjacent Same-Parity Pair FAQ
How hard is the First Adjacent Same-Parity Pair question really?+
It's easy. One linear pass with a parity comparison on neighbors. The difficulty is in the edge cases: empty array, single element, and negative odd numbers. If you handle those three, you're done in a few minutes.
What's the trick for negative numbers?+
Don't rely on x % 2 equaling 1 for odd values. In several languages, -3 % 2 is -1. Use (x & 1), or check whether (a + b) is even, or compare a % 2 != 0 style booleans. Any of those treats negative odds as odd.
What should I return for an empty or single-element array?+
Return -1. There are no adjacent pairs to check, so no same-parity pair exists. Your loop should run from 0 to length minus 2, which naturally skips both cases without a special branch.
Do I need a hash map or sorting?+
No. Sorting would destroy the original indices and the adjacency you need. A hash map adds nothing. Just iterate once, compare each value with the next one, and return the first matching left index.
How do I prepare for this in 48 hours?+
Write it once in your language and test with [1,2,4,5], [1,2,3,4], [], [7], and [-3,3]. Confirm the output is 1, -1, -1, -1, and 0. That covers the negative modulo trap and the boundary cases.