Given an array of integers nums, return how many of them have an even number of digits. With nums = [12, 345, 2, 6, 7896] the answer is 2, because only 12 and 7896 have an even digit count.
The most readable answer is to turn each number into a string and check its length.
def findNumbers(nums):
return sum(1 for n in nums if len(str(n)) % 2 == 0)
This is O(n * d) time, where d is the number of digits, and it is what most interviewers expect first.
If the interviewer asks you to avoid strings, count digits by repeated integer division.
def digits(n):
count = 0
while n > 0:
n //= 10
count += 1
return count
The constraints say 1 <= nums[i] <= 10^5. So a number has an even digit count only if it is in 10..99, 1000..9999, or exactly 100000. That turns each check into two comparisons, which is a nice way to show you read the constraints.
Say the simple version out loud first, then offer the follow-ups: what about negative numbers (take the absolute value), and what about zero (it has one digit, so the division loop needs a special case). Small edge cases like these are where easy questions are actually graded.
If you want to practice explaining solutions like this under time pressure, Linkjob AI is an AI interview assistant that helps with live coding and behavioral rounds. (Disclosure: I work on Linkjob AI.)