Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Sum with a `long`, then divide by `nums.length` as a `double` so you don't lose the fraction.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you recognise integer-division truncation and cast at the right moment.
Where candidates slip
Computing `total / n` in ints and losing the fraction.
How to stand out
Sum in long, cast to double before dividing, and mention the empty-array guard.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Sum then divide
1Hint 1Explore hintHide hint
Mean is sum over count — what type must the division happen in?
2Hint 2Explore hintHide hint
Integer division truncates: 10/4 becomes 2, not 2.5.
3Hint 3Explore hintHide hint
Cast one operand to double BEFORE dividing; sum in long to avoid overflow.
A · Core idea
The mean is sum / count. The whole subtlety is floating-point division: you must divide in a real-number type or the fraction is lost.
Why cast before, not after. In Java, total / nums.length with two integers does integer division first — 10 / 4 = 2 — and only then would a later cast widen 2 to 2.0. You must cast an operand before the division: (double) total / nums.length makes the whole division floating-point, yielding 2.5.
Why sum in `long`. The numerator can overflow int just like array-sum, so accumulate in long, then cast to double for the divide.
Empty input. Dividing by a zero length is undefined; guard it (return 0 or throw) if the contract allows empty arrays. Here it's guaranteed non-empty.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [1, 2, 3, 4]sum
1+2+3+4
total = 102.5C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
(double) total / n is the fix — cast before the division, not after.
ComplexityO(n) time, O(1) space — one pass to sum plus a single division — linear time, constant space.
E · Edge cases and pitfalls5nums = [5, 5, 5] — Returns 5.0; whole-number mean still works.Explore edge casesHide edge cases
Edge cases handled
nums = [5, 5, 5]Returns 5.0; whole-number mean still works.nums = [2, 3]5 / 2 = 2.5 — the case that exposes integer division.nums = []Division by zero — guard if empties are allowed.
Pitfalls
- Integer division truncating the fractional part.
- Dividing by zero on an empty array.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic double average(int[] nums) { long total = 0; for (int x : nums) total += x; return (double) total / nums.length; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1Integer division
`total / nums.length` with two ints truncates before any cast; 10/4 yields 2, not 2.5.
Explore the code comparisonHide the code comparison
Integer division
`total / nums.length` with two ints truncates before any cast; 10/4 yields 2, not 2.5.
Don't write this
double avg = total / nums.length; // truncates to 2Write this instead
double avg = (double) total / nums.length;Revise
Carry the pattern into the interview
Pattern to remember
Mean = sum / count
Code pattern to memorize
return (double) sum / n;Rules to remember
- 1Sum in long.
- 2Cast to double before dividing.
- 3Guard against empty input.
Use this pattern when
- Averages/ratios from integer counts
- Anywhere integer division would truncate
Interview takeaway
Cast before dividing, not after — order matters.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Mean = sum / count. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups