Calculate a Basket Total from Delimited Product Records

Read the full interview experience this question came from →

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 integer number of cents. 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. ### 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

|Home/Coding & Algorithms/Instacart
Instacart logo
Instacart
Sep 6, 2026
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

Implement total_price_cents(items) for a list of product records. Each record has exactly this format:

sku|product name|quantity|unit price in cents

Return the sum of quantity * unit price in cents across all records, as an integer number of cents. 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.

Examples

total_price_cents(["a100|Apple|2|125", "b200|Bread|1|350"])
-> 600

The records contribute 250 cents and 350 cents.

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.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...