FastPrepCount Set Bits
Problems/Count Set Bits

Count Set Bits

Bank of America logoBank of America● EasyNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported by a Bank of America new grad from a 2023 campus drive, as one of two tasks in a 60-minute online coding test that was followed by a pre-recorded video interview. The task was to count the 1 bits in the binary form of an unsigned integer. The candidate's approach removed the lowest 1 bit again and again until the number became zero.

The report gives no examples or limits. Here, n is an unsigned 32-bit value, from 0 to 4,294,967,295.

Our version is about 90% the same as the reported question.

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) → int

Examples

Example 1

n = 45return = 4

45 = 32 + 8 + 4 + 1, which is 101101 in binary. It has four 1 bits.

Example 2

n = 1024return = 1

1024 = 2^10, which is a single 1 followed by ten 0 bits.

Example 3

n = 4294967295return = 32

This 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

Bank of America interview experiences

  • Bank of America interviews · December 2025
  • Bank of America OA · December 2025
  • Bank of America interviews · September 2026
  • Bank of America OA · September 2026
All Bank of America interview questionsSee Bank of America hiring insights
Saved
class Solution {
    public int countSetBits(long n) {
        // write your code here
    }
}
Checking account…