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
November 29, 20260
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample 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...