Problem Description
Imagine an online hardware store that sells products of different sizes.
The store could sell small items like a mouse or larger items like a laptop.
These items cost a different amount to be shipped out.
Shipping to different countries can also cost a different amount.
Imagine you have an order object that looks like the following:
Order:
{
"country": "US", // or "CA" for Canada
"items": [
{"product": "mouse", "quantity": 20},
{"product": "laptop", "quantity": 5}
]
}
Note: In your solution you can pass in an in-memory object that has the same shape as the JSON.
You don’t need to worry about parsing JSON for this problem.
The US and CA orders have the same shape, they just represent different countries.
Shipping Cost Matrix
Each country and product has a corresponding shipping cost matrix.
The cost is stored in the smallest currency unit (e.g., cents).
ShippingCost:
{
"US": [
{"product": "mouse", "cost": 550},
{"product": "laptop", "cost": 1000}
],
"CA": [
{"product": "mouse", "cost": 750},
{"product": "laptop", "cost": 1100}
]
}
Task 1
Write a function called calculate_shipping_cost that takes in an order and a shipping cost matrix, and returns the total shipping cost.
Example
calculate_shipping_cost(order_us, shipping_cost)
calculate_shipping_cost(order_ca, shipping_cost)
Quantity-Based Discounts (Tiered Pricing)
For some items, shipping the first few units is more expensive than shipping additional units.
You need to update the shipping matrix to reflect quantity-based discounts.
The order remains the same, but the shipping cost matrix is now updated like the following:
Updated Shipping Cost Matrix (Tiered Discounts)
US
{
"US": [
{
"product": "mouse",
"costs": [
{"minQuantity": 0, "maxQuantity": null, "cost": 550}
]
},
{
"product": "laptop",
"costs": [
{"minQuantity": 0, "maxQuantity": 2, "cost": 1000},
{"minQuantity": 3, "maxQuantity": null, "cost": 900}
]
}
]
}
CA
{
"CA": [
{
"product": "mouse",
"costs": [
{"minQuantity": 0, "maxQuantity": null, "cost": 750}
]
},
{
"product": "laptop",
"costs": [
{"minQuantity": 0, "maxQuantity": 2, "cost": 1100},
{"minQuantity": 3, "maxQuantity": null, "cost": 1000}
]
}
]
}
Task 2
Update the function calculate_shipping_cost to now calculate shipping based on the tiered discount rules.
Example Output
calculate_shipping_cost(order_us, shipping_cost) # → 15700
calculate_shipping_cost(order_ca, shipping_cost) # → 20200
Key Points and Challenges of the Problem
🔍 Common Follow-up Questions from Interviewers
✅ Summary
This is a very typical Stripe system design style algorithm question — the prompt closely reflects real business logic (international shipping fees, tiered discounts), but the core still involves data structures + logical matching + edge case handling. In interviews, candidates should focus on demonstrating clear logical expression, good naming, and code readability.
🚀 Further Recommendations
Our Stripe mock interview coaching offers complete problem explanations, breakdowns of thought processes, and mock scenario training to help you quickly get into the zone and answer accurately during the real interview.