Ad Rotation Scheduler With Cooldown
Updated
Problem statement
You are building an ad rotation scheduler. There are n ads, numbered from 1 to n. Ad i must be shown exactly counts[i - 1] times.
Implement the scheduler by returning the sequence of values that repeated calls to next() would produce.
On each call, the scheduler should choose the next currently available ad in round-robin order. Initially, available ads are ordered by increasing ad ID. After an ad is shown, if it still has remaining displays, it becomes unavailable for its cooldown period and then returns to the back of the available queue.
Cooldown is measured in calls to next(). If ad i is shown on call t, it can be shown again no earlier than call t + cooldowns[i - 1] + 1. If no ad is available on a call but some displays remain, that call returns -1 and time still advances by one call.
Return the full output sequence until all display counts have been consumed. Do not include trailing -1 values after every ad has been shown the required number of times.
Original Interview Report
Onsite 2 was an ad rotation / scheduling problem. Multiple ads each have a display count. Design next(); each call returns the next ad, rotating through ads as evenly as possible, such as 1,2,3,1,2,3, unless only one ad remains.
The follow-up adds cooldown: after an ad is displayed, it must wait for its corresponding cooldown period before it can be displayed again.
Function
scheduleAdsWithCooldown(counts: int[], cooldowns: int[]) → int[]Examples
Example 1
counts = [2,2,2]cooldowns = [0,0,0]return = [1,2,3,1,2,3]All ads have no cooldown, so the scheduler cycles through them in round-robin order.
Example 2
counts = [3,1]cooldowns = [0,0]return = [1,2,1,1]After ad 2 has been used once, only ad 1 remains, so it is returned on the remaining calls.
Example 3
counts = [3]cooldowns = [2]return = [1,-1,-1,1,-1,-1,1]The only ad must wait two calls after each display. During those waiting calls, no ad is available, so -1 is returned.
Constraints
1 <= counts.length == cooldowns.length <= 20000 <= counts[i] <= 2000for at least one ad0 <= cooldowns[i] <= 2000- The returned sequence length will not exceed
200,000.
More Google Onsite Interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Combine the Two Smallest Until All Reach kAdobe · PHONE SCREENAdobe interview questions
- API Call Thread Pool ScheduleBaseten · ONSITE INTERVIEWBaseten interview questions
- Streaming Ingredient DishesHarness · ONSITE INTERVIEW
- CPU Scheduling SimulatorMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Assign Partitions to the Least-Loaded ServersAmazon · ONSITE INTERVIEW
- Durable Work Queue OperationsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Dynamic Batch Decode TracexAI · ONSITE INTERVIEWxAI interview questions
- Team-Preferred Stable MatchingBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Batch API Requests with Retry DelaysDecagon · PHONE SCREENDecagon interview questions
- Automation Pipeline, Part 3: Stage DurationPlaid · PHONE SCREENPlaid interview questions