Reported October 2022
ZipRecruiterarray

Difference between Even- and Odd-Index Sums

Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live ZipRecruiter OA. Under 2s to a working solution.
Founder's read

The mistake that sinks a first attempt on this ZipRecruiter OA, reported in October 2022, is getting the sign backwards. It's evenSum minus oddSum, not the other way around. The problem itself is tiny: walk an array, add values at even indices, subtract values at odd ones. It's a plain array pass, and it's meant to be quick. But quick problems punish sloppy reading. If you blank on the details under the clock, StealthCoder runs invisibly on your desktop as a safety net during the live assessment. Most candidates won't need it here. Read the spec twice and you're fine.

The problem

Using zero-based indexing, sum values at even indices and values at odd indices. Return evenSum - oddSum.

Function
indexParityDifference(values: int[]) → int

Examples
Example 1
values = [1,2,3,4]
return = -2
Even-index sum four minus odd-index sum six gives negative two.
Example 2
values = [7]
return = 7
Index zero contributes to the even sum.

Constraints
0 <= values.length <= 100000
-100 <= values[i] <= 100

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that there's no trick. One loop, one running total. Add values[i] when i is even, subtract it when i is odd. That gives evenSum - oddSum directly, with O(n) time and O(1) space. The pitfalls are all small. Flipping the sign gives 2 instead of -2 on Example 1. Forgetting that index 0 counts as even breaks the single-element case, where [7] must return 7. Empty arrays are allowed by the constraints, so return 0 instead of crashing. Values are small and the array tops out at 100000, so overflow isn't a concern. Don't build two separate arrays or sums unless it helps you read the code. Test both examples and an empty input before submitting. If your head goes blank on a problem this simple, StealthCoder is the hedge during the live OA, but the real defense is checking the sign against Example 1.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Difference between Even- and Odd-Index Sums 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 StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass ZipRecruiter's OA.

ZipRecruiter 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.

Difference between Even- and Odd-Index Sums FAQ

How hard is the even and odd index sum difference problem really?+

It's easy. One pass through the array with a running total. The only way to lose points is a sign error, an off-by-one on index parity, or missing the empty array case. Budget your time for edge cases, not the algorithm.

What's the trick to indexParityDifference?+

Add the value when the index is even, subtract it when the index is odd. That computes evenSum - oddSum in a single loop with no extra storage. Check it against Example 1, where 4 minus 6 gives -2.

Which edge cases should I test?+

Test an empty array, which should return 0. Test a single element like [7], which returns 7 because index 0 is even. Test negative values too, since the range is -100 to 100. Those three cover nearly every way this breaks.

Do I need to worry about performance with 100000 elements?+

No. A linear scan is O(n), which is trivial at 100000. Values are bounded by 100 in magnitude, so the total stays small and fits in a normal integer. Don't over-engineer it.

How do I prepare for this in 48 hours?+

Write the loop from memory once in your chosen language. Then run the two given examples plus an empty array by hand. Spend the rest of your time on harder array and hash map problems, since an OA usually has more than one question.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with ZipRecruiter.

OA at ZipRecruiter?
Invisible during screen share
Get it