Definition
- Stepping loop – start at 20, step by 2 up to 40, accumulating each value into a sum seeded at 0.
- Arithmetic series formula –
count * (first + last) / 2, where count =(last - first) / step + 1. - Term count – for evens from 20 to 40:
(40 - 20) / 2 + 1 = 11terms. - Average of first and last –
(20 + 40) / 2 = 30. - Total –
11 * 30 = 330. - Off-by-one – both 20 and 40 are included, so the +1 in the count is essential.
National Bank of India sums processing fees charged on even-numbered account tiers from tier 20 to tier 40. The system starts at 20 and adds every second tier, or uses the formula to get 330 instantly.
Understanding the Concepts
What is the problem?
Calculate the sum of all even numbers from 20 to 40 inclusive. The terms are 20, 22, 24, ..., 40. Expected output: 330.
How do you implement the loop?
package com.bank.service;
public class RangeEvenSum {
public static long sumEvensLoop(int start, int end) {
long sum = 0;
for (int i = start; i <= end; i += 2) {
sum += i;
}
return sum;
}
public static long sumEvensFormula(int first, int last, int step) {
int count = (last - first) / step + 1;
return (long) count * (first + last) / 2;
}
public static void main(String[] args) {
System.out.println("Loop: " + sumEvensLoop(20, 40));
System.out.println("Formula: " + sumEvensFormula(20, 40, 2));
}
}
Loop: 330
Formula: 330
How does the formula work?
- For an evenly spaced arithmetic series:
- First term: 20
- Last term: 40
- Step: 2
- Count: (40 - 20) / 2 + 1 = 11 terms
- Average of first and last: (20 + 40) / 2 = 30
- Sum: 11 * 30 = 330
The formula computes in O(1) regardless of range size.
Why is the +1 in the count essential?
- Both 20 and 40 are included. Without the +1:
- (40 - 20) / 2 = 10 (wrong: misses one endpoint)
- (40 - 20) / 2 + 1 = 11 (correct: counts both endpoints)
This off-by-one is the single most common mistake in range-sum problems.
Why step by 2 instead of filtering?
| Feature | Step by 2 | Filter with % |
|---|---|---|
| Iterations | (end-start)/2 + 1 | end - start + 1 |
| Per-iteration test | none | modulo |
| Correctness | guaranteed even | depends on test |
| Simplicity | very clear | more code |
Stepping by 2 is cleaner and faster.
What are edge cases?
- Start > end: empty range, sum is 0.
- Start == end and even: sum is that single value.
- Start is odd: adjust to the next even before beginning. For this problem, 20 is even.
What are common mistakes?
// Wrong: stepping by 1 and filtering
for (int i = 20; i <= 40; i++) {
if (i % 2 == 0) sum += i; // works but twice the iterations
}
// Wrong: off-by-one in formula count
int count = (40 - 20) / 2; // 10 instead of 11!
// Correct
int count = (40 - 20) / 2 + 1; // 11
How do you verify correctness?
Run both the loop and the formula and assert they agree. This cross-check catches off-by-one errors in either approach.
assert sumEvensLoop(20, 40) == sumEvensFormula(20, 40, 2);
What should a senior engineer know?
The arithmetic-series formula generalizes to any evenly spaced sequence. The sum of odd numbers, multiples of 3, or any fixed stride is the same formula with different parameters. Knowing this replaces loops with constant-time calculations wherever an arithmetic series appears.
For money, accumulate with BigDecimal in the loop or use exact integer arithmetic in the formula. The division by 2 is always exact for the sum of an arithmetic series because count * (first + last) is always even.
What are common interview follow-up questions?
- How would you sum even numbers in a dynamic range [A, B] where A might be odd?
- What is the formula for the sum of the first N even numbers? (N * (N + 1))
- How would you generalize to sum multiples of K in a range?
Visual Overview
Terms: 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40
Count: 11
Loop: sum = 0
+20=20, +22=42, +24=66, +26=92, +28=120,
+30=150, +32=182, +34=216, +36=252, +38=290, +40=330
Formula: 11 * (20 + 40) / 2 = 11 * 30 = 330
Key Takeaway
Summing the even numbers between 20 and 40 inclusive is an arithmetic-series sum over a stepped range. The loop starts at 20, steps by 2 up to 40, and adds each value. The terms 20, 22, ..., 40 sum to 330.
The senior insight is the closed form: the sum of an arithmetic series is the count of terms times the average of the first and last term. Here the count is (40 - 20) / 2 + 1 = 11, and the average is (20 + 40) / 2 = 30, so the sum is 11 * 30 = 330 in constant time. The +1 in the count is essential because both endpoints are included, and missing it is the most common mistake.
Verifying the loop and the formula against each other catches off-by-one errors in either approach. Stepping by 2 is cleaner and faster than iterating every integer and filtering with modulo.
In a banking project at National Bank of India, fees were rolled up across even-numbered account tiers from tier 20 to tier 40. The RangeEvenSum class offered both a loop and a formula, and both produced 330.
Remember one sentence: the sum of an arithmetic series is count * (first + last) / 2, and the count for inclusive bounds needs the +1.
---