Shopping Cart Receipt
Updated
Problem statement
Build the final receipt for a shopping cart. The arrays products and prices form a catalog: prices[i] is the unit price of products[i], in integer cents. The cart starts empty.
Process operations in order:
ADD nameadds one purchased unit of that product.REMOVE nameremoves one purchased unit if present; otherwise it does nothing.
The product named by promotion has a buy-three-get-two-free offer. For every complete group of three purchased units remaining in the cart, include two extra free units of the same product. Free units do not qualify for further gifts. Recompute gifts from the final purchased count after all removals.
Return one line per product with a positive purchased count, sorted alphabetically by product name, followed by a total line. Each product line is exactly name|quantity|unitPrice|lineTotal, where quantity includes free units, unitPrice is the catalog price in cents, and lineTotal charges only purchased units. The last line is exactly TOTAL|total, the sum of all line totals. Use decimal integers without extra spaces or leading zeros.
For example, three purchased units priced at 200 cents produce quantity 5 and line total 600 under the promotion. Five purchased units produce quantity 7 and line total 1000. An empty cart returns ["TOTAL|0"].
Function
buildReceipt(products: String[], prices: int[], operations: String[], promotion: String) → String[]Examples
Example 1
products = ["milk","cheese"]prices = [150,200]operations = ["ADD cheese","ADD cheese","ADD cheese","ADD milk"]promotion = "cheese"return = ["cheese|5|200|600","milk|1|150|150","TOTAL|750"]Three purchased cheeses deliver five units for 600 cents; milk adds 150 cents. Lines are alphabetized.
Example 2
products = ["cheese"]prices = [200]operations = ["ADD cheese","ADD cheese","ADD cheese","REMOVE cheese"]promotion = "cheese"return = ["cheese|2|200|400","TOTAL|400"]Two purchased units remain, so the two former gifts disappear and the charge is 400 cents.
Example 3
products = ["bread"]prices = [300]operations = ["REMOVE bread"]promotion = "bread"return = ["TOTAL|0"]Removing an absent product does nothing. The receipt contains only the zero total.
Constraints
1 <= products.length == prices.length <= 1000.- Product names are unique strings of 1 to 20 lowercase English letters.
1 <= prices[i] <= 10000.0 <= operations.length <= 10000. Each operation has exactly one ASCII space and names a catalog product.promotionis a catalog product. No other discounts or charges apply.
Similar questions at other companies
- Merchant Payout Scheduler, Part 2: Per-Merchant SchedulesStripe · PHONE SCREENStripe interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Pins Violation Log QueriesPinterest · PHONE SCREENPinterest interview questions
- Camel CardsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Message Event AggregationOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Banking System with Pending Transfer AcceptanceCircle · PHONE SCREENCircle interview questions
- Server Log Summary and Minute AnomaliesNinjaTrader · PHONE SCREEN
- Streaming Top-K Frequent ElementsNVIDIA · PHONE SCREENNVIDIA interview questions
- Interview Slot BookingTrueFoundry · PHONE SCREEN
- Streaming Step Progress HierarchyThe Allen Institute for AI · PHONE SCREEN