PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates the ability to implement tiered billing and aggregate shipping costs across order line items, testing competencies in algorithm design, data modeling, and piecewise pricing logic including per-unit, incremental (progressive) tiers, and fixed-price tiers.

  • easy
  • Stripe
  • Coding & Algorithms
  • Data Engineer

Compute shipping cost with tiered pricing

Company: Stripe

Role: Data Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

You are implementing a shipping-cost calculator for an e-commerce checkout. An **order** contains multiple line items. Each line item has: - `country: str` (shipping destination) - `product_id: str` - `quantity: int` (assume `quantity >= 0`) You are also given shipping pricing data (how shipping is charged) and must compute the **total shipping cost** for the order as the **sum of shipping cost across all line items**. Implement the following three progressively harder versions. --- ## Part 1 — Fixed per-unit shipping You are given a table of fixed per-unit shipping prices: - `unit_price[country][product_id] -> decimal` For each line item, shipping cost is: - `quantity * unit_price[country][product_id]` **Task:** Write a function that returns the total shipping cost for the order. **Required output:** - A single number: `total_shipping_cost`. --- ## Part 2 — Incremental (progressive) tier pricing Now per-unit shipping price depends on the **unit index** within the quantity, like taxi pricing. Pricing is defined as tiers per `(country, product_id)`, each tier has: - `start_unit: int` (1-indexed, inclusive) - `end_unit: int` (inclusive, can be `+infinity`) - `unit_price: decimal` (applies per unit for units in this tier) Example tiers (for a single product/country): - units 1–3 cost $5 each - units 4–10 cost $4 each - units 11+ cost $3 each Then quantity 5 costs: `3*$5 + 2*$4`. **Task:** Compute total shipping cost for the order under this incremental scheme. **Clarification to handle:** - If the quantity falls entirely within one tier, the result should still follow the same rule (all units priced by that tier). --- ## Part 3 — Two pricing modes: incremental vs fixed-total-by-tier Now tiers can be of two different pricing modes for each `(country, product_id)`: 1) **Incremental tiers** (same as Part 2): per-unit price applied progressively by unit index. 2) **Fixed tiers**: if the **total quantity** falls within a tier `[start_unit, end_unit]`, then shipping cost for the entire line item is a fixed amount independent of quantity: - `fixed_price: decimal` **Rule:** For a line item, if there exists a **fixed tier** whose range contains the line item’s `quantity`, use that fixed price for the line item. Otherwise, fall back to the incremental calculation from Part 2. **Task:** Compute total shipping cost for the order under this combined rule. --- ## Notes / Assumptions - You may assume input data is well-formed unless you choose to handle errors. - State any additional assumptions you need (e.g., what to do if pricing for a `(country, product_id)` is missing). - Provide at least one simple test case that demonstrates correctness for each part.

Quick Answer: This question evaluates the ability to implement tiered billing and aggregate shipping costs across order line items, testing competencies in algorithm design, data modeling, and piecewise pricing logic including per-unit, incremental (progressive) tiers, and fixed-price tiers.

Part 1: Fixed Per-Unit Shipping Cost

You are given an order represented by parallel arrays of countries, product IDs, and quantities. You are also given a nested pricing table unit_price where unit_price[country][product_id] is the fixed shipping price per unit. Compute the total shipping cost by summing quantity * unit price for every line item.

Constraints

  • 0 <= len(countries) == len(product_ids) == len(quantities) <= 100000
  • 0 <= quantities[i] <= 1000000000
  • All prices are nonnegative numbers.
  • Pricing exists for every (country, product_id) pair appearing in the order.
  • Return the raw numeric total; no rounding is required.

Examples

Input: (['US', 'US', 'CA'], ['A', 'B', 'A'], [2, 1, 3], {'US': {'A': 5.0, 'B': 2.5}, 'CA': {'A': 7.0}})

Expected Output: 33.5

Explanation: US/A costs 2*5.0 = 10.0, US/B costs 1*2.5 = 2.5, and CA/A costs 3*7.0 = 21.0, for a total of 33.5.

Input: ([], [], [], {})

Expected Output: 0.0

Explanation: An empty order has no line items, so the total shipping cost is 0.

Input: (['US'], ['A'], [0], {'US': {'A': 9.99}})

Expected Output: 0.0

Explanation: A line item with quantity 0 contributes no cost.

Input: (['US', 'US'], ['A', 'A'], [1, 4], {'US': {'A': 3.25}})

Expected Output: 16.25

Explanation: Both line items are charged independently: 1*3.25 + 4*3.25 = 16.25.

Hints

  1. Each line item is independent, so compute its cost and add it to a running total.
  2. Use the country first, then the product ID, to look up the unit price in the nested pricing table.

Part 2: Incremental Progressive Tier Shipping Cost

You are given an order and a nested tiered pricing table. For each (country, product_id), shipping cost is charged progressively by unit index. For example, if units 1 through 3 cost 5 each and units 4 through 10 cost 4 each, then quantity 5 costs 3*5 + 2*4. Compute the total shipping cost across all line items.

Constraints

  • 0 <= len(countries) == len(product_ids) == len(quantities) <= 100000
  • 0 <= quantities[i] <= 1000000000
  • For each product pricing list, tiers are sorted by start_unit and do not overlap.
  • Incremental tiers start at unit 1, and end_unit == -1 may appear only as the final tier.
  • Pricing exists for every (country, product_id) pair appearing in the order.

Examples

Input: (['US'], ['A'], [5], {'US': {'A': [[1, 3, 5.0], [4, 10, 4.0], [11, -1, 3.0]]}})

Expected Output: 23.0

Explanation: Quantity 5 uses 3 units at 5.0 and 2 units at 4.0, giving 15.0 + 8.0 = 23.0.

Input: (['US'], ['A'], [2], {'US': {'A': [[1, 3, 5.0], [4, 10, 4.0], [11, -1, 3.0]]}})

Expected Output: 10.0

Explanation: The entire quantity falls within the first tier, so both units cost 5.0 each.

Input: (['US'], ['A'], [3], {'US': {'A': [[1, 3, 5.0], [4, 10, 4.0], [11, -1, 3.0]]}})

Expected Output: 15.0

Explanation: Quantity exactly equals the end of the first tier, so all 3 units cost 5.0 each.

Input: (['US', 'US', 'CA'], ['A', 'B', 'A'], [12, 1, 4], {'US': {'A': [[1, 3, 5.0], [4, 10, 4.0], [11, -1, 3.0]], 'B': [[1, -1, 2.5]]}, 'CA': {'A': [[1, 2, 6.0], [3, -1, 5.0]]}})

Expected Output: 73.5

Explanation: US/A quantity 12 costs 49.0, US/B quantity 1 costs 2.5, and CA/A quantity 4 costs 22.0.

Input: ([], [], [], {})

Expected Output: 0.0

Explanation: An empty order has total cost 0.

Hints

  1. For a line item with quantity q, only units 1 through q matter.
  2. For each tier, compute how many unit indices overlap with the interval [1, q].

Part 3: Mixed Fixed-Tier and Incremental Shipping Cost

You are given an order, incremental tier pricing, and fixed tier pricing. For each line item, first check whether a fixed tier contains the line item's total quantity. If so, use that fixed price for the entire line item. Otherwise, compute the line item cost using progressive incremental tiers as in Part 2. Return the total shipping cost across all line items.

Constraints

  • 0 <= len(countries) == len(product_ids) == len(quantities) <= 100000
  • 0 <= quantities[i] <= 1000000000
  • Incremental tiers for a product are sorted, non-overlapping, and start at unit 1.
  • Fixed tiers for a product are non-overlapping; if fixed_tiers omits a product, no fixed tier applies.
  • If no fixed tier applies to a line item, incremental pricing exists for that line item's (country, product_id).
  • A fixed tier may start at 0 to explicitly price quantity 0.

Examples

Input: (['US'], ['A'], [5], {'US': {'A': [[1, 3, 5.0], [4, 10, 4.0], [11, -1, 3.0]]}}, {'US': {'A': [[1, 5, 18.0], [6, 10, 30.0]]}})

Expected Output: 18.0

Explanation: Quantity 5 is inside the fixed tier [1, 5], so the line item costs 18.0 instead of the incremental cost 23.0.

Input: (['US'], ['A'], [12], {'US': {'A': [[1, 3, 5.0], [4, 10, 4.0], [11, -1, 3.0]]}}, {'US': {'A': [[1, 5, 18.0], [6, 10, 30.0]]}})

Expected Output: 49.0

Explanation: No fixed tier contains quantity 12, so incremental pricing is used: 3*5.0 + 7*4.0 + 2*3.0 = 49.0.

Input: (['CA'], ['A'], [0], {'CA': {'A': [[1, -1, 9.0]]}}, {'CA': {'A': [[0, 0, 0.0]]}})

Expected Output: 0.0

Explanation: Quantity 0 is explicitly covered by a fixed tier [0, 0] with fixed price 0.0.

Input: (['US', 'US', 'CA'], ['A', 'B', 'A'], [5, 3, 2], {'US': {'A': [[1, 3, 5.0], [4, 10, 4.0], [11, -1, 3.0]], 'B': [[1, -1, 2.0]]}, 'CA': {'A': [[1, 2, 6.0], [3, -1, 5.0]]}}, {'US': {'A': [[1, 5, 18.0]], 'B': [[3, 3, 5.0]]}})

Expected Output: 35.0

Explanation: US/A uses fixed 18.0, US/B quantity 3 uses fixed 5.0, and CA/A has no fixed tier so it uses incremental cost 12.0.

Input: ([], [], [], {}, {})

Expected Output: 0.0

Explanation: An empty order has total cost 0.

Hints

  1. The fixed-tier check should happen before any incremental calculation.
  2. If no fixed tier matches, reuse the same overlap calculation from progressive tier pricing.
Last updated: Jun 18, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Assign Reviewers from Changed Files - Stripe (medium)
  • Generate Account Email Notifications - Stripe (medium)
  • Calculate Transaction Fees - Stripe (medium)
  • Build an Account Transfer Ledger - Stripe (medium)
  • Implement Validation and String Compression - Stripe (hard)