Coding questions · ByteDance
Count Numbers with an Even Number of Digits
Count how many positive integers in an array have an even number of decimal digits. Respect ordinary representations without leading zeroes, count repeated values independently, and handle boundaries such as 10 and 10000 correctly.
Problem
Given an array of positive integers, count how many elements have an even number of decimal digits.
Function Contract
Implement count_even_digit_numbers(numbers) -> int.
Constraints
1 <= len(numbers) <= 1000.1 <= numbers[i] <= 10000.- Use the ordinary base-10 representation without leading zeroes.
Examples
[12, 134, 111, 1111, 10]returns3.[7, 10000]returns0.
Hint: Count decimal places
Converting to a decimal string is sufficient under these bounds; repeated division by ten is another valid approach.
Edge Cases
- A one-digit number is not counted.
- The upper-bound value
10000has five digits. - Repeated values count separately.
Count Numbers with an Even Number of Decimal Digits
Hints
- Count decimal places by converting to a decimal string or repeatedly dividing by ten.
Approach
For each positive number, repeatedly divide by ten and count how many divisions are needed to reach zero. This is exactly its number of decimal digits. Increment the answer when that count is even. Processing elements independently naturally preserves multiplicity.
- Time complexity: O(n d), where d is at most 5 under the stated bounds.
- Space complexity: O(1) auxiliary space.