How do I explain the sum(1)(2)(3) solution in an interview?
Step by step: sum(1) returns a function with a=1. Calling it with 2 returns sum(3) (a new function with a=3). Each call accumulates. Calling with no argument returns the accumulated sum. Each step uses a closure.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in sum(1)(2)(3) Interview Tips for Amazon
Recognize it as currying. Start with the simple solution (terminate with empty call). Explain how closures accumulate the sum. Mention toString as an alternative. Handle follow-ups like multiple arguments.
Not returning a function (the chain breaks), not accumulating the previous value, no termination mechanism (no way to get the result), and overcomplicating (using classes or complex state instead of a simple closure).
Yes. After the basic solution, mention that you could also use toString or valueOf for automatic type coercion. This shows deeper JavaScript knowledge (type coercion, toString, valueOf).
Still have questions?
Browse all our FAQs or reach out to our support team
