Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Promise Queue With Concurrency Limit

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Promise Queue With Concurrency Limit. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Write a class that accepts an array of anonymous functions that return promises and a number indicating how many jobs should run at once, and process the jobs concurrently.

Implement process_jobs(jobs, limit). For grading, each job is represented as {"result": value, "delay": milliseconds}; your implementation must create and run an asynchronous job for each descriptor. Return results in the original input order, even when completion order differs. At most limit jobs may run simultaneously. Assume all jobs resolve successfully and limit >= 1.

Constraints

  • 0 <= len(jobs) <= 10^4
  • 1 <= limit <= 10^4
  • Each job descriptor has a JSON-compatible result and a nonnegative integer delay in milliseconds
  • All jobs resolve successfully
  • Results must be returned in input order

Function Signature

def process_jobs(jobs, limit):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output