Total Bit Differences Across All Pairs of Feature Flag Masks

Last updated: November 29, 2026

Quick Overview

Each experiment arm is encoded as a 32-bit feature flag mask. Return the sum of Hamming distances over all pairs of masks in the list, faster than comparing every pair.

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

0

0

0 solved


Each experiment arm is encoded as a 32-bit feature flag mask. Return the sum of Hamming distances over all pairs of masks in the list, faster than comparing every pair.

A Meta screen question where the naive O(n^2) pairwise popcount is too slow for 10^5 masks. The interviewer checks whether you switch to per-bit counting and can justify the c * (n - c) term.

What the Interviewer Expects
  • Give the O(n^2) pairwise baseline and why it is too slow
  • Count, per bit, how many masks have it set
  • Use c * (n - c) pairs differing at that bit
  • Sum across 32 bits for O(32n)
  • Explain how this relates to the variance of bits
Key Topics to Cover
Hamming distance
Bit counting
Combinatorics
Contribution technique
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
  • Return the pair with the maximum Hamming distance.
  • What if masks are 4096 bits long and sparse?
  • How would you compute this incrementally as arms are added and removed?
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: list of n non-negative integers below 2^32, n up to 10^5. Output: ∑i<jpopcount(mi⊕mj)\sum_{i<j} \text{popcount}(m_i \oplus m_j). Pairwise is about 5 x 10^9 popcounts, too slow, so we need near-linear time....

High-Level Design

Swap the order of summation. The total equals, for each bit position b, the number of pairs that differ at b. If c masks have bit b set and n - c do not, every pairing of one from each side differs at...


Submit Your Answer
Markdown supported