FastPrepAd Score Scheduler With Delay
Problem · Heap

Ad Score Scheduler With Delay

Learn this problem
MediumGoogle logoGoogleFULLTIMEONSITE INTERVIEW
See Google hiring insights

Problem statement

Process the operations in operations against an advertisement scheduler. Each operation is one of:

  • INSERT id score delay: insert a new advertisement with a unique identifier, its current integer score, and a nonnegative delay.
  • GET: return the eligible advertisement with the highest current score. If several eligible ads have the same score, return the lexicographically smallest identifier. After an ad is returned, decrease its score by 1.

An advertisement returned by a GET cannot be returned on the next max(1, delay) GET calls. This guarantees that the same advertisement is never returned consecutively, even when its declared delay is 0. Calls that return no ad still count toward every waiting period.

If no advertisement is eligible, return NONE. Inserted advertisements remain registered for the rest of the simulation. Return the result of every GET operation in order.

Function

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

Examples

Example 1

operations = ["INSERT alpha 5 0","INSERT beta 4 0","GET","GET","GET"]return = ["alpha","beta","alpha"]

alpha wins the first call and its score becomes 4. It cannot repeat immediately, so beta is returned next. On the third call, alpha is eligible again and has the larger score.

Example 2

operations = ["INSERT a 10 2","INSERT b 5 0","GET","GET","GET","GET"]return = ["a","b","NONE","a"]

After the first result, a must sit out two calls. After the second result, b must sit out one call, so the third call has no eligible ad. Both are eligible by the fourth call, and a has the larger score.

Constraints

  • 1 <= operations.length <= 100000
  • Every operation is exactly GET or has the form INSERT id score delay.
  • Each identifier contains 1 through 20 lowercase English letters and is inserted exactly once.
  • -1000000000 <= score <= 1000000000
  • 0 <= delay <= operations.length

More Google problems

drafts saved locally
public String[] runAdScheduler(String[] operations) {
  // write your code here
}
operations["INSERT alpha 5 0","INSERT beta 4 0","GET","GET","GET"]
expected["alpha", "beta", "alpha"]
checking account