Function Composition - Problem
Given an array of functions [f1, f2, f3, ..., fn], return a new function fn that is the function composition of the array of functions.
The function composition of [f(x), g(x), h(x)] is fn(x) = f(g(h(x))).
The function composition of an empty list of functions is the identity function f(x) = x.
You may assume each function in the array accepts one integer as input and returns one integer as output.
Input & Output
Example 1 — Multiple Functions
$
Input:
functions = ["square", "addOne", "double"], x = 4
›
Output:
81
💡 Note:
Composition applies right-to-left: square(addOne(double(4))) = square(addOne(8)) = square(9) = 81
Example 2 — Single Function
$
Input:
functions = ["double"], x = 5
›
Output:
10
💡 Note:
Single function: double(5) = 10
Example 3 — Empty Array
$
Input:
functions = [], x = 42
›
Output:
42
💡 Note:
Empty function array returns identity function: f(x) = x, so result is 42
Constraints
- 0 ≤ functions.length ≤ 1000
- -1000 ≤ x ≤ 1000
- Each function accepts and returns integers
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code