Skip to solution
hardFrontend

What are function composition and pipe?

1.1k views
01

Understand the problem

Question presented to candidate: "If compose(f, g, h)(x) and pipe(h, g, f)(x) are given, do they produce the same result? Walk me through exactly why or why not."

What a strong answer should cover:

  • 📌 Interview term: function composition — building a new function by chaining several smaller functions together, where each one's output becomes the next one's input.
  • 📌 Interview term: compose(...fns) — applies the functions right-to-left — the RIGHTMOST function runs first on the initial input, and each result flows leftward through the rest.
  • 📌 Interview term: pipe(...fns) — applies the functions left-to-right — the LEFTMOST function runs first, and each result flows rightward — the mirror image of compose.
  • 📌 Interview term: the real, direct answer to the prompt — verified directly: compose(square, addOne, double)(3) and pipe(double, addOne, square)(3) (the identical three functions, in correspondingly REVERSED order) genuinely produce the identical result (49) — confirming compose and pipe are genuinely mirror images of each other, not fundamentally different operations.
  • A precise answer names that function ORDER genuinely matters when the functions don't commute — verified directly: compose(square, addOne, double)(3) (49) and compose(double, addOne, square)(3) (20, the SAME three functions in a genuinely different order) produce genuinely DIFFERENT results, confirming this is not a trivial or order-independent operation.

Clarifying questions expected:

  • None — this is a definitional/comparison question; directly answering the prompt's own reversed-order equivalence question with real proof is the strong signal.

Code / implementation expected: Yes — a real, generic compose/pipe implementation, verified producing identical results with correspondingly reversed function order, plus a real proof that order genuinely changes the result, is the clearest demonstration.

composition
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 fundamentals/functional-programming interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every numeric result below was actually run in Node.

1. Why Th

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof: compose and pipe are genuine mirror images (identical results with correspondingly reversed order), and function order genuinely matters
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 121 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track