Skip to main content

Partial Function with Placeholders - Solution & Explanation

EasyPremiumFree on FleetCode4 min read
Practice this problem

Problem Statement

Given a function fn and an array args, return a function partialFn

Placeholders "_" in the args should be replaced with values from restArgs starting from index 0. Any remaining values in the restArgs should be added at the end of the args.

partialFn should return a result of fnfn should be called with the elements of the modified args passed as separate arguments.

 

Example 1:

Input: fn = (...args) => args, args = [2,4,6], restArgs = [8,10]
Output: [2,4,6,8,10]
Explanation: 
const partialFn = partial(fn, args)
const result = partialFn(...restArgs) 
console.log(result) // [2,4,6,8,10]

There are no placeholders "_" in args therefore restArgs is just added at the end of args. Then the elements of the args are passed as separate arguments to fn, which returns passed arguments as an array.
Example 2:
Input: fn = (...args) => args, args = [1,2,"_",4,"_",6], restArgs = [3,5]
Output: [1,2,3,4,5,6]
Explanation: 
const partialFn = partial(fn, args) 
const result = partialFn(...restArgs) 
console.log(result) // [1,2,3,4,5,6] 

Placeholders "_" are replaced with values from the restArgs. Then the elements of the args are passed as separate arguments to fn, which returns passed arguments as an array.

Example 3:

Input: fn = (a, b, c) => b + a - c, args = ["_", 5], restArgs = [5, 20]
Output: -10
Explanation: 
const partialFn = partial(fn, args)
const result = partialFn(...restArgs)
console.log(result) // -10

Placeholder "_" is replaced with 5 and 20 is added at the end of args. Then the elements of the args are passed as separate arguments to fn, which returns -10 (5 + 5 - 20).

 

Constraints:

  • fn is a function
  • args and restArgs are valid JSON arrays
  • 1 <= args.length <= 5 * 104
  • 1 <= restArgs.length <= 5 * 104
  • 0 <= number of placeholders <= restArgs.length

Approach Overview

Problem Overview: Build a partial function that pre-fills arguments of another function while supporting placeholders. When the returned function is called, incoming arguments replace the placeholders in order, and any remaining arguments are appended before invoking the original function.

Approach 1: Two‑Pass Placeholder Replacement (O(n + m) time, O(n) space)

Start with the stored argument list from the partial call. Scan it to locate placeholder positions. When the returned function executes, iterate through those positions and replace them with values from the new argument list in order. If extra arguments remain after filling all placeholders, append them to the end of the argument list before calling fn(...args). This approach separates placeholder detection and replacement, making the logic straightforward but requiring an additional pass through the array.

Approach 2: Single‑Pass Merge of Arguments (O(n + m) time, O(n) space)

Process the stored argument array once while maintaining a pointer to the incoming arguments. For each element, check whether it equals the placeholder value (usually partial.placeholder). If it is a placeholder and unused runtime arguments exist, insert the next runtime value. Otherwise keep the original element. After finishing the scan, append any remaining runtime arguments. This produces the final argument list in one pass and avoids storing placeholder indices. The method behaves like a merge between two arrays.

The key idea behind both solutions is ordered substitution: placeholders consume new arguments sequentially. No complex data structure is required; an array scan is enough. Problems like this commonly appear when discussing JavaScript function utilities or functional programming patterns. The implementation mainly relies on predictable array traversal similar to common array manipulation tasks.

Recommended for interviews: The single‑pass merge approach. It demonstrates clean reasoning about argument ordering and minimizes unnecessary passes over the array. Mentioning the two‑pass idea still shows you understand the mechanics of placeholder tracking, but interviewers typically expect the concise single traversal solution.

Solution

Code

TypeScript

JavaScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Two‑Pass Placeholder ReplacementO(n + m)O(n)When clarity is preferred and tracking placeholder indices makes reasoning simpler
Single‑Pass Argument MergeO(n + m)O(n)Optimal approach for interviews and production utilities

Video Solution

Valid Parentheses - Stack - Leetcode 20 - Python • NeetCode • 475,216 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Partial Function with Placeholders easy or hard?
Partial Function with Placeholders is classified as an Easy problem. The main challenge is correctly preserving argument order while replacing placeholders. Once the substitution rule is clear, the implementation becomes a straightforward linear scan.
Partial Function with Placeholders Python/Java solution
The original problem targets JavaScript and TypeScript because placeholders are commonly used in functional utilities in those ecosystems. The same idea can be implemented in Python or Java by storing the initial arguments in a list and replacing placeholder markers when the wrapper function is called.
How to solve Partial Function with Placeholders in O(n)?
Traverse the stored argument array once while keeping a pointer to the new arguments. When a placeholder is encountered, substitute the next runtime value if available. Otherwise keep the original placeholder or value. After finishing the scan, append remaining runtime arguments and invoke the target function.
What is the best approach for Partial Function with Placeholders?
The most efficient method is a single-pass merge of stored arguments and runtime arguments. Iterate through the preset argument array and replace each placeholder with the next incoming argument. Any remaining arguments are appended at the end. This approach runs in O(n + m) time with O(n) additional space.
Is Partial Function with Placeholders asked at Google/Amazon/Meta?
This style of problem appears in interviews that test JavaScript utility design and functional programming concepts. Companies that emphasize frontend engineering, such as Meta or Google teams working with JavaScript infrastructure, sometimes ask variations of partial application or currying questions.
What data structure is used in Partial Function with Placeholders?
The solution mainly uses arrays to store and merge arguments. A pointer or index tracks the position in the runtime argument list while scanning the preset arguments. No advanced data structures are required beyond basic array traversal.
What is the time complexity of Partial Function with Placeholders?
The optimal implementation runs in O(n + m) time where n is the number of stored arguments in the partial call and m is the number of arguments provided during invocation. Each list is processed at most once. Space complexity is O(n) for building the final argument array passed to the function.

Ready to solve this problem?

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

Practice on FleetCode