In Stripe's full-stack or backend interviews, "Shipping Cost Calculation" is a classic business logic simulation problem. It tests not only your ability to handle complex data structures but also your code readability, extensibility, and edge case handling.
Problem Description
Imagine you are building a shipping cost calculation system for an online hardware store. The store sells items of different sizes (e.g., Mouse, Laptop) and ships to various countries (e.g., US, CA).
You need to implement a function that calculates the total shipping cost based on an Order and a Shipping Cost Matrix.
1. Basic Calculation
Input Examples:
// Order
{
"country": "US",
"items": [
{"product": "mouse", "quantity": 20},
{"product": "laptop", "quantity": 5}
]
}
// Shipping Cost Matrix (in cents)
{
"US": [
{"product": "mouse", "cost": 550},
{"product": "laptop", "cost": 1000}
],
"CA": [
{"product": "mouse", "cost": 750},
{"product": "laptop", "cost": 1100}
]
}
Task: Implement calculate_shipping_cost(order, shipping_cost).
2. Tiered Pricing (Quantity-Based Discounts)
In real-world scenarios, shipping costs often have volume discounts. The problem introduces a new matrix structure with quantity tiers.
Updated Matrix Example (US):
- Mouse: Any quantity -> 550 cents
- Laptop:
- 0-2 units -> 1000 cents/unit
- 3+ units -> 900 cents/unit
Task: Update your function to support tiered pricing logic.
oavoservice Solution Analysis
The core of this problem lies in Data Structure Transformation and Interval Matching Logic.
- Preprocessing: The raw shipping matrix is often a list, implying O(N) lookup time. Convert it into a Hash Map (Dictionary) keyed by
country->productfor O(1) access. - Tier Logic:
- Avoid hardcoding. Encapsulate logic in a helper function like
get_unit_cost(product, quantity, country_rules). - Handle open-ended intervals (e.g.,
maxQuantity: nullmeans infinity).
- Avoid hardcoding. Encapsulate logic in a helper function like
- Currency: Always use integers (cents) to avoid floating-point precision errors.
Suggested Code Structure
def parse_shipping_rules(rules_json):
# Convert JSON to a query-friendly dictionary
pass
def calculate_tier_cost(item_quantity, tiers):
# Match quantity against defined tiers
pass
def calculate_shipping_cost(order, rules):
total_cost = 0
country = order['country']
# Iterate through items and accumulate cost
return total_cost
Common Follow-ups
- Scaling: How to handle thousands of countries if the matrix doesn't fit in memory? (Database/Caching design)
- Error Handling: What if a product in the order is missing from the shipping matrix?
- Currency Conversion: How to handle a matrix with mixed currencies?
Need Professional Interview Support?
oavoservice specializes in providing high-quality OA/VO assistance. Our team of senior engineers offers everything from resume optimization to mock interviews and real-time support.
- 100% Original Code: Unique solutions to ensure safety.
- Real-time Support: Key insights and code snippets during your VO to keep you on track.
Contact Us for more Stripe interview questions and personalized coaching.