Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2797: Partial Function with Placeholders

In this guide, we solve Leetcode #2797 Partial Function with Placeholders in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

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.

Quick Facts

  • Difficulty: Easy
  • Premium: Yes
  • Tags: JavaScript

Intuition

We need to fill placeholders in order, then append any remaining arguments.

A single pass through args while consuming restArgs achieves this cleanly.

Approach

Build a new argument list by replacing each "_" with the next value from restArgs. After the scan, append any remaining restArgs, then call fn.

Steps:

  • Iterate over args, replacing "_" from an iterator over restArgs.
  • Append leftover restArgs to the end.
  • Call fn with the final argument list.

Example

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.

Python Solution

class Solution: def partial(self, fn, args): def partial_fn(*restArgs): rest_iter = iter(restArgs) merged = [] for a in args: if a == "_": try: merged.append(next(rest_iter)) except StopIteration: merged.append("_") else: merged.append(a) merged.extend(list(rest_iter)) return fn(*merged) return partial_fn

Complexity

The time complexity is O(n+m)O(n + m)O(n+m), and the space complexity is O(n+m)O(n + m)O(n+m).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy