Implement multi-part cost calculator
StripeSoftware Engineer · Technical ScreenLast updated
This question evaluates a candidate's ability to implement robust data-processing and numeric computation logic for a multi-part cost calculator, covering input validation, per-item discounts and tax, sorting semantics, multi-currency conversion, and precise monetary rounding.
You are given a price catalog and a list of orders. Implement the following in any language (Python examples acceptable):
Inputs
- price_catalog: mapping from sku (string) -> unit_price (float).
- orders: list of orders; each order has:
- order_id (string)
- items: list of { sku: string, qty: integer }
Part 1 — Basic totals
- For each order, compute total_cost = sum(qty * unit_price) over its valid items.
- Validations and corner cases:
- If an item's sku is not in price_catalog, skip that item and record it in a per-order "missing_skus" list.
- If qty is non-numeric, non-integer, <= 0, or overflows type limits, treat it as invalid; skip and record in a per-order "invalid_items" list.
- Do not crash on malformed input; return both results and an "errors" structure.
- Output: list of { order_id, total_cost } sorted by order_id ascending. In Python, show the lambda used for sorting; in other languages, provide the equivalent.
Part 2 — Discounts and tax
- Extend inputs to support optional per-item discounts: discounts: mapping sku -> discount_rate (0.0–1. 0), and optional per-order tax_rate (0.0–1. 0).
- Compute total_cost = (sum over items of qty * unit_price * (1 - discount_rate))) * (1 + tax_rate).
- Validate numeric ranges for discount_rate and tax_rate; out-of-range values should be treated as invalid and reported.
- Allow the caller to choose sorting: by "order_id" or by "total_cost", ascending or descending (demonstrate the sort key/compare function; in Python show the lambda).
Part 3 — Multi-currency and rounding
- Extend orders with optional currency code (e.g., "USD", "EUR"). Given a rates mapping base->target (e.g., usd_per_unit: mapping currency -> rate), convert each order’s total into a target_currency parameter (default "USD").
- Validate that required conversion rates exist and are numeric; report missing or invalid rates.
- Round monetary outputs to two decimals with banker's rounding (or specify the rounding mode you choose and apply it consistently).
Complexity and robustness
- State the time and space complexity of your implementation for each part, and discuss how you would handle very large inputs (streaming, batching, or memory constraints).
- Provide a brief test plan that covers normal cases, missing_skus, invalid qty, extreme numeric values, discount/tax edge cases, sorting correctness, and currency conversion failures.
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.