Count Set Bits in an Integer Array
Updated
Problem statement
You are given an integer array values. Return the total number of set bits across all its elements. A set bit is a binary digit equal to 1.
For this exercise, each element is a signed 32-bit integer. Count the 1 bits in its 32-bit two's-complement representation, including the sign bit. Equivalently, interpret a nonnegative value v as the unsigned word v, and a negative value as the unsigned word v + 2^32. Only bit positions 0 through 31 belong to that word.
Count every array occurrence. Repeated values contribute each time; do not deduplicate the array. Return one aggregate count, rather than a separate count for each element.
The empty array has a total of 0. Do not change values, and use fresh local state for each call. Return a nonnegative 64-bit integer. The maximum total under these bounds is 32*256 = 8192, so no modulus or overflow rule is needed.
Function
countArraySetBits(values: int[]) → longExamples
Example 1
values = [0,1,2,3]return = 4The values 0, 1, 2 and 3 have 0, 1, 1 and 2 set bits, respectively. The total is 0+1+1+2 = 4.
Example 2
values = [-1,-2147483648,2147483647]return = 64The 32-bit word for -1 has 32 set bits. The word for -2147483648 has only its highest bit set. The word for 2147483647 has 31 set bits. Their total is 32+1+31 = 64.
Example 3
values = [5,5,-2]return = 35Each occurrence of 5 has two set bits. The 32-bit word for -2 has 31 set bits. Both occurrences of 5 count, so the total is 2+2+31 = 35.
Constraints
0 <= values.length <= 256.- Every element of
valuesis an integer between-2147483648and2147483647, inclusive. - Use the 32-bit two's-complement word for every element, and count all occurrences, including duplicates.
More Akuna Capital Onsite Interview questions
- Match Orders by Price and Arrival TimeOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Positive Nth Root with Gradient DescentPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Remove Duplicates While Preserving OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2025
- First and Last Position in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2022
- Maximum Observation in a Time WindowPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2021
- Stream Median, Mean and MaximumPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2021
- Group AnagramsONSITE INTERVIEW · Seen Aug 2021
- Maximum Stock Profit with a Transaction LimitONSITE INTERVIEW · Seen Aug 2021
- Rotate a Square MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2020
- Trace of a Square MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen May 2019
Similar questions at other companies
- Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW
- Missing and Repeated NumberCommvault · ONSITE INTERVIEWCommvault interview questions
- Check Endianness Of A Byte ArrayNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Find the Odd-Occurrence ValueExpedia · ONSITE INTERVIEWExpedia interview questions
- Packed Bit Array: Set and Reset BitsNutanix · ONSITE INTERVIEWNutanix interview questions
- Find the Two Unique NumbersZomato / Eternal · ONSITE INTERVIEWZomato / Eternal interview questions
- Bayesian Scenario ClassifierAmazon · ONSITE INTERVIEW
- Maximum Unique-Character Word SubsetMeta · ONSITE INTERVIEWMeta interview questions
- Hybrid Run-Length and Bit-Packing EncoderDatabricks · ONSITE INTERVIEWDatabricks interview questions
- Largest Combination With Bitwise AND Greater Than ZeroMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
Akuna Capital interview experiences
- Akuna Capital Quant Trader and QR Interviews: 5 Experiences
- Akuna Capital JQR CodePair Rounds: 8 Experiences (Part 6 of 6)
- Akuna Capital QR Probability Phones: 14 Experiences (Part 5 of 6)
- Akuna Capital QR-ML Interviews: 11 Experiences (Part 4 of 6)
- Akuna Capital QR Regression Rounds: 11 Experiences (Part 3 of 6)
- Akuna Capital QR Coding Interviews: 17 Experiences (Part 2 of 6)