Magicsheet logo

Find Numbers with Even Number of Digits

Easy
87.2%
Updated 6/1/2025

Find Numbers with Even Number of Digits

What is this problem about?

The Find Numbers with Even Number of Digits interview question is a fundamental array task. You are given an array of integers and need to count how many of them contain an even number of digits (e.g., 12 has two digits, 123 has three). This Find Numbers with Even Number of Digits coding problem is a great starting point for beginners to practice numeric analysis.

Why is this asked in interviews?

Companies like Quora and Microsoft use this as a warm-up. It tests your ability to perform a linear scan and your knowledge of integer manipulation. You can solve it by converting numbers to strings or by using mathematical operations like logarithms or repeated division.

Algorithmic pattern used

This problem follows the Linear Scan pattern.

  1. Iterate: Loop through every number in the array.
  2. Digit Counting: Determine the length of the number.
    • String Method: Convert to string and check length.
    • Math Method: Repeatedly divide by 10 until 0.
    • Log Method: floor(log10(n))+1floor(log10(n)) + 1.
  3. Parity Check: If the length is divisible by 2, increment the counter.

Example explanation

Array: [12, 345, 2, 6, 7896]

  • 12: 2 digits. (Even). Count = 1.
  • 345: 3 digits. (Odd).
  • 2: 1 digit. (Odd).
  • 6: 1 digit. (Odd).
  • 7896: 4 digits. (Even). Count = 2. Result: 2.

Common mistakes candidates make

  • Off-by-one: Mistakes in the digit counting loop (e.g., not counting the last digit).
  • Efficiency: Using string conversion if the interviewer specifically asks for a mathematical solution.
  • Negative numbers: Forgetting to handle negative signs if the input constraints allow them (though usually they are positive).

Interview preparation tip

Be ready to discuss the trade-offs between string conversion and mathematical methods. String conversion is often easier to read, but mathematical division is generally more memory-efficient. This shows you care about Array interview pattern performance.

Similar Questions