← Questions
Find the number that appears an odd number of times
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's coding round, last asked Aug 2025. It tests bit manipulation and hashing.
In an integer array, exactly one value appears an odd number of times and every other value appears an even number of times. Return that value. Start with any correct solution, then reduce the extra space to O(1), and explain why it works for very large inputs and negative numbers.
Interface
Unless you agree on a different interface with the interviewer, assume:
odd_one(nums: list[int]) -> int
Example
odd_one([2, 3, 2, 4, 4]) -> 3
odd_one([7]) -> 7
odd_one([-5, 1, -5, -5, 1]) -> -5 # -5 appears three times
Constraints
-
1 <= len(nums) <= 10^6;-10^9 <= nums[i] <= 10^9. - Exactly one value has an odd count.
Clarifying questions
- Is it guaranteed that exactly one value has an odd count, or should invalid input be detected?
- Can values be negative or exceed 32 bits?
- Is the array read-only, or may it be modified or sorted?
What a strong answer covers
- A hash map of counts first: O(n) time, O(d) space.
- XOR of all elements:
x ^ x = 0andx ^ 0 = x, XOR is associative and commutative, so every even group cancels. O(n) time, O(1) space. - Why it is correct for negative numbers (two's complement bits cancel just the same) and for long arrays (no overflow, unlike summing).
- Sorting as an O(1)-extra-space alternative, and its O(n log n) cost.
Follow-ups
- Two values appear an odd number of times: return both (split on any set bit of the XOR).
- Every value appears three times except one, which appears once.
- The numbers arrive as a stream that cannot be stored.
Where it's asked
- Amazon · Coding · Last asked Aug 202555 min · Usually two rounds; one is often an object-oriented design built in steps · No AI
What it tests
bit manipulation · hashing
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
