Common Permission Bits Shared by Every ID in a Contiguous Range

Last updated: November 29, 2026

Quick Overview

Given two integers left <= right, return the bitwise AND of every integer in [left, right], which tells you which flag bits are set across an entire contiguous block of account IDs.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given two integers left <= right, return the bitwise AND of every integer in [left, right], which tells you which flag bits are set across an entire contiguous block of account IDs.

A Meta screen question where iterating the range is infeasible because right can be 2^31. The interviewer is looking for the common-prefix insight and a crisp argument for why lower bits always zero out.

What the Interviewer Expects
  • Explain why looping over the range is too slow
  • Argue that any bit below the highest differing bit becomes 0
  • Compute the common binary prefix by shifting
  • Offer the n & (n - 1) alternative
  • Trace an example
Key Topics to Cover
Bitwise AND
Common prefix
Bit shifting
Math reasoning
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What is the bitwise OR of the range instead?
  • What is the XOR of all integers from 0 to n in O(1)?
  • How would you answer many range queries fast?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: 0 <= left <= right < 2^31. Output: AND of all integers in the range. Looping is up to 2 billion operations, so we need something O(log right)....

High-Level Design

Write left and right in binary and find their longest common prefix. At the first bit where they differ, left has 0 and right has 1. Somewhere in the range we pass from a number of the form `prefix 0 ...


Submit Your Answer
Markdown supported