Total Pairwise Hamming Distance of Member Permission Bitmasks
Last updated: November 29, 2026
Quick Overview
Each member in a company page admin group has a 30-bit permission mask. Return the sum of Hamming distances over all unordered pairs of masks, in O(30 n) time.
0
0
0 solved
Each member in a company page admin group has a 30-bit permission mask. Return the sum of Hamming distances over all unordered pairs of masks, in O(30 n) time.
LinkedIn Pages and Campaign Manager encode roles as permission bitmasks, and audits compare them. This tests whether you see that bits are independent, so you count per bit position instead of comparing every pair.
What the Interviewer Expects
- Notes the O(n^2) pairwise baseline
- Decomposes the distance by bit position
- For each bit, multiplies count of ones by count of zeros
- Uses bit shifts and masks correctly
- States O(B n) time with B = 30
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 would you find the pair with the largest Hamming distance?
- What changes for 4,096-bit masks stored as byte arrays?
- How would you group members whose masks differ by at most one bit?
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: list of nonnegative ints below 2^30, length up to 10^5. Output: integer sum over i < j of popcount(a[i] XOR a[j]). An empty or single-element list returns 0....
High-Level Design
Hamming distance is the number of bit positions that differ, and the total over all pairs is the sum over each bit position of the number of pairs that differ in that bit. For a fixed bit, a pair diff...