KatInterview

Fault-Tolerant Task Dispatcher with Retry Limit and Dependencies

Data Structure DesignAuto-graded · 17 test cases
HardAuto-gradedData Structure Designsimulationqueuegraph

Last updated

Simulate a job queue where failed jobs retry up to a limit, then go to a dead-letter list, and failures cascade to jobs that depend on them.

Task Dispatcher Simulation

A dispatcher hands out jobs to workers. Workers can finish a job or report that it failed. A job that fails too many times is shelved permanently, and any job that needs a shelved job is shelved with it.

You are given a limit max_attempts and a script of operations to run in order. Return what each operation produces.

Operations

Operation i has the name operations[i] and the argument list arguments[i].

OperationArgumentsEffect
"submit"[job_id, payload] or [job_id, payload, deps]Registers a new job. deps is a list of job IDs that must be completed first. It may be missing or null, which means no dependencies.
"reserve"[]Gives the next eligible job to a worker (see below).
"complete"[job_id]Marks a currently reserved job as finished.
"fail"[job_id]Reports that a currently reserved job failed.
"dead_letters"[]Reports every shelved job so far.

Job life cycle

Each job starts with 0 failed attempts and is waiting. It can become reserved, complete, or shelved (permanently dead).

Queue order. The dispatcher keeps one waiting line. A job joins the back of the line when it is submitted, whether or not its dependencies are satisfied. A job whose retry is granted also rejoins the back of the line.

Eligibility. A waiting job is eligible only if every ID in its deps belongs to a job that is complete. A dependency ID may refer to a job that has not been submitted yet. In that case the dependency is not satisfied until that job is submitted and completed.

reserve. Scan the line from front to back and take the first waiting job that is eligible. Remove it from the line, mark it reserved, and output [job_id, payload]. Jobs that are skipped because they are not eligible keep their place in line. If no job is eligible, output null.

complete. If job_id is currently reserved, it becomes complete. Otherwise (unknown ID, or any other state) nothing happens.

fail. If job_id is currently reserved, increase its attempt count by one.

  • If the new count is less than max_attempts, the job goes back to waiting at the back of the line.
  • Otherwise the job is shelved.

If job_id is not currently reserved, nothing happens.

Shelving and cascading. When a job is shelved, every job that lists it in deps and is not already shelved or complete is shelved too, and so on through chains of dependents. The shelving order is breadth-first: the failing job first, then its direct dependents in the order they were submitted, then their dependents, and so on. A shelved job never runs again.

Also, if a job is submitted and any of its deps is already shelved, the new job is shelved immediately (and the cascade applies to jobs that were already waiting on this ID).

dead_letters. Output a list of all shelved job IDs in the order they were shelved.

Output

Return a list with one entry per operation:

  • submit, complete, fail: null (None).
  • reserve: [job_id, payload] or null.
  • dead_letters: a list of job IDs (possibly empty).

Constraints

  • 1 <= max_attempts.
  • Job IDs are strings and are unique among submit operations.
  • operations and arguments have the same length.
  • A job never depends on itself, and dependencies are acyclic.
  • complete and fail may name unknown jobs or jobs that are not reserved. These are silently ignored.
  • Payloads are opaque values returned exactly as submitted.

Examples

Example 1

Input: simulate_job_dispatcher(2, ['submit', 'submit', 'reserve', 'fail', 'reserve', 'fail', 'dead_letters', 'reserve'], [['cover', 'cover page'], ['bind', 'bind booklet', ['cover']], [], ['cover'], [], ['cover'], [], []])

Output: [None, None, ['cover', 'cover page'], None, ['cover', 'cover page'], None, ['cover', 'bind'], None]

Example 2

Input: simulate_job_dispatcher(3, ['submit', 'submit', 'reserve', 'reserve', 'complete', 'reserve', 'complete', 'dead_letters'], [['a', 'A'], ['b', 'B', ['a']], [], [], ['a'], [], ['b'], []])

Output: [None, None, ['a', 'A'], None, None, ['b', 'B'], None, []]

Example 3

Input: simulate_job_dispatcher(2, ['submit', 'submit', 'reserve', 'fail', 'reserve', 'complete', 'reserve', 'complete', 'dead_letters'], [['first', 'x', ['later']], ['later', 'y'], [], ['later'], [], ['later'], [], ['first'], []])

Output: [None, None, ['later', 'y'], None, ['later', 'y'], None, ['first', 'x'], None, []]

Hints

Hint 1

Track a state per job (waiting, reserved, complete, shelved) plus an attempt counter, and keep a separate list of the order in which jobs were shelved.

Hint 2

Keep a reverse map from each dependency ID to the jobs that list it, and fill it at submit time even when the dependency does not exist yet. Then a shelving event can walk straight to the affected jobs.

Hint 3

Do not remove shelved jobs from the waiting line. Leave them in place and check each job's current state during the front-to-back scan in reserve. Use a breadth-first queue for the cascade so the shelving order matches the statement.