Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Loop summing `i*i` into a `long`, or use the formula `n*(n+1)*(2n+1)/6` for O(1).
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you know the closed form and guard overflow.
Where candidates slip
Int overflow, or an O(n) loop when O(1) exists.
How to stand out
State and (optionally) justify the n(n+1)(2n+1)/6 identity.
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 squares
1Hint 1Explore hintHide hint
Accumulate i*i — but is there a formula that skips the loop?
2Hint 2Explore hintHide hint
Sum of squares = n(n+1)(2n+1)/6.
3Hint 3Explore hintHide hint
Use a long accumulator; squares overflow int quickly.
A · Core idea
Sum 1² + 2² + … + n². The direct way accumulates i*i in a loop; the elegant way uses the closed form n(n+1)(2n+1)/6 for O(1).
Why a long accumulator. Squares grow fast: for n = 10⁴ the sum is ~3.3×10¹¹, well past int range. Accumulate in long (and cast i*i to long) to avoid overflow.
The closed form. Just as 1..n sums to n(n+1)/2, the sum of squares has the identity n(n+1)(2n+1)/6 — provable by induction. It turns the O(n) loop into a handful of multiplications. Knowing it (and that the /6 is always exact) is the standout.
Inclusive bound. Loop i from 1 to n inclusive; the usual off-by-one applies.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 3i=1
+1
total=114C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
The closed form is the standout: O(1) instead of O(n).
ComplexityO(n) time, O(1) space — the loop is O(n); the closed form is O(1) — a fixed number of multiplications.
E · Edge cases and pitfalls5n = 1 — 1² = 1.Explore edge casesHide edge cases
Edge cases handled
n = 11² = 1.n = 555.n = 10000~3.3×10¹¹ — needs long.
Pitfalls
- Overflowing an int accumulator.
- Off-by-one in the loop bound.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic long sumSquares(int n) { long total = 0; for (int i = 1; i <= n; i++) { total += (long) i * i; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Overflowing an int accumulator.
- Off-by-one in the loop bound.
Revise
Carry the pattern into the interview
Pattern to remember
Accumulate / closed form
Code pattern to memorize
sum i*i == n(n+1)(2n+1)/6Rules to remember
- 1Sum i*i in long.
- 2Formula: n(n+1)(2n+1)/6.
- 3Inclusive bound.
Use this pattern when
- Power sums
- Replacing loops with closed forms
Interview takeaway
Many sums have a closed form that beats the loop.
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 / closed form. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups