Count Enabled Feature Flags for Every Config Version
Last updated: November 29, 2026
Quick Overview
Each AWS service configuration version v is a bitmask where every set bit is an enabled feature flag. Given n, return a list where entry v is the number of enabled flags in version v for every v from 0 to n, in better than O(n log n) time.
Amazon
November 29, 20260
0
0 solved
Each AWS service configuration version v is a bitmask where every set bit is an enabled feature flag. Given n, return a list where entry v is the number of enabled flags in version v for every v from 0 to n, in better than O(n log n) time.
Shows up in Amazon online assessments as a short bit-counting task. The grader cares about the O(n) dynamic programming relation between a number and a smaller number, not about calling a popcount builtin.
What the Interviewer Expects
- Notes the naive popcount per number is O(n log n)
- Derives bits[v] = bits[v >> 1] + (v & 1) or bits[v & (v - 1)] + 1
- Implements in one linear pass
- Handles n = 0
- Explains why the recurrence is correct
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
- Why does v & (v - 1) clear the lowest set bit?
- How would you count flags that differ between two versions?
- How would you return only versions with exactly k flags enabled?
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 ans of length n + 1 where ans[v] is the number of 1 bits in v. Target: O(n) time. The interviewer is fine with O(n) output space since the output itself is that...
High-Level Design
Counting bits of each number independently costs O(log v) per number. Instead reuse answers we already computed. Shifting v right by one drops its lowest bit, giving a smaller number whose count we kn...