Skip to solution
hardDSA

How would you implement a curry() function that supports partial application with placeholder arguments?

255 views
01

Understand the problem

Question presented to candidate: "Implement a curry function that turns any function into one that can be called with its arguments spread across multiple calls — curry(add)(1)(2)(3) should equal add(1, 2, 3). Now extend it to support placeholders, so a caller can skip an argument in an early call and supply it in a later one — curry(add)(_, 2, 3)(1) should also equal add(1, 2, 3). How do you decide, at each call, whether there are finally enough real arguments to actually invoke the original function?"

What a strong answer should cover:

  • The curried function needs to know the target function's arity (how many arguments it expects) — usually fn.length, though that is wrong for variadic (rest-parameter) functions, so a real implementation should accept an explicit arity override.
  • At each call, the function is only actually invoked once there are at least arity arguments collected AND none of the first arity slots is still a placeholder — both conditions matter, not just the count.
  • A placeholder is typically a unique sentinel value (a Symbol, or a well-known exported constant like curry.placeholder, sometimes aliased to _) — never a plain string like "_", since that could collide with a genuine argument value.
  • When a follow-up call arrives, its new arguments should fill EXISTING placeholders first, left to right, and only append as new trailing arguments once every existing placeholder has been filled.
  • Multiple placeholders in the same call must each be individually fillable, potentially across multiple separate follow-up calls, not just in one single all-at-once fill.
  • A correct implementation is a straightforward extension of classic curry: the recursive/closure-returning structure stays the same, only the "are we done yet" check and the "how do we merge args" logic change to account for placeholders.

Clarifying questions expected:

  • "Should the placeholder be exported as part of the curry function itself, like curry.placeholder, or does the caller supply their own sentinel value?" — affects the public API shape.
  • "What happens if a follow-up call supplies MORE real values than there are placeholders left to fill?" — a genuine edge case worth resolving explicitly (the extra values are typically appended after the merged, placeholder-filled arguments).

Code / implementation expected: Yes — a full, runnable curry implementation with placeholder support, plus a real, multi-case test suite proving correct behavior for single, multiple, and progressively-filled placeholders.

functionaltrickyreal-world
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript functional-programming interview questions. Difficulty: Hard

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result shown below is real, captured output from actually runn

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JScurry() with placeholder support, plus a real 14-case test suite (run directly)
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 158 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track