Compute shipping cost with tiered pricing
StripeData Engineer · Technical ScreenLast updated
Evaluates implementation-level algorithmic logic for tiered pricing, covering per-unit aggregation, interval reasoning, and mixed incremental vs fixed pricing models within the Coding & Algorithms domain and Data Engineering role; abstraction level is practical, implementation-focused business logic and data transformation.
You are building a shipping-cost calculator. Each order contains line items, and shipping rules vary by destination country and product.
Assume:
OrderLine = { country: string, product: string, quantity: int }, wherequantity >= 1.- All costs are in the same currency.
- Quantity tiers use integer boundaries and are inclusive.
- If a required shipping rule is missing for a (country, product) pair, you may either throw an error or treat shipping cost as 0, but be consistent and document your choice.
Implement the following progressively more complex versions.
Part 1 — Fixed per-unit shipping
You are given:
orderLines: OrderLine[]unitRates: { country: string, product: string, unit_price: number }[]
For each order line, shipping cost is quantity * unit_price for that (country, product). Return the total shipping cost across all order lines.
Part 2 — Incremental tiered shipping (taxi-meter style)
Now per-unit shipping depends on which quantity interval each unit falls into.
You are given:
orderLines: OrderLine[]incrementalTiers: { country: string, product: string, start_qty: int, end_qty: int, unit_price: number }[]
For a given order line with quantity Q, compute shipping by charging each unit according to the tier covering that unit index.
Example interpretation: if tiers are [1–3 @ $5] and [4–10 @ $3] and Q=5, cost is 3*$5 + 2*$3.
Return total shipping cost across all order lines.
Also explain how your implementation handles the special case where the order quantity falls entirely within a single tier.
Part 3 — Mixed pricing models: incremental vs fixed total
Now tiers can be of two types:
- incremental: same as Part 2 (per-unit charges by unit index)
- fixed: if the total quantity Q falls within the tier
[start_qty, end_qty], the shipping cost for the entire line item is a fixed total price (independent of Q)
You are given:
orderLines: OrderLine[]tiers: { country: string, product: string, start_qty: int, end_qty: int, pricing_type: 'incremental'|'fixed', price: number }[]
Rules to compute shipping for one order line with quantity Q:
- If there exists a fixed tier whose interval covers
Q(i.e.,start_qty <= Q <= end_qty), use that tier’spriceas the total shipping for that line item. - Otherwise, compute shipping using the incremental tiers as in Part 2.
Return total shipping cost across all order lines.
Write clean, correct code and include at least one meaningful test case for each part.
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.