Average Purchase Price
Updated
Problem statement
A product manager wants to see the average price that buyers pay for each item. Purchases and requests for the average come in one at a time, as the array events in arrival order. Each event is one of these:
"BUY item price": someone boughtitemforpricecents."AVG item": a request for the average price of all purchases ofitemthat came before this event.
The average is the total of those prices divided by how many there are, rounded down to a whole number of cents. If item has no purchases yet, the answer is -1.
Return the answers to the AVG events, in the order the events appear.
Function
averagePrices(events: String[]) → int[]Examples
Example 1
events = ["BUY mug 1200","BUY mug 1500","AVG mug","BUY hat 999","AVG hat","AVG mug"]return = [1350,999,1350]The two mug purchases cost 1200 and 1500, so the average is 2700 / 2 = 1350. The hat has one purchase at 999. The purchase of a hat does not change the average for mug, which is still 1350.
Example 2
events = ["AVG card","BUY card 5","BUY card 6","AVG card"]return = [-1,5]The first query comes before any card purchase, so its answer is -1. Later the average is 11 / 2 = 5.5, which rounds down to 5.
Example 3
events = ["BUY a1 1","BUY a1 1","BUY a1 2","AVG a1","AVG b2"]return = [1,-1]Three purchases add up to 4, and 4 / 3 rounds down to 1. Nobody has bought b2, so that query returns -1.
Example 4
events = ["BUY tv 1000000","BUY tv 1000000","BUY tv 999999","AVG tv","BUY tv 1","AVG tv"]return = [999999,750000]The first three purchases add up to 2999999, and 2999999 / 3 rounds down to 999999. After a fourth purchase at 1, the total is 3000000 over 4 purchases, so the average is 750000.
Constraints
1 <= events.length <= 2 * 104- Each event is
"BUY item price"or"AVG item", with single spaces.itemhas1to10lowercase letters or digits, andpriceis an integer written without leading zeros. 1 <= price <= 106- There are between
1and5000AVGevents.
More WhatNot Onsite Interview questions
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Detect Blocked Chat PhrasesPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2026
- Minimum Remove to Make Valid ParenthesesONSITE INTERVIEW · Seen Dec 2024
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2024
- Merge Two Sorted ListsONSITE INTERVIEW · Seen Jul 2024
- Number of Submatrices That Sum to TargetONSITE INTERVIEW · Seen Jul 2024
- Parse and Store Log RecordsONSITE INTERVIEW · Seen Oct 2023
- Room Occupancy at Query TimesONSITE INTERVIEW · Seen Oct 2023
- K Smallest Values in a StreamPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2022
Similar questions at other companies
- Count Adjacent Symbol Pairs for BPEAmazon · ONSITE INTERVIEW
- Count a User's Actions in an Event LogMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Filter Pins by Excluded IDsPinterest · ONSITE INTERVIEWPinterest interview questions
- First Non-Repeating CharacterGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Longest Dictionary Word from Nine LettersGoogle · ONSITE INTERVIEWGoogle interview questions
- Minimum Replacements to Make Two Strings AnagramsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Logger Rate LimiterAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Isomorphic StringsRemitly · ONSITE INTERVIEWRemitly interview questions
- Check AnagramsDeloitte · ONSITE INTERVIEWDeloitte interview questions