Priority Ad Rotation with Show Limits
Updated
Problem statement
An ad server keeps a set of ads and picks one of them each time a page asks for an ad. Each ad has an id, a priority, a show count (the most times it may be shown) and a timeout. You are given an array operations that lists calls to the server in the order they happen. Each call is one of these two:
[1, id, priority, showCount, timeout]issetAd: add an ad with this id, priority, show count and timeout. It returns nothing.[0]isgetAd: pick the ad to show now and return its id, or-1if no ad may be shown.
Number the getAd calls 1, 2, 3, ... in order. setAd calls do not get a number, and a getAd call that returns -1 still does. If an ad is shown on getAd call c, it may not be shown on the next timeout calls, so the earliest call where it may be shown again is c + timeout + 1.
Examples
Example 1
operations = [[1,7,5,3,1],[1,4,3,1,1],[0],[0],[0],[0],[0]]return = [7,4,7,-1,7]Ad 7 has priority 5, 3 shows and timeout 1. Ad 4 has priority 3, 1 show and timeout 1. Call 1 shows ad 7. Call 2 may not show ad 7 again right away, so it shows ad 4, which uses up ad 4. Call 3 shows ad 7. On call 4, ad 7 was just shown and ad 4 has no shows left, so the call returns -1. Call 5 shows ad 7 for the third and last time.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 2 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.
More Amazon Onsite Interview questions
- Add Course Batches All or NothingONSITE INTERVIEW · Seen Dec 2026
- Next Greater Element IONSITE INTERVIEW · Seen Dec 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Optimal Account BalancingONSITE INTERVIEW · Seen Dec 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Flood FillONSITE INTERVIEW · Seen Nov 2026
- Merge k Sorted ListsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Design Authentication ManagerOracle · ONSITE INTERVIEWOracle interview questions
- Delayed Job SchedulerWalmart Labs · ONSITE INTERVIEW
- Thread-Safe Task Storage and Worker DispatchNuro · ONSITE INTERVIEWNuro interview questions
- Notification Deduplication WindowGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Helpdesk Ticket ProcessingThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Execute Tasks by PrioritySnowflake · ONSITE INTERVIEWSnowflake interview questions
- Priority Job Scheduler with CooldownsGoogle · ONSITE INTERVIEWGoogle interview questions
- Order Matching EngineJane Street · ONSITE INTERVIEWJane Street interview questions
- Trip Booking with the Earliest Free DriverRippling · ONSITE INTERVIEWRippling interview questions
- Reusable Number AllocatorDropbox · ONSITE INTERVIEWDropbox interview questions
Amazon interview experiences
- Amazon Experienced SDE Onsite Questions: 1 Experience
- Amazon SDE Onsite Interview Questions: 9 Experiences
- Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
- Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
- Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
- Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)