Reported June 2024
Zscalerbit manipulation

Find the Element With Odd Frequency

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

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

This Zscaler OA, reported in June 2024, looks like a frequency-counting problem, but it's really a parity problem in disguise. One value shows up an odd number of times, everything else shows up an even number of times, and you return the odd one. If you've got an invite for the next couple of days, this is the kind of question where the fast answer and the clever answer are both fine. StealthCoder sits on your desktop as a safety net if you blank mid-assessment, but you probably won't need it once you see what this reduces to.

The problem

Given a nonempty integer array values, exactly one distinct value occurs an odd number of times. Every other distinct value occurs an even number of times.
Return the value whose frequency is odd.

Function
findOddFrequencyElement(values: int[]) → int

Examples
Example 1
values = [2,3,2,3,3]
return = 3
Value 2 appears twice, while 3 appears three times.
Example 2
values = [7]
return = 7
The only value appears once, which is odd.
Example 3
values = [10,20,10,20,10,20,10]
return = 20
Value 10 appears four times and 20 appears three times.

Constraints
1 <= values.length <= 200000
-10^9 <= values[i] <= 10^9
Exactly one distinct value has odd frequency; every other distinct value has even frequency.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The problem reduces to one question: which value has odd parity of occurrences? Two clean solutions work. First, a hash map counting frequencies, then scan for the odd count. That's O(n) time and O(n) space, and it's the safe answer. Second, XOR every element together. Pairs cancel because x ^ x = 0, and a value appearing an even number of times cancels fully, so what's left is the value with odd frequency. Wait, check Example 3: 10 appears four times and 20 three times, so XOR gives 20. It works because even counts cancel and the odd count leaves one copy. The pitfall is overthinking it with sorting, or forgetting negative values, which XOR handles fine. With n up to 200000, either approach passes. If you freeze during the live OA, StealthCoder can hand you the XOR one-liner while you keep your head straight.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Find the Element With Odd Frequency 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 StealthCoder

Related leaked OAs

⏵ The honest play

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

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

Find the Element With Odd Frequency FAQ

What's the trick in the Zscaler odd frequency problem?+

Parity. Only one distinct value has an odd count, and every other value has an even count. XOR all elements and the even-count values cancel to zero, leaving the answer. A hash map count also works if you want the more obvious route.

Does XOR really work when a value appears four times?+

Yes. XOR is commutative and associative, so any value appearing an even number of times XORs to 0, no matter how many times. Example 3 with 10 four times and 20 three times gives 20. The odd-count value leaves exactly one copy.

Is the hash map solution good enough?+

Yes. With up to 200000 elements, a frequency map runs in O(n) time and O(n) space, which is fine. XOR just trims the space to O(1). If you're nervous, write the hash map first, get it passing, then mention XOR as an optimization.

Do negative numbers break the XOR approach?+

No. Values range from -10^9 to 10^9, and XOR works on the bit representation of integers, including negatives in most languages. Python, Java, and C++ all return the correct signed result here. Just initialize your accumulator to 0.

How do I prepare for this in 48 hours?+

Write both solutions from memory once: the hash map count and the XOR fold. Then test the single-element case [7] and the Example 3 case. That's about 20 minutes of work. Spend the rest of your time on other hash table and bit manipulation basics.

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

OA at Zscaler?
Invisible during screen share
Get it