Shopify VO Interview Problem: Calculate Shipping Cost

19 Views
No Comments

Calculate Shipping Cost

Order:

{
  "country": "US", // or "CA" for the CA order
  "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 are just different countries.

Each country/product has a corresponding shipping cost matrix. The cost is stored in the smallest currency unit.

Shipping Cost:

Each product has its own shipping cost

{
  "US": [{ "product": "mouse", "cost": 550},
    {"product": "laptop", "cost": 1000}
  ],
  "CA": [{ "product": "mouse", "cost": 750},
    {"product": "laptop", "cost": 1100}
  ]
}

Write a function called calculate_shipping_cost that takes an order and shipping cost matrix and returns the shipping cost.

Examples:

calculate_shipping_cost(order_us, shipping_cost) == 16000
calculate_shipping_cost(order_ca, shipping_cost) == 20500

This problem asks you to compute the total shipping cost for an order using a country-specific shipping matrix. The key idea is to look up the correct country first, then map each product to its shipping cost and sum quantity × unit cost across all items. Because prices are given in the smallest currency unit, the solution can use plain integer arithmetic without any decimal handling.

END
 0