Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Loop from 2 to n multiplying into an accumulator that starts at 1. Use `long`; 21! already overflows 64 bits.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you seed an accumulator correctly and reason about the type's overflow boundary.
Where candidates slip
Mishandling 0! or overflowing an int accumulator.
How to stand out
Quote the exact limits (12! overflows int, 20! is the last that fits in long) and prefer iteration to avoid stack risk.
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
Iterative product
1Hint 1Explore hintHide hint
Multiply every integer from 1 (or 2) up to n into a running product.
2Hint 2Explore hintHide hint
Seed the product at 1 so n = 0 returns 1 automatically.
3Hint 3Explore hintHide hint
Watch the type: factorials overflow fast — long only reaches 20!.
A · Core idea
n! is the product 1·2·…·n, with 0! defined as 1. Multiply an accumulator (seeded at 1) by each integer up to n.
Why seed the accumulator at 1. 1 is the multiplicative identity, so multiplying by it changes nothing — and it doubles as the correct answer for n = 0 and n = 1, where the loop body never runs or runs once. Seeding a product at 1 (like seeding a sum at 0) makes the empty/base case fall out for free.
Why iteration over recursion here. Factorial is the textbook recursion example, but an iterative loop avoids call-stack overhead and the risk of a deep-recursion stack overflow. For an interview, iteration is the safer default; mention recursion as the elegant-but-costlier alternative.
The hard limit: overflow. Factorials explode — 13! already overflows 32-bit int, and 21! overflows 64-bit long. So even with long, the usable range is n ≤ 20. Stating that boundary shows you reason about ranges, not just write loops.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 5i = 2
1 × 2
result = 2120C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
Calling out the overflow limit (20! fits in long, 21! doesn't) shows you think about ranges, not just logic.
ComplexityO(n) time, O(1) space — one multiplication per integer from 2 to n, so n−1 operations, and only one accumulator variable.
E · Edge cases and pitfalls5n = 0 — Loop runs zero times; the seed 1 is returned — correct, since 0! = 1.Explore edge casesHide edge cases
Edge cases handled
n = 0Loop runs zero times; the seed 1 is returned — correct, since 0! = 1.n = 1Loop runs zero times (starts at 2); returns 1.n = 21Overflows even long — the answer is outside the representable range.
Pitfalls
- Returning 0 for n = 0 instead of 1.
- Storing the product in int and silently overflowing past 12!.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic long factorial(int n) { long result = 1; for (int i = 2; i <= n; i++) { result *= i; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Returning 0 for n = 0 instead of 1.
- Storing the result in int and overflowing past 12!.
Revise
Carry the pattern into the interview
Pattern to remember
Iterative accumulation
Code pattern to memorize
result = 1; for i in 2..n: result *= iRules to remember
- 1Init accumulator to 1.
- 2Loop 2..n multiplying in.
- 3Use long; 21! overflows.
Use this pattern when
- Products over a range
- Any accumulate-with-identity fold
Choose another pattern when
- n large and exact result needed — switch to BigInteger
Interview takeaway
Seed a product with 1 so empty/zero cases fall out naturally.
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: Iterative accumulation. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Factorial (recursion)Coming soonSame product expressed as n × (n−1)! with a base case of 1.
- Power of a NumberAnother accumulate-a-product pattern, optimised with fast exponentiation.