Schedule Priority Jobs with Cooldowns
Implement a scheduler for recurring jobs:
addJob(job_id, priority, cooldown) -> bool
getJob() -> job_id or None
addJob inserts a new job and returns false if the ID already exists. Every successful getJob call selects the eligible job with the greatest priority; ties are broken by earlier insertion. The selected job remains registered but becomes ineligible for its configured number of subsequent getJob invocations. Calls that return None still count toward cooldown progress.
For example, a job with cooldown 3 selected on call 5 cannot be selected on calls 6, 7, or 8 and becomes eligible on call 9. A cooldown of 0 permits selection on the next call.
Constraints
-
Up to
200000
total operations.
-
Priorities and cooldowns are non-negative integers.
-
Job IDs are unique non-empty strings.
-
Avoid scanning every registered job on each call.
Example
Add A with priority 10 and cooldown 2, then B with priority 5 and cooldown 0. Four calls to getJob return A, B, B, A.
Clarifications
Insertion order never changes after a job runs. Define the invocation counter precisely and explain how jobs move between waiting and eligible structures.
Hints
One priority structure can hold eligible jobs while another structure orders cooling jobs by the invocation when they become eligible.
Extensions
-
Support priority updates and job removal.
-
Use wall-clock release times instead of invocation counts.
-
Make
getJob
safe for concurrent workers without returning one job twice.