Count Set Bits
Updated
Problem statement
Given a non-negative integer n, return the number of 1 bits in its binary form. These are also called its set bits.
Treat n as an unsigned 32-bit value: it is between 0 and 2^32 - 1, so the answer is between 0 and 32.
Function
countSetBits(n: long) → intExamples
Example 1
n = 45return = 445 = 32 + 8 + 4 + 1, which is 101101 in binary. It has four 1 bits.
Example 2
n = 1024return = 11024 = 2^10, which is a single 1 followed by ten 0 bits.
Example 3
n = 4294967295return = 32This is 2^32 - 1, the largest allowed value. All 32 of its bits are 1.
Constraints
0 <= n <= 2^32 - 1(that is,4,294,967,295)
More Bank of America Online Assessment questions
- Return Exact Cash Register ChangeOA · Seen Aug 2026
- Sum Skipping Multiples of 7OA · Seen Nov 2025
- Wildcard Substring MatchOA · Seen Nov 2025
- Maximum String BeautyOA · Seen Sep 2024
- Merge Point of Two Linked ListsOA · Seen Oct 2023
- Remove Duplicates from a ListOA · Seen Oct 2023
- Pairs with Target Sum in a Sorted ArrayOA · Seen Oct 2023
- Count Level-Order Traversals of a TreeOA · Seen Sep 2023
- Count Pairs with Sum KOA · Seen Jun 2023
- Letter Pattern SumOA · Seen Sep 2022
Similar questions at other companies
- Minimum Testers to Find the Poisoned BottleWalleye Capital · OA
- Address on Same PageNVIDIA · OANVIDIA interview questions
- Three-Speaker Truth ConsistencySusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Find the Missing Number from 1 to nGEP · OA
- Invert Ad Visibility BitsJP Morgan Chase · OAJP Morgan Chase interview questions
- Are They PangramsVanguard · OAVanguard interview questions
- Count Times Using at Most Two DigitsLSEG · OALSEG interview questions
- Minimum Tokens RemainingMicrosoft · OAMicrosoft interview questions
- Minimum Operations to Make Binary PalindromicGoogle · OAGoogle interview questions
- Powers of 2Oracle · OAOracle interview questions