Shipping Cost Calculator Part 2 - Volume-Tiered Pricing
Updated
Problem statement
This continues the shipping cost calculator. Now each product's per-unit cost varies by quantity using tiered (volume-discount) pricing.
Inputs:
country: the destination country code.items: aString[]of"product,quantity".rules: aString[]of tier rules, each"country,product,minQuantity,maxQuantity,cost". AmaxQuantityof*means the tier has no upper limit.
For each item, walk that product's tiers in ascending order. The range of a tier is half-open [minQuantity, maxQuantity), so the tier's capacity is maxQuantity - minQuantity (unbounded if maxQuantity is *). Assign as many remaining units as fit into each tier (the smaller of remaining units and the tier capacity), charge units * cost for those units, and continue until all units are priced. Sum across all items and return the total integer cost.
Function
calculateTieredShippingCost(country: String, items: String[], rules: String[]) → intExamples
Example 1
country = "US"items = ["mouse,20", "laptop,5"]rules = ["US,mouse,0,*,550", "US,laptop,0,2,1000", "US,laptop,3,*,900", "CA,mouse,0,*,750", "CA,laptop,0,2,1100", "CA,laptop,3,*,1000"]return = 15700US rates. mouse: 20 x 550 = 11000 (single unbounded tier). laptop (5 units): tier [0,2) holds 2 units at 1000 = 2000, tier [3,*) holds the remaining 3 units at 900 = 2700, laptop total 4700. Grand total 11000 + 4700 = 15700.
Example 2
country = "CA"items = ["mouse,20", "laptop,5"]rules = ["US,mouse,0,*,550", "US,laptop,0,2,1000", "US,laptop,3,*,900", "CA,mouse,0,*,750", "CA,laptop,0,2,1100", "CA,laptop,3,*,1000"]return = 20200CA rates. mouse: 20 x 750 = 15000. laptop (5 units): tier [0,2) 2 units x 1100 = 2200, tier [3,*) 3 units x 1000 = 3000, laptop total 5200. Grand total 15000 + 5200 = 20200.
Constraints
rulesentries are"country,product,minQuantity,maxQuantity,cost";maxQuantity = *means unbounded.- Process a product's tiers from lowest to highest minQuantity.
- The tier range is half-open
[minQuantity, maxQuantity); capacity =maxQuantity - minQuantity. - Each tier prices
min(remaining units, tier capacity) * cost. - Return the total cost across all items as an integer.
More Stripe Phone Screen questions
- Merchant Payout Scheduler, Part 2: Per-Merchant SchedulesPHONE SCREEN · Seen Nov 2026
- Financial Account LedgerOA · PHONE SCREEN · Seen Nov 2026
- Business Account KYC Verification — Parts 1–5PHONE SCREEN · Seen Nov 2026
- Render a Task Tree from CSV RowsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Invoice / Payment ReconciliationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Linked Merchants Within Two HopsPHONE SCREEN · Seen Nov 2026
- Portfolio Valuation, Part 1PHONE SCREEN · Seen Nov 2026
- Account Roles With Parent-User InheritancePHONE SCREEN · Seen Nov 2026
- Merchant Payout Scheduler, Part 3: Hold PeriodsPHONE SCREEN · Seen Nov 2026
- Merchant Payout Scheduler, Part 1PHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- File Storage ServiceCresta · PHONE SCREENCresta interview questions
- String Interpolation with a Missing-Value ModeMongoDB · PHONE SCREENMongoDB interview questions
- Dasher Pay with Store Waits and Peak HoursDoorDash · PHONE SCREENDoorDash interview questions
- Return Every Matching DecryptionDuolingo · PHONE SCREENDuolingo interview questions
- Fixed-Order Task Run Time With CooldownWaymo · PHONE SCREENWaymo interview questions
- Character Counts with Retried Remote WorkersCoalition · PHONE SCREEN
- Route Pattern MatcherGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Risk-Priority Detection StoreCrowdstrike · PHONE SCREENCrowdstrike interview questions
- Environment Variable Store and SubstitutionAmazon · PHONE SCREEN