Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Fold with a `long` accumulator starting at 0; add each element. Empty arrays naturally yield 0.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you fold from the identity and pick a type wide enough for the worst-case total.
Where candidates slip
Overflowing an int sum.
How to stand out
Quote the worst-case magnitude (10⁵ × 10⁹ = 10¹⁴) to justify long.
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
Accumulate
1Hint 1Explore hintHide hint
Carry a running total — what value should it start at?
2Hint 2Explore hintHide hint
Seed at 0 so an empty array returns 0 naturally.
3Hint 3Explore hintHide hint
Think about the type: 10⁵ values near 10⁹ overflow a 32-bit int.
A · Core idea
Summing an array is the accumulate fold: start a total at 0 (the additive identity) and add each element. Seeding at 0 makes the empty array return 0 with no special case.
Why a `long` accumulator. With up to 10⁵ elements each up to 10⁹ in magnitude, the true sum can reach 10¹⁴ — far beyond the 32-bit int ceiling of ~2.1×10⁹. Accumulating into an int silently overflows partway through and returns garbage. A 64-bit long holds the result safely.
Why seed at 0, not nums[0]. Seeding with the additive identity means the loop body is uniform (it adds every element) and the empty input falls out correctly. Seeding with nums[0] would double-count it unless you also skip index 0 — extra fiddliness for no gain.
This is the template for every 'total / mean / weighted sum' problem; the filtered variant (sum of evens) just adds an if inside the loop.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [1, 2, 3, 4]start
total = 0
010C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
Choosing long for the accumulator is the detail that separates a careful answer from a buggy one.
ComplexityO(n) time, O(1) space — one addition per element; a single accumulator means O(1) extra space.
E · Edge cases and pitfalls5nums = [] — Empty sum is 0 — the seed handles it.Explore edge casesHide edge cases
Edge cases handled
nums = []Empty sum is 0 — the seed handles it.nums = [-1, 5, -4]Mixed signs sum to 0.100000 elements of 10^9≈10^14 — fits in long, overflows int.
Pitfalls
- Using an int accumulator and overflowing on large arrays.
- Seeding the total with nums[0] and double-counting it.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic long sum(int[] nums) { long total = 0; for (int x : nums) { total += x; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Using an int accumulator and overflowing.
- Initialising the total to nums[0] and double counting.
Revise
Carry the pattern into the interview
Pattern to remember
Accumulate fold
Code pattern to memorize
total = 0; for x in nums: total += x (use long)Rules to remember
- 1Start at 0.
- 2Add each element.
- 3Use long for the total.
Use this pattern when
- Totals, means, weighted sums
- Any reduce-to-one-number scan
Interview takeaway
Sum folds start at the additive identity, 0.
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: Accumulate fold. Master it on small inputs and it transfers to harder problems.