← 返回博客列表
Stripe

Stripe Interview Question: Shipping Cost Calculation

2026-01-05

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):

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.

  1. Preprocessing: The raw shipping matrix is often a list, implying O(N) lookup time. Convert it into a Hash Map (Dictionary) keyed by country -> product for O(1) access.
  2. 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: null means infinity).
  3. 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


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.

Contact Us for more Stripe interview questions and personalized coaching.