04 / 09

Implement Currying using Recursion

Difficulty: 5/10
currying, recursion, functional programming
javascript
javascript

Scenario Questions

0-2 years experience

  1. 1We have a simple function `add(a, b)` that returns `a + b`. Show me how you'd create a curried version of `add` using recursion in JavaScript.
  2. 2If you call your curried `add` with only one argument, what does it return and why?
  3. 3What happens if you invoke the curried function with all arguments at once versus one at a time?

2-5 years experience

  1. 1Our utility `curry` is used across the codebase, but when a developer passes more arguments than the original function expects, we sometimes get unexpected results. Walk me through how you'd debug the recursive curry implementation and adjust it to safely ignore extra arguments.
  2. 2We need to expose a curried API for a data‑fetching function `fetchData(url, options, callback)`. Explain how you'd implement this using recursion and discuss any trade‑offs you see in terms of readability and error handling.
  3. 3During a code review you notice that a curried function loses the original `this` context when called as a method on an object. How would you modify the recursive curry to preserve `this`?

5-8 years experience

  1. 1You're leading a team building a functional utilities library that heavily relies on curried functions. At scale we see performance regressions when many curried functions are composed. How would you profile the recursive curry overhead and what optimizations could you apply?
  2. 2Our library must work in both Node.js and browser bundles, and we want the curry helper to be tree‑shakable. What design decisions would you make for the recursive implementation to keep bundle size low and avoid runtime penalties?
  3. 3We have a mix of synchronous and asynchronous functions that need to be curried. How would you adapt the recursive curry to handle functions that return promises without breaking the call‑chain semantics?

8+ years experience

  1. 1The organization is migrating a large legacy codebase to a functional style, replacing many multi‑parameter functions with curried versions. As a staff engineer, outline a migration plan that ensures backward compatibility, minimizes disruption, and addresses any pitfalls of a recursive curry implementation.
  2. 2Multiple teams need a shared currying utility that works with TypeScript, provides accurate type inference for any arity, and can be extended with custom wrappers. What architectural approach would you take to make this utility maintainable, performant, and easy to evolve across services?
  3. 3Consider a scenario where the curried utility becomes a public API used by external partners. How would you design versioning, deprecation, and testing strategies to evolve the recursive curry implementation without breaking existing consumers?

Follow-up Questions

  • How would you preserve the original function's name and length properties?
  • What changes would you make to avoid potential stack overflow with deep recursion?
  • Can you explain how your curry implementation interacts with JavaScript's `this` binding?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.