Problem · String
Count Digits 0, 2, and 4
Learn this problemProblem statement
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) → intExamples
Example 1
n = 10return = 4The digit 0 appears in 0 and 10, while 2 and 4 each appear once. The total is 2 + 1 + 1 = 4.
Example 2
n = 22return = 11The 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
nis a non-negative integer.- The answer fits in a signed 32-bit integer.
Source note: Additional source evidence from September 4 and 5, 2026 Capital One CodeSignal online assessment reports.