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 fn. fn 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 functionargs and restArgs are valid JSON arrays1 <= args.length <= 5 * 1041 <= restArgs.length <= 5 * 1040 <= number of placeholders <= restArgs.lengthProblem 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.
TypeScript
JavaScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Two‑Pass Placeholder Replacement | O(n + m) | O(n) | When clarity is preferred and tracking placeholder indices makes reasoning simpler |
| Single‑Pass Argument Merge | O(n + m) | O(n) | Optimal approach for interviews and production utilities |
Valid Parentheses - Stack - Leetcode 20 - Python • NeetCode • 475,216 views views
Watch 9 more video solutions →Practice Partial Function with Placeholders with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor