PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

Evaluates implementation-level algorithmic logic for tiered pricing, covering per-unit aggregation, interval reasoning, and mixed incremental vs fixed pricing models within the Coding & Algorithms domain and Data Engineering role; abstraction level is practical, implementation-focused business logic and data transformation.

  • Stripe
  • Coding & Algorithms
  • Data Engineer

Compute shipping cost with tiered pricing

Company: Stripe

Role: Data Engineer

Category: Coding & Algorithms

Interview Round: Technical Screen

You are building a shipping-cost calculator. Each order contains line items, and shipping rules vary by **destination country** and **product**. Assume: - `OrderLine = { country: string, product: string, quantity: int }`, where `quantity >= 1`. - All costs are in the same currency. - Quantity tiers use integer boundaries and are **inclusive**. - If a required shipping rule is missing for a (country, product) pair, you may either throw an error or treat shipping cost as 0, but be consistent and document your choice. Implement the following progressively more complex versions. ### Part 1 — Fixed per-unit shipping You are given: - `orderLines: OrderLine[]` - `unitRates: { country: string, product: string, unit_price: number }[]` For each order line, shipping cost is `quantity * unit_price` for that (country, product). Return the **total shipping cost** across all order lines. ### Part 2 — Incremental tiered shipping (taxi-meter style) Now per-unit shipping depends on which **quantity interval** each unit falls into. You are given: - `orderLines: OrderLine[]` - `incrementalTiers: { country: string, product: string, start_qty: int, end_qty: int, unit_price: number }[]` For a given order line with quantity `Q`, compute shipping by charging each unit according to the tier covering that unit index. Example interpretation: if tiers are `[1–3 @ $5]` and `[4–10 @ $3]` and `Q=5`, cost is `3*$5 + 2*$3`. Return total shipping cost across all order lines. Also explain how your implementation handles the special case where the order quantity falls entirely within a single tier. ### Part 3 — Mixed pricing models: incremental vs fixed total Now tiers can be of two types: - **incremental**: same as Part 2 (per-unit charges by unit index) - **fixed**: if the **total quantity Q** falls within the tier `[start_qty, end_qty]`, the shipping cost for the entire line item is a **fixed total price** (independent of Q) You are given: - `orderLines: OrderLine[]` - `tiers: { country: string, product: string, start_qty: int, end_qty: int, pricing_type: 'incremental'|'fixed', price: number }[]` Rules to compute shipping for one order line with quantity `Q`: 1. If there exists a **fixed** tier whose interval covers `Q` (i.e., `start_qty <= Q <= end_qty`), use that tier’s `price` as the total shipping for that line item. 2. Otherwise, compute shipping using the **incremental** tiers as in Part 2. Return total shipping cost across all order lines. Write clean, correct code and include at least one meaningful test case for each part.

Quick Answer: Evaluates implementation-level algorithmic logic for tiered pricing, covering per-unit aggregation, interval reasoning, and mixed incremental vs fixed pricing models within the Coding & Algorithms domain and Data Engineering role; abstraction level is practical, implementation-focused business logic and data transformation.

Part 1: Fixed Per-Unit Shipping

You are given a list of order lines and a list of per-unit shipping rates. Each order line has a destination country, a product, and a quantity. For each order line, find the matching rate for the same (country, product) pair and add quantity * unit_price to the total. For this problem, use the following consistent rule for missing data: if a (country, product) pair does not appear in unit_rates, that order line contributes 0 shipping cost. Return the total shipping cost across all order lines.

Constraints

  • 0 <= len(order_lines), len(unit_rates) <= 100000
  • Each order line has quantity >= 1
  • unit_price is a non-negative int or float
  • For a given (country, product), assume unit_rates contains at most one matching rate
  • If a rate is missing for a (country, product) pair, treat its shipping cost as 0

Examples

Input: ([{'country': 'US', 'product': 'book', 'quantity': 2}, {'country': 'US', 'product': 'toy', 'quantity': 3}, {'country': 'CA', 'product': 'book', 'quantity': 1}], [{'country': 'US', 'product': 'book', 'unit_price': 4}, {'country': 'US', 'product': 'toy', 'unit_price': 2}, {'country': 'CA', 'product': 'book', 'unit_price': 5}])

Expected Output: 19

Explanation: Shipping is 2*4 + 3*2 + 1*5 = 19.

Input: ([{'country': 'JP', 'product': 'pen', 'quantity': 1}], [{'country': 'JP', 'product': 'pen', 'unit_price': 7}])

Expected Output: 7

Explanation: Single order line with the minimum quantity boundary.

Input: ([{'country': 'US', 'product': 'book', 'quantity': 2}, {'country': 'FR', 'product': 'game', 'quantity': 4}], [{'country': 'US', 'product': 'book', 'unit_price': 3}])

Expected Output: 6

Explanation: The FR/game rate is missing, so that line contributes 0. Total = 2*3 = 6.

Input: ([], [{'country': 'US', 'product': 'book', 'unit_price': 4}])

Expected Output: 0

Explanation: Edge case: no order lines means total shipping cost is 0.

Hints

  1. A dictionary keyed by (country, product) lets you find each rate in O(1) average time.
  2. First preprocess unit_rates, then make a single pass through order_lines.

Part 2: Incremental Tiered Shipping

You are given order lines and incremental shipping tiers. For a line with quantity Q, shipping is charged unit by unit based on which tier each unit index falls into. Example: if the tiers are [1-3 @ 5] and [4-10 @ 3], then Q = 5 costs 3*5 + 2*3 = 21. Return the total shipping cost across all order lines. Use this consistent rule for missing data: if a (country, product) pair has no tiers, or if some unit indexes are not covered by any tier, those missing units contribute 0 cost. Special case note: if the order quantity is fully covered by just one tier, the same overlap logic still works. That tier contributes all charged units, and every other tier contributes 0.

Constraints

  • 0 <= len(order_lines), len(incremental_tiers) <= 100000
  • Each order line has quantity >= 1
  • For each tier, 1 <= start_qty <= end_qty
  • unit_price is a non-negative int or float
  • For the same (country, product), incremental tiers do not overlap
  • If a pair is missing or some units are uncovered by tiers, those units cost 0

Examples

Input: ([{'country': 'US', 'product': 'book', 'quantity': 5}], [{'country': 'US', 'product': 'book', 'start_qty': 1, 'end_qty': 3, 'unit_price': 5}, {'country': 'US', 'product': 'book', 'start_qty': 4, 'end_qty': 10, 'unit_price': 3}])

Expected Output: 21

Explanation: Units 1-3 cost 5 each and units 4-5 cost 3 each, so total = 15 + 6 = 21.

Input: ([{'country': 'US', 'product': 'book', 'quantity': 5}, {'country': 'CA', 'product': 'toy', 'quantity': 4}], [{'country': 'US', 'product': 'book', 'start_qty': 1, 'end_qty': 3, 'unit_price': 5}, {'country': 'US', 'product': 'book', 'start_qty': 4, 'end_qty': 10, 'unit_price': 3}, {'country': 'CA', 'product': 'toy', 'start_qty': 1, 'end_qty': 2, 'unit_price': 1}, {'country': 'CA', 'product': 'toy', 'start_qty': 3, 'end_qty': 4, 'unit_price': 2}])

Expected Output: 27

Explanation: US/book costs 21. CA/toy costs 2*1 + 2*2 = 6. Total = 27.

Input: ([{'country': 'US', 'product': 'lamp', 'quantity': 5}], [{'country': 'US', 'product': 'lamp', 'start_qty': 1, 'end_qty': 5, 'unit_price': 4}])

Expected Output: 20

Explanation: Edge-style boundary case: the whole quantity falls into one inclusive tier, so cost = 5*4 = 20.

Input: ([], [{'country': 'US', 'product': 'book', 'start_qty': 1, 'end_qty': 3, 'unit_price': 5}])

Expected Output: 0

Explanation: Edge case: no order lines means total shipping cost is 0.

Hints

  1. Do not simulate each unit one by one. Instead, compute how many unit indexes from 1..Q overlap each tier interval.
  2. Group tiers by (country, product) so each order line only scans relevant tiers.

Part 3: Mixed Pricing Models - Fixed Total vs Incremental

You are given order lines and shipping tiers. Each tier belongs to a (country, product) pair and is one of two pricing types: - 'fixed': if the total quantity Q falls inside [start_qty, end_qty], the entire order line costs exactly price - 'incremental': each unit index covered by that tier costs price, just like the taxi-meter model from Part 2 For each order line with quantity Q, apply these rules in order: 1. If there is a fixed tier whose interval contains Q, use that tier's price as the total shipping cost for the whole line. 2. Otherwise, compute shipping from the incremental tiers by summing the overlapping unit counts. Return the total shipping cost across all order lines. Use this consistent rule for missing data: if a (country, product) pair has no usable tiers, or some unit indexes are uncovered when using incremental pricing, the missing portion contributes 0 cost.

Constraints

  • 0 <= len(order_lines), len(tiers) <= 100000
  • Each order line has quantity >= 1
  • For each tier, 1 <= start_qty <= end_qty
  • price is a non-negative int or float
  • For the same (country, product), fixed tiers do not overlap each other
  • For the same (country, product), incremental tiers do not overlap each other
  • If a fixed tier applies, ignore incremental tiers for that order line
  • If no fixed tier applies and some units are uncovered by incremental tiers, those units cost 0

Examples

Input: ([{'country': 'US', 'product': 'book', 'quantity': 6}], [{'country': 'US', 'product': 'book', 'start_qty': 6, 'end_qty': 10, 'pricing_type': 'fixed', 'price': 20}, {'country': 'US', 'product': 'book', 'start_qty': 1, 'end_qty': 3, 'pricing_type': 'incremental', 'price': 5}, {'country': 'US', 'product': 'book', 'start_qty': 4, 'end_qty': 10, 'pricing_type': 'incremental', 'price': 3}])

Expected Output: 20

Explanation: Boundary-inclusive fixed tier applies because Q = 6 is inside [6, 10], so the whole line costs 20.

Input: ([{'country': 'US', 'product': 'book', 'quantity': 5}], [{'country': 'US', 'product': 'book', 'start_qty': 6, 'end_qty': 10, 'pricing_type': 'fixed', 'price': 20}, {'country': 'US', 'product': 'book', 'start_qty': 1, 'end_qty': 3, 'pricing_type': 'incremental', 'price': 5}, {'country': 'US', 'product': 'book', 'start_qty': 4, 'end_qty': 10, 'pricing_type': 'incremental', 'price': 3}])

Expected Output: 21

Explanation: No fixed tier matches Q = 5, so use incremental pricing: 3*5 + 2*3 = 21.

Input: ([{'country': 'US', 'product': 'book', 'quantity': 6}, {'country': 'US', 'product': 'book', 'quantity': 4}, {'country': 'CA', 'product': 'toy', 'quantity': 2}], [{'country': 'US', 'product': 'book', 'start_qty': 6, 'end_qty': 10, 'pricing_type': 'fixed', 'price': 20}, {'country': 'US', 'product': 'book', 'start_qty': 1, 'end_qty': 3, 'pricing_type': 'incremental', 'price': 5}, {'country': 'US', 'product': 'book', 'start_qty': 4, 'end_qty': 10, 'pricing_type': 'incremental', 'price': 3}, {'country': 'CA', 'product': 'toy', 'start_qty': 1, 'end_qty': 1, 'pricing_type': 'fixed', 'price': 9}, {'country': 'CA', 'product': 'toy', 'start_qty': 1, 'end_qty': 5, 'pricing_type': 'incremental', 'price': 2}])

Expected Output: 42

Explanation: US/book qty 6 uses fixed = 20. US/book qty 4 uses incremental = 3*5 + 1*3 = 18. CA/toy qty 2 does not match its fixed tier, so it uses incremental = 2*2 = 4. Total = 42.

Input: ([], [{'country': 'US', 'product': 'book', 'start_qty': 6, 'end_qty': 10, 'pricing_type': 'fixed', 'price': 20}])

Expected Output: 0

Explanation: Edge case: no order lines means total shipping cost is 0.

Hints

  1. Store fixed tiers and incremental tiers separately for each (country, product) pair.
  2. For each order line, check fixed tiers first. Only if none match should you compute incremental overlap.
Last updated: May 7, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ 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

  • Calculate Transaction Fees - Stripe (medium)
  • Build an Account Transfer Ledger - Stripe (medium)
  • Implement Validation and String Compression - Stripe (hard)
  • Compute transaction fees from a CSV string - Stripe (hard)
  • Implement bitmap font render/compress/invert - Stripe (medium)