Problem · Hash Table

Shipping Cost Calculator Part 1 - Fixed Per-Unit Price

Learn this problem
EasyStripe logoStripeINTERNFULLTIMEPHONE SCREEN
See Stripe hiring insights

Problem statement

Calculate the total shipping cost for an online order. The cost depends on the destination country and the product, and in this first version every product has a single fixed cost per unit.

Inputs:

  • country: the destination country code for the order.
  • items: a String[] where each entry is "product,quantity".
  • rules: a String[] where each entry is "country,product,cost" giving the fixed per-unit cost of a product in a country.

For each ordered item, multiply its quantity by the per-unit cost for that product in the order's country, and sum across all items. Return the total as an integer.

Function

calculateShippingCost(country: String, items: String[], rules: String[]) → int

Examples

Example 1

country = "US"items = ["mouse,20", "laptop,5"]rules = ["US,mouse,550", "US,laptop,1000", "CA,mouse,750", "CA,laptop,1100"]return = 16000
Using US rates: mouse 20 x 550 = 11000, laptop 5 x 1000 = 5000. Total = 16000.

Example 2

country = "CA"items = ["mouse,20", "laptop,5"]rules = ["US,mouse,550", "US,laptop,1000", "CA,mouse,750", "CA,laptop,1100"]return = 20500
Using CA rates: mouse 20 x 750 = 15000, laptop 5 x 1100 = 5500. Total = 20500.

Constraints

  • items entries are "product,quantity" with integer quantity.
  • rules entries are "country,product,cost" with integer cost (per unit).
  • Use the per-unit cost for the order's country and each product.
  • Total = sum over items of quantity * cost; return as an integer.

More Stripe problems

drafts saved locally
public int calculateShippingCost(String country, String[] items, String[] rules) {
  // write your code here
}
country"US"
items["mouse,20", "laptop,5"]
rules["US,mouse,550", "US,laptop,1000", "CA,mouse,750", "CA,laptop,1100"]
expected16000
checking account