Coding Easy Software Engineer Backend Full Stack

Given a non-empty array of integers `nums`, every element appears twice except for one. Find that single one in linear runtime and constant space.

Practise this question with AI feedback

Record your spoken answer or type your response to receive immediate scoring and STAR improvements.

💡 Suggested Approach & Sample Answer

Optimal Bit Manipulation Approach (XOR):
- Leverage the mathematical properties of the bitwise XOR (^) operator: x ^ x = 0 and x ^ 0 = x.

def single_number(nums: list[int]) -> int:
    result = 0
    for num in nums:
        result ^= num
    return result

Complexity & Key Logic:
- Time Complexity: O(N) single linear scan over the array.
- Space Complexity: O(1) constant auxiliary space.
- Algorithm Details:
- XOR is commutative and associative: the order of operations does not matter (a ^ b ^ a = (a ^ a) ^ b = 0 ^ b = b).
- Every number that appears twice cancels itself out to 0.
- The single unique element remains as the final value in result.

🎯 What Interviewers Listen For

How would you answer this in a real interview?

Use our free AI mock practice tool to test your response, analyze speaking pacing, and refine your STAR structure.

🔗 Related Coding Questions