Count Set Bits with a Byte Lookup Table
Updated
Problem statement
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[]) → longExamples
Example 1
values = [0,1,255,256]return = 10The values contain 0, 1, 8, and 1 set bits respectively.
Example 2
values = [-1]return = 32Signed -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