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

Leetcode #2795: Parallel Execution of Promises for Individual Results Retrieval

In this guide, we solve Leetcode #2795 Parallel Execution of Promises for Individual Results Retrieval 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 an array functions, return a promise promise. functions is an array of functions that return promises fnPromise.

Quick Facts

  • Difficulty: Medium
  • Premium: Yes
  • Tags: JavaScript

Intuition

We want to run all async tasks in parallel, but keep each result alongside its success or failure status.

If we gather all tasks and capture exceptions, we can produce a Promise.allSettled-style list.

Approach

Wrap each function call so synchronous errors are captured, then await all of them concurrently. Map each outcome to a {"status": ..., "value"/"reason": ...} object.

Steps:

  • For each function, call it inside a wrapper that awaits its result and catches errors.
  • Use asyncio.gather to run wrappers in parallel.
  • Convert each outcome into a fulfilled or rejected record.

Example

Input: functions = [ () => new Promise(resolve => setTimeout(() => resolve(15), 100)) ] Output: {"t":100,"values":[{"status":"fulfilled","value":15}]} Explanation: const time = performance.now() const promise = promiseAllSettled(functions);                 promise.then(res => { const out = {t: Math.floor(performance.now() - time), values: res} console.log(out) // {"t":100,"values":[{"status":"fulfilled","value":15}]} }) The returned promise resolves within 100 milliseconds. Since promise from the array functions is fulfilled, the resolved value of the returned promise is set to [{"status":"fulfilled","value":15}].

Python Solution

import asyncio import inspect async def promiseAllSettled(functions): async def run(fn): try: res = fn() if inspect.isawaitable(res): res = await res return res except Exception as exc: return exc results = await asyncio.gather(*(run(fn) for fn in functions)) out = [] for res in results: if isinstance(res, Exception): out.append({"status": "rejected", "reason": res}) else: out.append({"status": "fulfilled", "value": res}) return out

Complexity

The time complexity is O(n)O(n)O(n) plus the underlying task time, and the space complexity is O(n)O(n)O(n).

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