Counting Bits
Reported by candidates from GoodScore's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that sinks people on the GoodScore Counting Bits question, reported in September 2026, is n = 0. Your output has to be [0], not an empty array, and loops that start at 1 quietly return the wrong length. The task is simple: for every integer from 0 through n, return how many set bits it has. It's a dynamic programming problem wearing a bit-manipulation costume. If you blank during the live OA, StealthCoder runs invisibly as a safety net and hands you the recurrence. Know the trick anyway, because it takes five lines.
The problem
For every integer from 0 through n, return the number of set bits in its binary representation. Function countBits(n: int) → int[] Examples Example 1 n = 0 return = [0] Case 1 exercises the documented deterministic contract. Example 2 n = 1 return = [0,1] Case 2 exercises the documented deterministic contract. Example 3 n = 2 return = [0,1,1] Case 3 exercises the documented deterministic contract. Constraints 0 <= n <= 1000000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The naive route counts bits for each number separately, which is O(n log n) and works, but the clean answer is O(n). Use the recurrence bits[i] = bits[i >> 1] + (i & 1). Shifting right drops the last bit, which you already computed, and the AND adds it back. Allocate an array of size n + 1, set bits[0] = 0, and loop from 1 to n. The pitfalls are an off-by-one on the array size and mishandling n = 0, which the first example tests directly. With n up to 1000000, recursion per number or string conversion is wasteful, so skip it. Brian Kernighan's trick (i & (i - 1)) also works for the per-number approach. During the live GoodScore OA, StealthCoder is the hedge if the recurrence slips your mind under pressure. Otherwise just write the loop and test n = 0, 1, 2 by hand.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Counting Bits cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as counting bits. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass GoodScore's OA.
GoodScore reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Counting Bits FAQ
How hard is Counting Bits really?+
Easy once you see the recurrence, annoying if you don't. The brute force of counting bits per number passes the constraints anyway. The O(n) DP version is what interviewers like, and it's only a few lines long.
What's the trick for the GoodScore Counting Bits question?+
Use bits[i] = bits[i >> 1] + (i & 1). The number i without its last bit is i >> 1, which is smaller and already solved. Add 1 if i is odd. That gives you linear time with no per-number bit loop.
What edge case should I test first?+
n = 0. The expected output is [0], a one-element array. Make sure your array has size n + 1 and that index 0 is set to 0 before your loop starts at 1. Example 1 in the problem checks exactly this.
Is the brute force acceptable with n up to 1000000?+
Yes, counting bits for each number costs about 20 operations, so roughly 20 million total. That's fine. Still, write the DP version if you can, since it's shorter and shows you recognized the pattern.
How do I prepare for this in 48 hours?+
Write the recurrence from memory twice, then test n = 0, 1, 2, 5 by hand. Learn the i & (i - 1) trick as a backup. That's all this problem needs. Spend the rest of your time on other patterns.