Skip to the solution
HardFrontend6 min read

What are function composition and pipe?

1.1k views

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

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: compose and pipe are genuine mirror images (identical results with correspondingly reversed order), and function order genuinely matters

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 121 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

What is the difference between Promise.all, allSettled, race, and any?

Continue