KatInterview

Layered Invoice Processor with Discounts, Tax, and FX

Arrays & HashingModel answer
MediumModel answerArrays & Hashingdata-processingdecimal-arithmeticinput-validation

Last updated

Build a multi-part order cost engine that handles input validation, per-item discounts, order-level tax, multi-currency conversion, and banker's rounding.

Layered Invoice Processor

You are building the backend logic for an invoicing service. Given a product catalog and a list of customer orders, implement a cost calculation engine in three progressive layers. Python is fine; any language works as long as reasoning is clear.

Inputs

  • price_catalog: dict[str, float] — maps a product code (sku) to its unit price.
  • orders: list of order objects, each containing:
    • order_id: string
    • items: list of { sku: str, qty: int }

Part 1 — Order totals with validation

For every order, compute:

total_cost = sum(qty * unit_price)  for each valid item

Validation rules:

  • If a sku does not appear in price_catalog, skip that line item and record the sku in a per-order missing_skus list.
  • A qty is valid only if it is a genuine integer (not a float like 2.0, not a boolean, not None), and falls in the range [1, 2^31 − 1]. Anything outside those bounds goes into a per-order invalid_items list.
  • Never raise an unhandled exception. Always return both a results list and an errors structure describing what was skipped.

Output: a list of { order_id, total_cost } sorted by order_id ascending. Show the sort key or lambda explicitly.


Part 2 — Discounts and tax

Extend the function signature with two optional inputs:

  • discounts: dict[str, float] — maps sku to a discount rate in [0.0, 1.0].
  • Per-order tax_rate field (float in [0.0, 1.0]).

Updated formula:

total_cost = (sum of qty * unit_price * (1 − discount_rate)) * (1 + tax_rate)
  • Out-of-range discount or tax values must be reported and ignored (treated as zero), not silently clamped.
  • Add a sort_by parameter that accepts "order_id" or "total_cost", and a descending flag. Show the sort logic explicitly.

Part 3 — Multi-currency conversion and rounding

Extend orders with an optional currency field (e.g., "EUR", "JPY"). Add:

  • fx_rates: dict[str, float] — maps each currency code to its value in USD (i.e., fx_rates["EUR"] = 1.08 means 1 EUR = 1.08 USD).
  • target_currency: string (default "USD").

Conversion:

converted_total = total_in_source_currency * fx_rates[source] / fx_rates[target]
  • Validate that rates exist and are positive finite numbers. If conversion is impossible for an order, set total_cost = None and record the reason.
  • Round all monetary outputs to two decimal places using banker's rounding (round half to even). Perform this rounding exactly once, as the final step.

What to address

  • Time and space complexity for each part (in terms of number of orders N and average items per order M).
  • How you would adapt the implementation for very large inputs that do not fit in memory.
  • A brief test plan covering: normal cases, unknown SKUs, bad quantities, extreme numeric values, out-of-range rates, sort correctness, and failed currency conversions.

Hints

Hint 1

Floating-point arithmetic is the wrong tool for money. Wrap every price, rate, and quantity in Decimal as early as possible, read floats via Decimal(str(x)) to avoid binary representation errors, and call .quantize() exactly once at the very end.

Hint 2

Validate at the boundary, not mid-computation. Parse the catalog and discount map once before the order loop. Then validate each order and each line item as you encounter them. Separation keeps the happy path clean and the error accumulation consistent.

Hint 3

For sorting when total_cost can be None, decide on a policy (e.g., failed orders always sort last) and enforce it explicitly rather than relying on comparison behavior that varies by language. For the total_cost sort, apply a secondary sort by order_id first to get a stable, deterministic tie-break.