Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given an array nums of integers, return how many of them contain an even number of digits.
Example 1:
Input: nums = [12,345,2,6,7896] Output: 2 Explanation: 12 contains 2 digits (even number of digits). 345 contains 3 digits (odd number of digits). 2 contains 1 digit (odd number of digits). 6 contains 1 digit (odd number of digits). 7896 contains 4 digits (even number of digits). Therefore only 12 and 7896 contain an even number of digits.
Example 2:
Input: nums = [555,901,482,1771] Output: 1 Explanation: Only 1771 contains an even number of digits.
Constraints:
1 <= nums.length <= 5001 <= nums[i] <= 105Problem summary: Given an array nums of integers, return how many of them contain an even number of digits.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[12,345,2,6,7896]
[555,901,482,1771]
finding-3-digit-even-numbers)number-of-even-and-odd-bits)find-if-digit-game-can-be-won)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1295: Find Numbers with Even Number of Digits
class Solution {
public int findNumbers(int[] nums) {
int ans = 0;
for (int x : nums) {
if (String.valueOf(x).length() % 2 == 0) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #1295: Find Numbers with Even Number of Digits
func findNumbers(nums []int) (ans int) {
for _, x := range nums {
if len(strconv.Itoa(x))%2 == 0 {
ans++
}
}
return
}
# Accepted solution for LeetCode #1295: Find Numbers with Even Number of Digits
class Solution:
def findNumbers(self, nums: List[int]) -> int:
return sum(len(str(x)) % 2 == 0 for x in nums)
// Accepted solution for LeetCode #1295: Find Numbers with Even Number of Digits
impl Solution {
pub fn find_numbers(nums: Vec<i32>) -> i32 {
nums.iter()
.filter(|&x| x.to_string().len() % 2 == 0)
.count() as i32
}
}
// Accepted solution for LeetCode #1295: Find Numbers with Even Number of Digits
function findNumbers(nums: number[]): number {
return nums.filter(x => x.toString().length % 2 === 0).length;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.