Top K Best-Selling Products in the Last N Days
Updated
Problem statement
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.
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.
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
- 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