Count Digits 0, 2, and 4
Reported by candidates from Capital One's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Capital One OA, reported in September 2026, is forgetting that 0 counts as a number and that leading zeros don't count as digits. The task is simple on paper: count every appearance of digits 0, 2, and 4 across all integers from 0 through n. Numbers like 22 count twice. The stated limit allows O(n^2), so brute force is on the table. But the edge cases are where people lose points. If you blank under the timer, StealthCoder runs invisibly on your desktop as a safety net, so one bad minute doesn't end the attempt.
The problem
Given a non-negative integer n, count how many times the decimal digits 0, 2, and 4 appear across all integers from 0 through n, inclusive. Count every occurrence. For example, the number 22 contributes two occurrences of the digit 2. A solution with time complexity no worse than O(n^2) fits within the execution limit. Function solution(n: int) → int Examples Example 1 n = 10 return = 4 The digit 0 appears in 0 and 10, while 2 and 4 each appear once. The total is 2 + 1 + 1 = 4. Example 2 n = 22 return = 11 The digit 0 appears 3 times, the digit 2 appears 6 times, and the digit 4 appears 2 times. The total is 3 + 6 + 2 = 11. Constraints n is a non-negative integer. The answer fits in a signed 32-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The simplest path is a loop from 0 to n. For each number, peel off digits with modulo 10 and integer division, and bump a counter when the digit is 0, 2, or 4. Since the limit allows O(n^2), that's well within bounds. The pitfall is the number 0 itself. Its loop never runs if you write while x > 0, so you miss one zero. Handle it separately: if x is 0, add one. Check example 1 by hand. n = 10 gives 0 once, 10 once, 2 once, 4 once, total 4. The faster route is digit DP or per-position counting, which runs in O(log n), but you don't need it here. If the clean version slips your mind mid-assessment, StealthCoder is the hedge that reads the problem and hands you working code.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Count Digits 0, 2, and 4 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Capital One's OA.
Capital One reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Digits 0, 2, and 4 FAQ
What's the trick in the Count Digits 0, 2, and 4 problem?+
There's barely a trick. Loop 0 through n, extract digits with mod and divide, and count matches. The real trap is the number 0, which has one digit that is a zero. Handle it as a special case or use a do-while style loop.
How hard is this Capital One OA question really?+
Easy if you accept brute force, since the stated limit allows O(n^2). It gets harder only if you try a closed-form digit-position solution. For a timed OA, write the simple loop first, verify against both examples, then stop.
Why does n = 10 return 4?+
Zero appears in 0 and in 10, so that's two. The digit 2 appears once in the number 2. The digit 4 appears once in the number 4. Total is 2 + 1 + 1 = 4. If you got 3, you missed the standalone 0.
Do I need digit DP for this one?+
No. Digit DP or per-position counting gives O(log n), but the problem says O(n^2) is fine. Use it only if you already know it cold. A bug in a clever solution costs more than the speed gains you.
How do I prepare for this in 48 hours?+
Write the digit-extraction loop from memory until it's automatic. Test n = 0, n = 10, n = 22, and a number like 100 where zeros repeat. Also confirm you count every occurrence, not just distinct digits per number. That covers nearly every failure mode.