Shipping cost with tiered pricing
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Stripe's technical phone screen, last asked Jan 2026. It tests arithmetic, hash map and pricing.
An order ships to one country and contains several products, each with a quantity. A pricing table gives, for each country and product, what shipping costs. Compute the order's total shipping cost in cents. The pricing model grows in three parts.
Interface
Unless you agree on a different interface with the interviewer, assume:
shipping_cost(order: dict, prices: dict) -> intorder = {"country": "US", "items": [{"product": "mouse", "quantity": 20}, ...]}-
prices[country][product]is the product's pricing in that country, in the shape of the current part.
Part 1: a price per unit
prices["US"]["mouse"] = 550 means every unit costs 550 cents.
Part 2: tiered prices
Each product has a list of tiers {"min": int, "max": int | None, "cost": int}. Units numbered min + 1 through max cost cost each; max: None means no upper bound. Each unit is charged at the rate of the tier it falls in.
Part 3: fixed-fee tiers
A tier may carry "type": "fixed": if any units fall in that tier, it costs cost once, however many units it covers. Other tiers stay per unit.
Example
order = {"country": "US", "items": [{"product": "mouse", "quantity": 20},
{"product": "laptop", "quantity": 5}]}
Part 1: mouse 550, laptop 1000
20 * 550 + 5 * 1000 = 16000
Part 2: mouse [{min 0, max 2, cost 550}, {min 2, max None, cost 450}]
laptop [{min 0, max 3, cost 1000}, {min 3, max None, cost 900}]
mouse 2 * 550 + 18 * 450 = 9200
laptop 3 * 1000 + 2 * 900 = 4800
total 14000
Part 3: laptop [{min 0, max 2, cost 1500, type fixed}, {min 2, max None, cost 900}]
5 laptops: 1500 + 3 * 900 = 4200
1 laptop: 1500
Constraints
- Tiers of a product are sorted, start at 0 and do not overlap or leave gaps.
- Quantities and costs are non-negative integers.
Clarifying questions
- Are tier bounds inclusive or exclusive? Is a quantity of exactly
maxin the lower tier? - What happens when a product or country has no price?
- Do tiers apply per order line or per product across the whole order?
- Can a fixed-fee tier be partly used, and is it still charged in full?
What a strong answer covers
- One function that charges a quantity against a tier list, reused by every part.
- Correct boundaries: units in a tier are
min(quantity, max) - min, never negative. - Fixed tiers charged once when any unit falls in them; zero quantity costs zero.
- Clear errors for missing countries or products, and tests at every boundary.
Follow-ups
- Add a free-shipping threshold on the order total.
- Some countries price by weight rather than by unit.
- Tiers apply to the total units of a product category, not per product.
Where it's asked
- Stripe · Technical phone screen · Last asked Jan 202660 min · One realistic task in parts, in a project editor (CoderPad project mode or HackerRank Projects) with several files and tests
What it tests
arithmetic · hash map · pricing
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
