Katkat

Calculate a Basket Total from Delimited Product Records

InstacartSoftware Engineer · Onsite

Last 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 quantity of 0 contributes 0 cents 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-101 first entry: 2 * 399 = 798 cents
  • SKU-205: 4 * 150 = 600 cents
  • SKU-101 second entry: 1 * 399 = 399 cents
  • Total: 798 + 600 + 399 = 1797 cents ($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 0 contribute 0 cents.
  • Only SKU-404 contributes: 3 * 549 = 1647 cents.

Constraints

  • 0 <= len(records) <= 10^5
  • Each element in records contains exactly three '|' delimiters separating four non-empty tokens.
  • 0 <= quantity <= 10^4
  • 0 <= unit_price_cents <= 10^6
  • Total basket cost fits within a standard 64-bit signed integer.

Edge Cases

  • Empty input: records = [] should return 0.
  • All zero quantities: Every record has quantity = 0, returning 0.
  • 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?