Quick Overview

Parse SKU, product, quantity, and integer-cent prices to calculate a basket total, with repeated records, zero quantities, and exact integer arithmetic.

Calculate a Basket Total from Delimited Product Records

Company: Instacart

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Implement `total_price_cents(items)` for a list of product records. Each record has exactly this format: ```text sku|product name|quantity|unit price in cents ``` Return the sum of `quantity * unit price in cents` across all records, as an exact integer number of cents represented by a decimal string as described below. For this interface, quantities are whole nonnegative units and prices are nonnegative integer cents. Records are well formed, and fields do not contain the separator character. Each record contributes independently, including records with the same SKU. An empty list has total zero. No discounts, taxes, or rounding are part of this function. ### Exact result representation The input records remain strings in the format above. Return the exact total as a canonical nonnegative decimal string: `"0"` or a sequence of ASCII digits beginning with `1` through `9`, with no sign, leading zeroes, or whitespace. This output representation preserves integer-cent totals of any magnitude. It does not impose a monetary bound or change the arithmetic. Do not round, overflow a fixed-width type, or return a remainder modulo another number. An empty list returns `"0"`. ### Examples ```text total_price_cents(["a100|Apple|2|125", "b200|Bread|1|350"]) -> "600" ``` The records contribute 250 cents and 350 cents. ```text total_price_cents(["a100|Apple|1|125", "a100|Apple|3|125", "c300|Cup|0|400"]) -> "500" ``` Both apple records contribute, and a zero-quantity record contributes zero. Use integer arithmetic to preserve the input's cent-based representation. No maximum list size or monetary bound is specified.

Overview: Parse SKU, product, quantity, and integer-cent prices to calculate a basket total, with repeated records, zero quantities, and exact integer arithmetic.

Read the full Instacart Software Engineer interview experience this question came from

Implement `total_price_cents(items)` for a list of product records. Each record has exactly this format: ```text sku|product name|quantity|unit price in cents ``` Return the sum of `quantity * unit price in cents` across all records, as an exact integer number of cents represented by a decimal string as described below. For this interface, quantities are whole nonnegative units and prices are nonnegative integer cents. Records are well formed, and fields do not contain the separator character. Each record contributes independently, including records with the same SKU. An empty list has total zero. No discounts, taxes, or rounding are part of this function. ### Exact result representation The input records remain strings in the format above. Return the exact total as a canonical nonnegative decimal string: `"0"` or a sequence of ASCII digits beginning with `1` through `9`, with no sign, leading zeroes, or whitespace. This output representation preserves integer-cent totals of any magnitude. It does not impose a monetary bound or change the arithmetic. Do not round, overflow a fixed-width type, or return a remainder modulo another number. An empty list returns `"0"`. ### Examples ```text total_price_cents(["a100|Apple|2|125", "b200|Bread|1|350"]) -> "600" ``` The records contribute 250 cents and 350 cents. ```text total_price_cents(["a100|Apple|1|125", "a100|Apple|3|125", "c300|Cup|0|400"]) -> "500" ``` Both apple records contribute, and a zero-quantity record contributes zero. Use integer arithmetic to preserve the input's cent-based representation. No maximum list size or monetary bound is specified.

Constraints

  • Each input is a well-formed sku|product name|quantity|unit price in cents record with no pipe inside a field.
  • Quantities and unit-cent prices are nonnegative integers; the original record input format is unchanged.
  • Every record contributes independently, including repeated SKUs.
  • Return the exact sum as a canonical nonnegative decimal string; empty input returns "0".
  • No monetary/list-size bound, discounts, taxes or rounding are introduced; canonical spelling is an output requirement.

Examples

Input: (['a100|Apple|2|125', 'b200|Bread|1|350'],)

Expected Output: '600'

Explanation: The first source example contributes 250 plus 350 cents.

Input: (['a100|Apple|1|125', 'a100|Apple|3|125', 'c300|Cup|0|400'],)

Expected Output: '500'

Explanation: The second source example preserves repeated SKU contributions and zero quantity.

Hints

  1. The last field is already measured in cents.
  2. Repeated SKUs still represent independent record contributions.

Loading coding console...

Show the approach

Approach

Separate each well-formed record at its three pipe delimiters. Read quantity from the third field and unit price from the fourth, multiply their exact nonnegative integer values and add the product to an exact accumulator. After the first r records, the accumulator is precisely the sum of their r contributions; induction over the records proves the final sum, including duplicate SKUs and zero factors. SKU and product name are not used for grouping, discounting or currency conversion. Starting from zero handles an empty list. All four languages use arbitrary-precision integer arithmetic; C++ parses decimal digits explicitly so input leading zeroes do not trigger octal interpretation. Python parses and renders chunks of at most nine digits, avoiding configurable whole-string/int decimal conversion limits for both very long inputs and outputs. The final conversion produces canonical decimal text without imposing that output-only spelling restriction on the input numeric fields.

Time complexity:
O(total record text length) delimiter work, plus exact decimal parsing, per-record multiplication, accumulation and output-conversion costs determined by digit lengths.
Space complexity:
Storage for the current parsed record and arbitrary-precision operands/total, plus the returned decimal output; no per-SKU map.