Count Numbers with the Same Set-Bit Count
Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The trap in this Microsoft OA, reported in September 2026, is the one that makes a brute-force loop quietly fail: n can be close to 2^31, so you can't walk every x up to f(n). The question dresses itself up with f(n), the next all-ones number, but it's a bit-manipulation counting problem underneath. If you see it with a clear head, it's a few lines of combinatorics. If you blank, StealthCoder is the invisible safety net running during the live OA. Either way, here's the shape of the answer before you open the invite.
The problem
You are given a positive integer n. Define f(n) as the smallest integer greater than or equal to n whose binary representation consists entirely of 1 bits. Count the positive integers x that satisfy all of the following conditions: x <= f(n) x != n x and n contain the same number of set bits in their binary representations Return the count modulo 1,000,000,007. Complete countSameBitNumbers for the given integer n. Function countSameBitNumbers(n: int) → int Examples Example 1 n = 6 return = 2 f(6) = 7, whose binary representation is 111. The integers other than 6 with two set bits and at most 7 are 3 (011) and 5 (101). Example 2 n = 10 return = 5 f(10) = 15. Among the six four-bit patterns containing two set bits, one represents n itself, so five other integers qualify. Example 3 n = 7 return = 0 The only positive integer at most 7 with three set bits is 7, which must be excluded. Constraints 0 < n < 2^31
Reported by candidates. Source: FastPrep
Pattern and pitfall
Let k be the bit length of n. f(n) is 2^k - 1, the smallest all-ones number at or above n. Every k-bit pattern with leading zeros allowed is then at most f(n), so the candidates are all k-bit patterns with the same popcount as n. That count is C(k, p), where p is popcount(n). Subtract 1 to exclude n itself, then take the result mod 1,000,000,007. Example 2 confirms it: k=4, p=2, C(4,2)=6, minus 1 gives 5. The pitfall is the edge case: when n is already all ones, like 7, C(k,k)=1 and the answer is 0. Also x must be positive, but with p>=1 the all-zero pattern is never counted. Don't iterate up to 2^31. Compute the binomial with Pascal's triangle up to 31 or with exact integers, then reduce mod. StealthCoder is there if the formula slips under pressure during the live assessment.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Count Numbers with the Same Set-Bit Count 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Microsoft's OA.
Microsoft reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Numbers with the Same Set-Bit Count FAQ
What's the trick in Count Numbers with the Same Set-Bit Count?+
Realize f(n) is 2^k - 1 where k is n's bit length. Every k-bit pattern is then in range, so the answer is C(k, popcount(n)) minus 1 for n itself, mod 1,000,000,007. No looping over values needed.
How hard is this Microsoft OA question really?+
Easy once you see the combinatorics, medium if you don't. The code is about five lines. The difficulty is noticing that brute force over a range near 2^31 won't work and that the range collapses into a clean binomial count.
What edge case breaks a naive solution?+
When n is all ones, like 7 or 15, the only pattern with that popcount is n itself, so the answer is 0. Forgetting the subtract-one step gives 1 there. Also, a brute-force loop up to 2^31 times out.
Do I need modular inverse for the binomial?+
No. k is at most 31, so build Pascal's triangle with additions mod 1,000,000,007, or use Python's math.comb and reduce at the end. Skipping inverses avoids a whole class of bugs.
How do I prepare for this in 48 hours?+
Practice popcount, bit length, and building small binomial tables. Then test your function on the three examples: n=6 gives 2, n=10 gives 5, n=7 gives 0. If those pass, you've covered the main edge cases.