Function Composition - Problem
Function Composition Challenge

In functional programming, function composition is a powerful concept where you combine multiple functions into a single function. Given an array of functions [f1, f2, f3, ..., fn], your task is to create a new function that represents the composition of all these functions.

๐ŸŽฏ The Goal: Return a composed function that applies all input functions from right to left. For example, if you have functions [f(x), g(x), h(x)], the composed function should work like f(g(h(x))) - first apply h, then g, then f.

โš ๏ธ Special Case: If the array is empty, return the identity function that simply returns its input unchanged: f(x) = x.

Each function in the array accepts one integer and returns one integer. Your composed function should do the same!

Input & Output

basic_composition.js โ€” JavaScript
$ Input: functions = [x => x * 2, x => x + 1, x => x * x], x = 4
โ€บ Output: 34
๐Ÿ’ก Note: The composition works as: multiply_by_2(add_one(square(4))) = multiply_by_2(add_one(16)) = multiply_by_2(17) = 34
single_function.js โ€” JavaScript
$ Input: functions = [x => x + 1], x = 5
โ€บ Output: 6
๐Ÿ’ก Note: With only one function, composition simply applies that function: add_one(5) = 6
empty_array.js โ€” JavaScript
$ Input: functions = [], x = 42
โ€บ Output: 42
๐Ÿ’ก Note: Empty array returns the identity function, so the input is returned unchanged: identity(42) = 42

Visualization

Tap to expand
4squarex โ†’ xยฒ16add_onex โ†’ x+117mult_2x โ†’ xร—23434Function Composition Pipelinef(g(h(x))) = multiply_by_2(add_one(square(4)))Flow Direction: Right-to-Left Function Application1. square(4) = 162. add_one(16) = 173. mult_2(17) = 34Functions are composed and applied from rightmost to leftmost
Understanding the Visualization
1
Input enters pipeline
Value 4 enters the composition pipeline
2
First transformation
Rightmost function (square) transforms 4 โ†’ 16
3
Second transformation
Middle function (add_one) transforms 16 โ†’ 17
4
Final transformation
Leftmost function (multiply_by_2) transforms 17 โ†’ 34
Key Takeaway
๐ŸŽฏ Key Insight: Function composition follows mathematical convention - read from right to left, creating elegant pipelines of transformations

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Still need to process each function once during composition and once during execution

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Creates nested function closures, but this is typically optimized by JavaScript engines

n
2n
โšก Linearithmic Space

Constraints

  • 0 โ‰ค functions.length โ‰ค 1000
  • All functions accept and return a single integer
  • -1000 โ‰ค x โ‰ค 1000 (input to composed function)
  • Functions are applied right-to-left (mathematical composition order)
  • Empty function array should return identity function f(x) = x
Asked in
Google 15 Meta 12 Netflix 8 Airbnb 6
67.2K Views
Medium Frequency
~15 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen