Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Promise Queue Class With Retries

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

Your question is Promise Queue Class With Retries. 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

The Farmer's Dog processes delivery-related jobs concurrently, but each job may fail and need to be retried. Implement a bounded job queue that starts with no active jobs, runs at most capacity attempts at once, records successful completion order, and retries failures up to max_retries times.

Because this is a deterministic coding exercise, each job provides the planned result and duration of every attempt. An attempt is an object with duration, a nonnegative integer, and success, a boolean. Jobs are initially queued in input order. When multiple attempts finish at the same time, process them in the order they were started. A retry is placed at the back of the queue after its failed attempt completes. New work is started only after all attempts finishing at the current time have been processed.

Formal Specification

Implement run_job_queue(jobs, capacity, max_retries). Return an object with completed, the IDs of jobs that eventually succeed in completion order, and failed, the IDs of jobs that exhaust their attempts without success. Each job has the form {"id": string, "attempts": [{"duration": int, "success": bool}, ...]}. The first attempt is always available, and each later attempt is used only after the previous attempt fails.

Constraints

  • 1 <= len(jobs) <= 10^5
  • 1 <= capacity <= len(jobs)
  • 0 <= max_retries <= 10
  • Every job has at least one attempt and at most max_retries + 1 attempts
  • 0 <= attempt.duration <= 10^6
  • Job IDs are unique

Function Signature

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