The Bitwise Judge

An easy Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.

Domain
Python
Difficulty
easy
Seniority
L3
Asked in
i360

Problem

Given an integer n (possibly negative), return True if n is even, False if odd. Solve using bitwise operations only - no %, no /, no //.

No division, no modulo - just a single bit tells you everything.

Example 1

Input:

n = 4

Output:

true

Example 2

Input:

n = 7

Output:

false

Worked solution and explanation

Why this problem exists in real interviews

Checking even/odd with bitwise AND tests the most basic bit manipulation concept: the least significant bit determines parity.


Break down the requirements

Step 1: Check the least significant bit

If n & 1 is 0, the number is even. If 1, it is odd.


The solution

LSB check for parity using bitwise AND
def is_even(n):
    result = (n & 1) == 0
    return result

Common follow-up questions