Calculate a Basket Total from Delimited Product Records
InstacartSoftware Engineer · OnsiteLast updated
Parse pipe-delimited shopping cart records to compute the total basket cost in integer cents, properly accumulating repeated SKUs and handling zero-quantity items.
Problem Statement
In an e-commerce checkout pipeline, cart line items are transmitted as a list of pipe-delimited (|) strings. Each string represents an item record containing four fields:
"sku|product_name|quantity|unit_price_cents"
Your task is to parse these records and calculate the total cost of the basket in integer cents.
Keep the following rules in mind:
- A SKU can appear multiple times in the list (e.g., when a customer adds items at different times or from different aisles). Every appearance represents an additional quantity of that item and must be included in the total.
- An item with a
quantityof0contributes0cents to the total cost. - Prices are given in integer cents to ensure exact arithmetic and avoid floating-point inaccuracies.
- If a record has leading or trailing whitespace around fields, it should be trimmed before parsing.
Write a function calculate_basket_total(records: list[str]) -> int that returns the total basket cost in integer cents.
Interface
def calculate_basket_total(records: list[str]) -> int:
pass
Examples
Example 1
records = [
"SKU-101|Organic Whole Milk|2|399",
"SKU-205|Avocado|4|150",
"SKU-101|Organic Whole Milk|1|399"
]
# Output: 1797
Explanation:
SKU-101first entry:2 * 399 = 798centsSKU-205:4 * 150 = 600centsSKU-101second entry:1 * 399 = 399cents- Total:
798 + 600 + 399 = 1797cents ($17.97)
Example 2
records = [
"SKU-300|Reusable Grocery Bag|0|100",
"SKU-404|Sparkling Water 12pk|3|549",
"SKU-505|Bananas|0|49"
]
# Output: 1647
Explanation:
- Items with quantity
0contribute0cents. - Only
SKU-404contributes:3 * 549 = 1647cents.
Constraints
0 <= len(records) <= 10^5- Each element in
recordscontains exactly three'|'delimiters separating four non-empty tokens. 0 <= quantity <= 10^40 <= unit_price_cents <= 10^6- Total basket cost fits within a standard 64-bit signed integer.
Edge Cases
- Empty input:
records = []should return0. - All zero quantities: Every record has
quantity = 0, returning0. - Duplicate SKUs: Repeated entries for the same SKU should both be added to the total; do not accidentally overwrite earlier records if storing in a map.
Hints
Hint 1
Split each string by the delimiter and convert the quantity and price fields directly into integers without converting to floating-point dollar amounts.
Hint 2
Be careful not to overwrite previous entries if you use a hash map keyed by SKU; multiple records can share the same SKU and must all be counted.
Hint 3
Since the return value is just the total basket price in cents, you can directly accumulate the product of quantity and unit price for each record in a running sum.
Follow-up questions
- How would you handle malformed records (e.g., missing fields, negative quantities, non-numeric values) gracefully without failing the entire batch?
- If the requirement changes so that a subsequent record with the same SKU represents a quantity update (override) rather than an addition, how would your approach change?
- How would you support item-level promotional discounts, such as 'Buy 2, Get 1 Free' or '$1 off when you buy 3 or more' across repeated records?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.