Facebook Pixel

sum(1)(2)(3) Interview Tips for Amazon

Tips for solving the Amazon sum question in an interview.

sum(1)(2)(3) Interview Tips for Amazon

Tips

  1. Recognize it as currying: the question tests currying and closures.
  2. Start with the simple solution: terminate with an empty call.
  3. Explain how closures work: each returned function closes over the accumulated sum.
  4. Mention toString as an alternative: shows deeper JavaScript knowledge.
  5. Handle the follow-up: support multiple arguments with rest params.

Step-by-Step Approach

  1. Identify that each call returns a function (currying).
  2. The function needs to accumulate the sum (closure).
  3. It needs a way to terminate (empty call or toString).
  4. Write the basic solution.
  5. Explain how it works step by step.
  6. Handle follow-up questions (multiple args, other operations).

Common Mistakes

  1. Not returning a function: forgetting to return the inner function.
  2. Not accumulating: losing the previous value.
  3. No termination: no way to get the final result.
  4. Overcomplicating: trying to use a class or complex state management.

The Takeaway

For the Amazon sum question: recognize currying, start with the empty-call solution, explain closures, mention toString as an alternative, and handle follow-ups (multiple args). Common mistakes: not returning a function, not accumulating, no termination, overcomplicating.

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).

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.

How would you support multiple arguments (sum(1, 2)(3)), how would you add other operations (multiply), how would you make it a generic curry, and what is the time/space complexity.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.