Problem · Hash Table

Shipping Cost Calculator Part 3 - Mixed Fixed/Incremental Tiers

MediumStripeONSITE INTERVIEW
See Stripe hiring insights

This continues the shipping cost calculator. Now a product's tiers can each charge in one of two ways:

  • incremental: charge per unit, i.e. units_in_tier * cost (same as Part 2).
  • fixed: charge a single flat cost for the whole tier, regardless of how many units fall into it (as long as at least one unit enters the tier).

A single product can mix the two types across its tiers.

Inputs: country; items as a String[] of "product,quantity"; and rules as a String[] of "country,product,type,minQuantity,maxQuantity,cost" where type is fixed or incremental and maxQuantity = * means unbounded. Tiers use the half-open range [minQuantity, maxQuantity) with capacity maxQuantity - minQuantity. Walk tiers in order, assign min(remaining, capacity) units to each, apply the tier's pricing type, and sum across all items. Return the total integer cost.

Examples
01 · Example 1
country = "US"
items = ["mouse,20", "laptop,5"]
rules = ["US,mouse,incremental,0,*,550", "US,laptop,fixed,0,2,1000", "US,laptop,incremental,3,*,900", "CA,mouse,incremental,0,*,750", "CA,laptop,fixed,0,2,1100", "CA,laptop,incremental,3,*,1000"]
return = 14700
US rates. mouse: 20 x 550 = 11000 (incremental). laptop (5 units): tier [0,2) is fixed -> flat 1000 for the first 2 units; tier [3,*) is incremental -> 3 x 900 = 2700; laptop total 1000 + 2700 = 3700. Grand total 11000 + 3700 = 14700.
02 · Example 2
country = "CA"
items = ["mouse,20", "laptop,5"]
rules = ["US,mouse,incremental,0,*,550", "US,laptop,fixed,0,2,1000", "US,laptop,incremental,3,*,900", "CA,mouse,incremental,0,*,750", "CA,laptop,fixed,0,2,1100", "CA,laptop,incremental,3,*,1000"]
return = 19100
CA rates. mouse: 20 x 750 = 15000 (incremental). laptop (5 units): fixed tier [0,2) -> flat 1100; incremental tier [3,*) -> 3 x 1000 = 3000; laptop total 4100. Grand total 15000 + 4100 = 19100.
Constraints
  • rules entries are "country,product,type,minQuantity,maxQuantity,cost"; type is fixed or incremental; maxQuantity = * is unbounded.
  • Process a product's tiers in ascending order; range is half-open [minQuantity, maxQuantity).
  • fixed tiers add cost once when at least one unit enters the tier; incremental tiers add units * cost.
  • A product can mix fixed and incremental tiers.
  • Return the total cost across all items as an integer.
More Stripe problems
drafts saved locally
public int calculateMixedShippingCost(String country, String[] items, String[] rules) {
  // write your code here
}
country"US"
items["mouse,20", "laptop,5"]
rules["US,mouse,incremental,0,*,550", "US,laptop,fixed,0,2,1000", "US,laptop,incremental,3,*,900", "CA,mouse,incremental,0,*,750", "CA,laptop,fixed,0,2,1100", "CA,laptop,incremental,3,*,1000"]
expected14700
sign in to submit