Magicsheet logo

Check if Bitwise OR Has Trailing Zeros

Easy
100%
Updated 8/1/2025

Asked by 1 Company

Check if Bitwise OR Has Trailing Zeros

What is this problem about?

The "Check if Bitwise OR Has Trailing Zeros interview question" is a bit manipulation task. You are given an array of positive integers. You need to determine if it's possible to select two or more elements from the array such that their bitwise OR result has at least one trailing zero. A number has a trailing zero in binary if it is an even number.

Why is this asked in interviews?

Companies like Meituan ask the "Check if Bitwise OR Has Trailing Zeros coding problem" to test a candidate's understanding of the OR operation and binary properties. It evaluates whether you recognize that for an OR result to be even, every number involved in the operation must also be even. It’s a test of "Bit Manipulation interview pattern" logic.

Algorithmic pattern used

This problem follows the Parity Analysis pattern.

  1. Trailing Zero Property: A number has trailing zeros if and only if it is even (x % 2 == 0).
  2. OR Property: The bitwise OR of two numbers (a,b)(a, b) is even only if both aa and bb are even.
    • even | even = even (LSB: 0 | 0 = 0)
    • even | odd = odd (LSB: 0 | 1 = 1)
    • odd | odd = odd (LSB: 1 | 1 = 1)
  3. Logic: To have at least one trailing zero in the OR of two or more elements, you simply need to find at least two even numbers in the array.

Example explanation

Array: [1, 2, 3, 4, 5]

  • Evens: {2, 4}.
  • Count of evens = 2.
  • Since we have two even numbers, we can OR them: 2 | 4 = 6. Binary: 110. It has a trailing zero. Result: True. Array: [1, 2, 3]
  • Evens: {2}.
  • Count of evens = 1.
  • We need at least two elements. We'd have to OR 2 with 1 or 3, both resulting in an odd number. Result: False.

Common mistakes candidates make

  • Misinterpreting Trailing Zeros: Thinking it means something complex like leading zeros or multiple zeros. It simply means the number is even.
  • Complex ORing: Trying to compute all possible OR combinations (O(2N)O(2^N)) instead of just counting even numbers (O(N)O(N)).
  • Selecting One Element: Forgetting the requirement to select at least two elements.

Interview preparation tip

Bitwise OR only preserves zeros where all inputs have a zero. If you need a zero at the least significant bit (LSB), all chosen numbers must have a zero at the LSB. This is a fundamental "Bit Manipulation interview pattern."

Similar Questions