Skip to main content

Function Composition - Solution & Explanation

Easy12 min readAsked at: Amazon, Microsoft, Meta +2
Practice this problem

Problem Statement

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.

 

Example 1:

Input: functions = [x => x + 1, x => x * x, x => 2 * x], x = 4
Output: 65
Explanation:
Evaluating from right to left ...
Starting with x = 4.
2 * (4) = 8
(8) * (8) = 64
(64) + 1 = 65

Example 2:

Input: functions = [x => 10 * x, x => 10 * x, x => 10 * x], x = 1
Output: 1000
Explanation:
Evaluating from right to left ...
10 * (1) = 10
10 * (10) = 100
10 * (100) = 1000

Example 3:

Input: functions = [], x = 42
Output: 42
Explanation:
The composition of zero functions is the identity function

 

Constraints:

  • -1000 <= x <= 1000
  • 0 <= functions.length <= 1000
  • all functions accept and return a single integer

Approach Overview

Problem Overview: You receive an array of functions and must return a single function representing their composition. When the returned function is called with value x, it should apply the functions from right to left, meaning the last function runs first and its result flows through the remaining functions.

Approach 1: Iterative Right-to-Left Composition (O(n) time, O(1) space)

Store the input value in a variable and iterate through the function array from the last index to the first. At each step, call the current function using the running value and overwrite the result. This mirrors mathematical function composition: f(g(h(x))). The key idea is that the output of one function becomes the input of the next as you move left in the array. Time complexity is O(n) per invocation of the composed function because each function is executed exactly once, while auxiliary space stays O(1) since only a single variable stores intermediate results. This approach is straightforward and avoids recursion overhead.

Approach 2: Recursive Right-to-Left Composition (O(n) time, O(n) space)

Recursion models composition naturally. Start from the last function and recursively apply the remaining functions until reaching the first. Each recursive call passes the result of the current function to the previous one. Conceptually, this builds a call stack similar to nested expressions like f(g(h(x))). The total runtime remains O(n) because each function executes once. However, recursion introduces O(n) space complexity due to the call stack. This style is common when working with recursion or functional pipelines, but many engineers prefer the iterative approach for clarity and stack safety.

Both solutions rely on simple traversal of the array of functions. The composition concept itself comes from functional programming, where functions are treated as first-class values and combined to build pipelines.

Recommended for interviews: The iterative right-to-left approach. It clearly demonstrates understanding of function composition while keeping space usage constant. The recursive version shows conceptual clarity but adds unnecessary stack overhead for this problem.

Approach 1: Iterative Right-to-Left Composition

Iterative Approach: In this approach, we compose the functions iteratively from the last function to the first. This is because function composition works in reverse order, i.e., f(g(h(x))) means first apply h, then g, and finally f. We start with the input value x and apply each function in the function list from right to left.

This C solution defines an array of function pointers and iterates over the functions from right to left, applying each one to the initial input. Each function is applied in turn, modifying the input, and the final result is returned.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) where n is the number of functions.
Space Complexity: O(1) since we are using a fixed amount of extra space.

Try this approach in the editor →

Approach 2: Recursive Right-to-Left Composition

Recursive Approach: This approach encapsulates the recursive function composition in a way that it applies the last function first and makes a recursive call to apply the rest. We either call the next composed function recursively until the base case, i.e., no functions left, is reached, or upon an empty function list, return the input as it is simply the identity function.

This C implementation defines a recursive function which applies the last function in the array and then calls itself with the remaining functions one less in length, composed into a single final result.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) where n is the number of functions.
Space Complexity: O(n) due to the recursive call stack.

Try this approach in the editor →

Approach 3: Default Approach

Code

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Iterative Right-to-Left Composition

Time Complexity: O(n) where n is the number of functions.
Space Complexity: O(1) since we are using a fixed amount of extra space.

Recursive Right-to-Left Composition

Time Complexity: O(n) where n is the number of functions.
Space Complexity: O(n) due to the recursive call stack.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative Right-to-Left CompositionO(n)O(1)Best general solution; avoids recursion overhead and processes each function once
Recursive Right-to-Left CompositionO(n)O(n)Useful for demonstrating recursive composition patterns or functional-style implementations

Video Solution

Function Composition - Leetcode 2629 - JavaScript 30-Day Challenge • NeetCodeIO • 19,231 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Function Composition easy or hard?
Function Composition is classified as an Easy problem. The main requirement is understanding how function outputs flow into subsequent function calls, combined with simple array traversal.
Function Composition Python/Java solution
In Python or Java, store the functions in a list or array and apply them from the last index to the first when the returned function executes. JavaScript solutions typically return a closure that performs this loop when called.
How to solve Function Composition in O(n)?
Start with the input value x and iterate through the function array from right to left. For each index i, update the value with functions[i](currentValue). After processing all functions, return the final value. This executes n function calls, giving O(n) time complexity.
What is the best approach for Function Composition?
The iterative right-to-left composition approach is the most practical solution. Iterate from the last function to the first and repeatedly apply each function to the running value. This processes all functions in O(n) time with O(1) extra space.
Is Function Composition asked at Google/Amazon/Meta?
Function composition problems appear in interviews that test JavaScript fundamentals or functional programming concepts. Variants of this question show up in front-end or full-stack interviews where candidates must demonstrate understanding of higher-order functions.
What data structure is used in Function Composition?
The problem primarily uses an array to store the list of functions. The algorithm simply traverses the array and applies each function sequentially, passing the result along the chain.
What is the time complexity of Function Composition?
Each invocation of the composed function calls every function in the array exactly once. If there are n functions, the total runtime is O(n). The iterative solution uses O(1) extra space, while the recursive approach requires O(n) stack space.

Ready to solve this problem?

Practice Function Composition with our built-in code editor and test cases.

Practice on FleetCode