FastPrepCount Set Bits with a Byte Lookup Table
Problems/Count Set Bits with a Byte Lookup Table

Count Set Bits with a Byte Lookup Table

Microsoft logoMicrosoft● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Microsoft Research PhD interview asks for the number of set bits in a byte. It then scales the idea to one million numbers by working out the counts for 0 to 255 ahead of time. We chose to sum four byte lookups for each signed 32-bit value, two's-complement rules, the function signature and the limits.

Given an array of signed 32-bit integers values, return the total number of one bits across their 32-bit two's-complement representations.

Precompute the set-bit count for every byte value from 0 through 255, then use four table lookups per integer.

Function

countSetBits(values: int[]) → long

Examples

Example 1

values = [0,1,255,256]return = 10

The values contain 0, 1, 8, and 1 set bits respectively.

Example 2

values = [-1]return = 32

Signed -1 has all 32 bits set in two's-complement representation.

Constraints

  • 0 <= values.length <= 1000000
  • Every value is a signed 32-bit integer.

More Microsoft Onsite Interview questions

  • Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
  • Coin ChangeONSITE INTERVIEW · Seen Oct 2026
  • Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
  • Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Find the Two Unique NumbersZomato / Eternal · ONSITE INTERVIEWZomato / Eternal interview questions
  • Smallest Sufficient TeamExpedia · ONSITE INTERVIEWExpedia interview questions
  • Bayesian Scenario ClassifierAmazon · ONSITE INTERVIEW
  • Maximum Unique-Character Word SubsetMeta · ONSITE INTERVIEWMeta interview questions
  • Game of Life: In-Place Next GenerationGoogle · ONSITE INTERVIEWGoogle interview questions
  • Single Number with TriplicatesGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • 2048 Simulation and Board CodecCitadel · ONSITE INTERVIEWCitadel interview questions
  • SubsetsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • BitFont - Maximum Non-Overlapping KerningStripe · ONSITE INTERVIEWStripe interview questions
  • Find the Value Occurring Exactly OnceMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions

Interview reports that mention this question

  • Microsoft AI/ML Intern Interviews: 114 Experiences (Part 1 of 2)

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public long countSetBits(int[] values) {
      // Write your code here.
    }

}
Checking account…