Count Enabled Permission Bits for Every Mask Up to N

Last updated: November 29, 2026

Quick Overview

Permission masks are integers whose set bits are enabled permissions. For every mask value from 0 to n inclusive, return how many permissions it enables, in O(n) total time.

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

0

0

0 solved


Permission masks are integers whose set bits are enabled permissions. For every mask value from 0 to n inclusive, return how many permissions it enables, in O(n) total time.

A Meta screen question that starts as popcount and becomes a small dynamic program. The interviewer checks whether you can reuse previous answers via a bit relation rather than recounting bits for each number.

What the Interviewer Expects
  • Give the naive per-number popcount at O(n log n)
  • Find the recurrence bits[i] = bits[i >> 1] + (i & 1)
  • Or the alternative bits[i] = bits[i & (i - 1)] + 1
  • Produce an O(n) solution and explain why it is correct
  • Mention built-in popcount instructions
Key Topics to Cover
Bit manipulation
Dynamic programming
Popcount
Permission masks
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
  • How does Brian Kernighan's trick count bits for a single number?
  • How would you count total set bits from 0 to n without the list, for n up to 10^18?
  • How would you vectorize this in production?
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: integer n >= 0. Output: list bits of length n+1 where bits[i] is the number of 1 bits in i. Target O(n) time, output-sized space....

High-Level Design

Shifting i right by one drops its lowest bit, so i >> 1 has the same set bits as i except possibly the last one. That gives the recurrence bits[i] = bits[i >> 1] + (i & 1), where i >> 1 < i is a...


Submit Your Answer
Markdown supported