‹ DS&A interview · Socratic
DSA · Bit Manipulation · #47

Bitwise AND of Numbers Range

Module 50 · difficulty 3/5·30:00starts on first keystroke

Given two integers `left` and `right` that represent the inclusive range `[left, right]`, return the bitwise AND of all numbers in this range. Implement the function `rangeBitwiseAnd(left, right)`. Key insight: a bit position is `1` in the result only if it is `1` for *every* number in the range. The result is exactly the **common binary prefix** of `left` and `right`, with all lower bits set to `0`.

Examples
  • left = 5, right = 7 45=101, 6=110, 7=111. AND = 100 = 4.
  • left = 0, right = 0 0
  • left = 1, right = 2147483647 0The range spans bit boundaries, so no high bit survives.
Constraints
  • · 0 <= left <= right <= 2^31 - 1
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.