Count the 1 Bits in a 32-Bit Value
Updated
Problem statement
You are given a non-negative integer n that fits in 32 unsigned bits, so n is between 0 and 2^32 - 1.
Return the number of bits of n that are 1 when n is written in binary.
Function
countOneBits(n: long) → intExamples
Example 1
n = 11return = 311 in binary is 1011, which has three 1 bits.
Example 2
n = 128return = 1128 is 2^7, so its binary form 10000000 has one 1 bit.
Example 3
n = 4294967295return = 324294967295 is 2^32 - 1, the largest allowed value. All 32 of its bits are 1.
Constraints
0 <= n <= 2^32 - 1
More Amazon Phone Screen questions
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 3SumPHONE SCREEN · Seen Nov 2026
- Circular Primes Below a LimitPHONE SCREEN · Seen Nov 2026
- Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Two SumOA · PHONE SCREEN · Seen Nov 2026
- Reverse a StringPHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Even or Odd Without DivisionAdobe · PHONE SCREENAdobe interview questions
- Power of Two Without DivisionArm · PHONE SCREEN
- Powers of Two in a RangeAkuna Capital · PHONE SCREENAkuna Capital interview questions
- Choose the Next Minesweeper ActionRunloop · PHONE SCREEN
- IP Address to CIDR BlocksOpenAI · PHONE SCREENOpenAI interview questions
- Fewest Games to Find the Top Two TeamsSusquehanna International Group (SIG) · PHONE SCREENSusquehanna International Group (SIG) interview questions
- Minimum Testers to Find the Poisoned BottleWalleye Capital · OA
- Address on Same PageNVIDIA · OANVIDIA interview questions
- Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW
- Find the Missing Number from 1 to nGEP · OA
Amazon interview experiences
- Amazon SWE OA with AI Coding: 1 Experience
- Amazon SWE Intern Phone Screen Interview Experience: 1 Experience
- Amazon New Grad SDE I OA and Virtual Onsite: 1 Experience
- Amazon BIE Intern SQL and Interview Questions: 17 Experiences
- Amazon Experienced SWE AI-Assisted Debugging OA: 1 Experience
- Amazon SDE II (L5) Interview Questions: 5 Experiences