FastPrepPriority Ad Rotation with Show Limits
Problems/Priority Ad Rotation with Show Limits

Priority Ad Rotation with Show Limits

Amazon logoAmazon● MediumFULLTIMEONSITE INTERVIEW

Updated Dec 2026

QuestionExamples

Problem statement

Source note: Reported in an Amazon interview for a Software Development Engineer 2 (L5) role in India. It was the third of four interview rounds after the online assessment, a coding round with a bar raiser. The report asks for two calls: one adds an ad that has a priority and a limit on how many times it can be shown, and the other returns the ad with the highest priority, uses up one of its shows, and never returns the same ad twice in a row. A follow-up gives every ad its own timeout: after it is shown, it is skipped for that many requests. The candidate used a priority queue and remembered the last ad shown.

The report gives no input format, return shape, examples, limits or tie rule. The limits are our own choice, and so are the list of calls with one result per getAd call, ties broken by the smaller id, -1 when no ad may be shown (also when only the ad shown last is left), calls that return -1 counting toward a timeout, and timeout 1 standing for the basic rule. All examples are our own.

Our version is about 85% the same as the reported question.

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] is setAd: add an ad with this id, priority, show count and timeout. It returns nothing.
  • [0] is getAd: pick the ad to show now and return its id, or -1 if 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.

The problem statement continues
Pro

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.

02 · Example 2
FastPrep Pro
Reported in 1 Amazon interview this week

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
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week

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)
See Amazon hiring insights
CodePython 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import heapq
from collections import defaultdict
 
class Solution:
def solve(self, values, queries):
# Build the adjacency structure
n = len(values)
graph = defaultdict(list)
for u, v in queries:
graph[u].append(v)
 
best = [float("inf")] * n
heap = [(0, 0)]
while heap:
cost, node = heapq.heappop(heap)
if cost > best[node]:
continue
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Amazon interview this week

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
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week