In this "Easy" coding problem, you are given two non-negative integers, low and high. Your task is to count how many odd numbers exist in the inclusive range .
Companies like Microsoft and Google use this as a very basic "sanity check" or a warm-up question. It tests whether a candidate can think mathematically and avoid an loop. In programming interviews, even simple problems often have an solution that interviewers expect you to find.
The pattern is Simple Math / Parity Logic. Instead of iterating through the range, you can calculate the count using the formula: (high + 1) / 2 - low / 2. Alternatively, you can find the number of integers in the range () and adjust based on whether the boundaries are odd or even.
Range: [3, 7]
The most common mistake is using a loop to count the numbers, which is unnecessary and inefficient for large ranges (e.g., to ). Another mistake is incorrect integer division or "off-by-one" errors when applying the mathematical formula.
Always look for solutions for problems involving ranges and counts. If the property you are counting (like being odd) alternates perfectly, there is almost always a formulaic way to find the answer.