FastPrepTop K Best-Selling Products in the Last N Days
Problems/Top K Best-Selling Products in the Last N Days

Top K Best-Selling Products in the Last N Days

Amazon logoAmazon● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionExamples

Problem statement

Source note: Reported in an Amazon full-time onsite interview. In one of the interview rounds, you get a stream of sales. Each sale has a product name, a number of units, and the day it happened. You must find the k products that sold the most units over the last n days. The interviewer also asked how you would handle about a billion sales.

The report gives no input format, window rule, tie rule, limits, or examples. Here, the sales and the query days come as arrays sorted by day. The last n days include the query day, so a query on day q covers days q - n + 1 to q, and sales after day q do not count. A product's total is the sum of its units in those days. Each query gets its own list of names, running from the most units sold to the fewest. Ties go to the name that comes first in alphabetical order. A query returns all products sold in those days when there are fewer than k, and an empty list when there are none. The billion-sales question is discussed in the editorial but not checked.

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

A store records its sales as a stream. Sale i says that quantities[i] units of the product named products[i] were sold on day days[i]. Sales arrive in order of day, so days never decreases.

You also get a list of query days queryDays, which never decreases either, and two integers n and k. A query on day q looks at the last n days, counting day q: every sale whose day is from q - n + 1 to q, inclusive. For each product, add up the units it sold in those days. That sum is the product's total.

The problem statement continues
Pro

Examples

Example 1

products = ["apple","banana","apple","cherry","banana","apple"]quantities = [3,5,2,4,1,1]days = [1,1,2,3,4,5]queryDays = [3,5]n = 3k = 2return = [["apple","banana"],["cherry","apple"]]

Day 3 looks at days 1 to 3. There, apple sold 3 + 2 = 5 units, banana sold 5, and cherry sold 4. Apple and banana tie at 5, and apple comes first in alphabetical order.

Day 5 looks at days 3 to 5. There, cherry sold 4, banana sold 1, and apple sold 1. Cherry is first, and apple comes before banana.

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

  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Dungeon GameONSITE INTERVIEW · Seen Nov 2026
  • Integer BreakONSITE INTERVIEW · Seen Nov 2026
  • LFU CacheONSITE INTERVIEW · Seen Nov 2026
  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
  • Count Zero Request ServersSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Message Event AggregationOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Longest Unique Substring RangeGoogle · ONSITE INTERVIEWGoogle interview questions
  • All Distinct Palindromic SubstringsDoorDash · ONSITE INTERVIEWDoorDash interview questions
  • Two SumMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions
  • Count Redundant File BackupsGoldman Sachs · OAGoldman Sachs interview questions
  • Reconcile Unmatched Trades with Timestamp ToleranceCitadel · PHONE SCREENCitadel interview questions
  • Disjoint Target-Sum TripletsAmerican Express · PHONE SCREENAmerican Express interview questions

Amazon interview experiences

  • Amazon SWE OA with AI Coding: 1 Experience
  • Amazon SWE Intern Phone Screen Interview Experience: 1 Experience
  • Amazon New Grad SDE I OA and Virtual Onsite: 1 Experience
  • Amazon BIE Intern SQL and Interview Questions: 17 Experiences
  • Amazon Experienced SWE AI-Assisted Debugging OA: 1 Experience
  • Amazon SDE II (L5) Interview Questions: 5 Experiences
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