FastPrepPriority Job Scheduler with Cooldowns
Problem · Heap

Priority Job Scheduler with Cooldowns

Learn this problem
MediumGoogle logoGoogleFULLTIMEONSITE INTERVIEW
See Google hiring insights

Problem statement

Simulate a scheduler over the finite sequence in operations. Each operation is one of:

  • ADD id priority cooldown: register one persistent job with a unique lowercase identifier, integer priority, and nonnegative cooldown.
  • GET: return the eligible job with the greatest priority. If several eligible jobs have the same priority, return the lexicographically smallest identifier. If no job is eligible, return NONE.

Only GET operations advance scheduler time. A newly added job is eligible immediately. After a job is returned by a GET, it remains registered but is ineligible for its next cooldown intervening GET operations. For example, if a job with cooldown 3 is returned on one call, three later GET calls must occur before it can be returned again. A GET that returns NONE still counts as an intervening call.

Return the result of every GET operation in order.

Function

runPriorityScheduler(operations: String[]) → String[]

Examples

Example 1

operations = ["ADD A 10 3","ADD B 5 0","GET","GET","GET","GET","GET"]return = ["A","B","B","B","A"]

Job A wins the first call by priority. Its cooldown requires calls two, three, and four to intervene, so A becomes eligible again on call five. Job B has cooldown 0 and fills the intervening calls.

Example 2

operations = ["GET","ADD beta 7 1","ADD alpha 7 2","GET","GET","GET","GET"]return = ["NONE","alpha","beta","NONE","alpha"]

The first call has no registered jobs. On the next call, alpha wins the priority tie lexicographically. It needs two intervening calls, while beta needs one after being selected. Both are cooling on the fourth overall GET, and alpha returns on the fifth.

Constraints

  • 1 <= operations.length <= 100000
  • Every operation is exactly GET or has the form ADD id priority cooldown.
  • Each identifier contains 1 through 20 lowercase English letters and is added exactly once.
  • 0 <= priority <= 10^9
  • 0 <= cooldown <= operations.length

More Google problems

drafts saved locally
public String[] runPriorityScheduler(String[] operations) {
    // Write your code here.
}
operations["ADD A 10 3","ADD B 5 0","GET","GET","GET","GET","GET"]
expected["A", "B", "B", "B", "A"]
checking account