Compare Odd- and Even-Index Sums
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
ZipRecruiter reported this OA in September 2024, and the input size is the first thing to read. Up to 100000 elements means you want one pass, not anything fancy. It's a plain array scan: add each value to an even bucket or an odd bucket, then compare. If the invite is in your inbox, this one is a warm-up, and the only way to lose it is sloppy edge cases. If you blank on the details live, StealthCoder runs invisibly as a safety net, but you probably won't need it here.
The problem
You are given an integer array values. Using zero-based indices, compute the sum at even indices and the sum at odd indices. Return "odd" when the odd-index sum is larger, "even" when the even-index sum is larger, and "equals" when the sums are equal. Function compareIndexSums(values: int[]) → String Examples Example 1 values = [1,2,3,4] return = "odd" The even-index sum is 4 and the odd-index sum is 6. Example 2 values = [5,1,0,4] return = "equals" Both index groups sum to 5. Constraints 0 <= values.length <= 100000 -100000 <= values[i] <= 100000
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that there isn't one. Loop once, check i % 2, add to the right running total, and compare at the end. That's O(n) time and O(1) space. Brute force barely exists here, so the 100000 limit just tells you not to build extra arrays or sort anything. Pitfalls are small but real. Values can be negative, so don't assume the larger sum is positive or that a bigger element count means a bigger sum. An empty array gives 0 and 0, so return "equals". Sums stay under about 5 billion in magnitude in the worst case (100000 * 100000 / 2 = 5e9), which overflows a 32-bit int, so use a long in Java or C++. Return the exact lowercase strings. If you freeze on the overflow detail during the live OA, StealthCoder is the hedge that catches it.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Compare Odd- and Even-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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Compare Odd- and Even-Index Sums FAQ
How hard is the ZipRecruiter even/odd index sum problem really?+
Easy. It's a single pass over the array with two running totals. The difficulty is entirely in edge cases: negative numbers, an empty array, and integer overflow on large inputs. If you handle those three, you're done in a few minutes.
What's the trick to compareIndexSums?+
There's no hidden trick. Iterate once, add values[i] to the even total when i is even and the odd total otherwise. Compare at the end and return "odd", "even", or "equals". Don't sort, don't slice into new arrays, and don't overthink it.
Do I need a long for the sums?+
Yes, to be safe. With 100000 elements at 100000 each, one group can reach about 5 billion, which overflows a 32-bit int. Use long in Java, C++, or C#. Python handles big integers automatically, so no change is needed there.
What should I return for an empty array?+
Return "equals". The constraints allow length 0, so both sums stay at 0 and compare equal. Your loop naturally handles this if you initialize both totals to zero and never index into the array directly.
How do I prep for this in 48 hours?+
Practice writing clean single-pass array loops with accumulators, and check your language's integer limits. For this reported September 2024 ZipRecruiter question, 15 minutes is plenty. Spend the rest of your time on the harder problems that might appear alongside it.