Count Good Numbers
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Strip away the digit rules and this Google OA question from July 2026 is a counting problem in disguise. You're asked how many integers in [1, m] have distinct nonzero digits with no interior valley, meaning no middle digit smaller than both neighbors. It's digit DP territory. If you're taking it in the next day or two, the shape matters more than the syntax. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but knowing the reduction first makes everything easier.
The problem
A positive integer is good when it satisfies all of the following conditions: It does not contain the digit 0. Each digit appears at most once. No interior digit is smaller than both of its adjacent digits. Given a positive integer m, return the number of good integers in the inclusive range [1, m]. Function countGoodNumbers(m: int) → int Examples Example 1 m = 21 return = 18 The good numbers are 1 through 9, 12 through 19, and 21. The numbers 10 and 20 contain 0, while 11 repeats a digit. Two-digit numbers have no interior digit.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to count numbers by length. For lengths shorter than m's digit count, count every valid arrangement. For the same length, walk m's digits left to right and count prefixes that stay under m. Distinct digits means you track a used-digit bitmask of 9 bits. No valley means you track the last digit and whether the previous step went up or down. A valley is down then up, so once you've gone down you can only keep going down. Pitfall: forgetting that 1 and 2 digit numbers have no interior digits, so only distinctness and nonzero apply. Another one is off-by-one on the bound, since m itself counts if it's good. Memoize on (position, mask, last, direction, tight). If you freeze on the state design during the live OA, StealthCoder is the hedge.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Count Good Numbers 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Good Numbers FAQ
What's the trick in Count Good Numbers?+
Treat it as digit DP. Track position, a bitmask of used digits, the last digit, and whether you've started descending. A valley means down then up, so after any descent only descents are legal. Add a tight flag for the upper bound m.
How hard is this one really?+
Harder than a typical medium because three constraints stack. But distinct nonzero digits caps the length at 9, so the state space is tiny. Once you see digit DP, the code is short. Spotting it is the hard part.
Can I just brute force it?+
Only if m is small. Checking every integer up to m works for the example, but if m can be large you'll time out. Since valid numbers have at most 9 digits, a DP or a DFS that builds valid numbers under m is the safer route.
How do I handle the no-interior-valley rule?+
Keep a state for direction: still ascending or already descending. When adding a digit, if you're descending, the new digit must be smaller than the last. If ascending, it can be larger, or smaller which flips you to descending. Going up after down is forbidden.
How do I prep for this in 48 hours?+
Write one digit DP from scratch with a tight flag and a bitmask, like counting numbers with unique digits. Then add the direction state. Test on m = 21, which should give 18. Check edge cases like m = 9 and m = 10.