KatInterview

Prioritized Delayed Job Queue Simulator

Data Structure DesignAuto-graded · 21 test cases
MediumAuto-gradedData Structure Designheaphash-mapsimulation

Last updated

Simulate a delayed job queue by replaying a log of schedule, reschedule, cancel and poll operations, returning each result in order while respecting priority and time ordering.

Delayed Job Queue Replay

A job queue holds jobs that become runnable at a given time and carry a priority. You are given a log of operations to apply, in order, to an initially empty queue. Return the result of every operation.

Inputs

  • operations: a list of strings. Each is one of "schedule", "reschedule", "cancel", or "poll".
  • arguments: a list of the same length. arguments[i] holds the arguments for operations[i]:
    • "schedule": [job_id, priority, run_at]
    • "reschedule": [job_id, new_run_at]
    • "cancel": [job_id]
    • "poll": [now, limit]

Here job_id is a string, and priority, run_at, new_run_at, now and limit are integers.

Operation rules

A job is pending if it was scheduled and has neither been cancelled nor run. A job is executed once a poll has returned it.

  • schedule: If job_id is pending or already executed, return False and change nothing. Otherwise add it as pending with the given priority and run time, and return True. A job ID that was cancelled earlier may be scheduled again.

  • reschedule: If job_id is pending, set its run time to new_run_at, keep its original priority, and return True. Otherwise (unknown, cancelled or executed) return False.

  • cancel: If job_id is pending, remove it and return True. Otherwise return False.

  • poll: Consider the pending jobs whose run_at <= now. Order them by:

    1. higher priority first,
    2. then smaller run_at first,
    3. then lexicographically smaller job_id first.

    Take the first limit jobs in this order (if limit is zero or negative, take none). Mark them as executed, so they can no longer be cancelled, rescheduled, scheduled again or polled again. Return their IDs as a list in the order taken. If none qualify, return [].

The value of now is not guaranteed to increase between polls. Each poll must be judged only on the current pending jobs and its own now.

Output

Return a list with one entry per operation, in the same order: a boolean for schedule, reschedule and cancel, and a list of strings for poll.

Constraints

  • len(operations) == len(arguments), up to 10^5.
  • Job IDs are strings compared lexicographically.
  • Priorities and times are integers.
  • Every operation name is one of the four listed above.

Examples

Example 1

Input: replay_job_queue(['schedule', 'schedule', 'schedule', 'schedule', 'reschedule', 'cancel', 'poll', 'poll', 'cancel'], [['taskA', 1, 100], ['taskB', 2, 120], ['taskC', 2, 90], ['taskA', 5, 50], ['taskA', 80], ['taskC'], [100, 5], [150, 1], ['taskB']])

Output: [True, True, True, False, True, True, ['taskA'], ['taskB'], False]

Hints

Hint 1

Keep a map of pending jobs and a set of executed job IDs. Most of the return values for schedule, reschedule and cancel come directly from those two structures.

Hint 2

A poll needs two orderings: which jobs are due (by run time) and which due jobs go first (by priority, run time, ID). A simple correct start is to filter the due jobs and sort them with that key.

Hint 3

To scale to 10^5 operations, use heaps with lazy deletion. Tag each heap entry with a version number and discard entries whose version no longer matches the pending map. Because time may go backwards between polls, a due-heap entry that is not due for the current poll must be returned to the time-ordered heap.