Counting Bits
A easy-tier problem at 80% community acceptance, tagged with Dynamic Programming, Bit Manipulation. Reported in interviews at Nvidia and 0 others.
Counting Bits is a deceptive easy problem. It looks like a bit-twiddling puzzle but it's really a dynamic programming problem in disguise, and that gap catches people off guard in live assessments. Nvidia asks it frequently. The naive approach feels right but times out. The trick is recognizing the recurrence relation hidden in binary representations. If you blank on the pattern during your online assessment, StealthCoder surfaces the optimal solution in seconds, invisible to the proctor, so you don't waste 15 minutes on brute force.
Companies that ask "Counting Bits"
Counting Bits is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround.
Get StealthCoderThe problem asks you to count the number of 1-bits in the binary representation of every number from 0 to n. The trap is trying to count bits directly for each number. The win is seeing that each number's bit count relates to a smaller number you've already solved. The key insight is that i and i >> 1 share almost the same bit pattern, or that i and i & (i-1) have a known difference. Once you map this relation into a DP table, the solution becomes linear time and linear space. Most candidates either iterate naively over bits for each number or miss the subproblem structure entirely. StealthCoder is your safety net if the pattern doesn't click before the timer runs low on your live OA.
Pattern tags
You know the problem.
Make sure you actually pass it.
Counting Bits recycles across companies for a reason. It's easy-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Counting Bits interview FAQ
Is this really an easy problem or is the rating wrong?+
It's rated easy because the bit count pattern is elegant once you see it, and a 70% acceptance rate confirms most candidates eventually solve it. The confusion comes from expecting a bit manipulation problem instead of a DP one. The algorithmic insight is simple; the psychology of the setup is the actual difficulty.
What's the trick I'm missing if my solution works but is slow?+
You're probably counting bits individually for each number. The trick is recognizing that each number's bit count depends on a previously computed answer. Use either i >> 1, i & (i-1), or i ^ (i-1) to relate the current number to a smaller one you've already solved.
How does this relate to Dynamic Programming if it feels like a bit problem?+
It's DP because you build up solutions from smaller subproblems. The 'state' is the number i, and the recurrence is derived from a mathematical property of binary numbers. You're not iterating bits; you're iterating numbers and using a known relation to avoid recomputation.
Do I need to know advanced bit tricks to solve this?+
No. You need to know that right-shifting removes the last bit, and that i & (i-1) clears the lowest set bit. These are basic operations. The insight is connecting one of these operations to the subproblem, not memorizing bit hacks.
Why does Nvidia ask this if it's marked easy?+
Easy problems test whether you think clearly under pressure, not whether you're a genius. Nvidia values candidates who recognize patterns and avoid obvious traps. This problem does both. The acceptance rate is high because the pattern is clean once found, but high enough that it stays a legitimate filter in screening.
Want the actual problem statement? View "Counting Bits" on LeetCode →