PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

Implement tiered shipping calculator evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

  • Medium
  • Stripe
  • Coding & Algorithms
  • Software Engineer

Implement tiered shipping calculator

Company: Stripe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Implement in JavaScript a shipping-cost calculator with three progressively richer pricing models based on a configuration object keyed by destination. 1) Flat per‑unit price: given config[destination] = { unitPrice }, return unitPrice * quantity. 2) Tiered per‑unit pricing: given config[destination] = { tiers: [ { upTo: q1, unitPrice: p1 }, { upTo: q2, unitPrice: p2 }, ..., { upTo: Infinity, unitPrice: pk } ] }, compute the total by applying each tier only to the items that fall within that tier’s range; treat each upTo as inclusive and accumulate across tiers. 3) Flat base for the first n items plus tiers afterward: given config[destination] = { baseFlat: { upTo: n, amount: F }, tiers: [...] }, charge F once for quantities 1..n; for quantities > n, charge F for the first n items and then apply tiered pricing starting from item n+1. Carefully avoid off‑by‑one mistakes at n and other tier boundaries. Define the function signature calculateShipping(destination, quantity, config) and describe tests for edge cases (quantity 0, exactly at cutoffs, unknown location, negative inputs, floating‑point handling).

Quick Answer: Implement tiered shipping calculator evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Implement `calculateShipping(destination, quantity, config)` where `config[destination]` selects one of three progressively richer pricing models: 1. **Flat per-unit**: `{ unitPrice }` -> `unitPrice * quantity`. 2. **Tiered per-unit (graduated)**: `{ tiers: [ { upTo, unitPrice }, ... ] }`. Apply each tier only to the items that fall within its range, treating each `upTo` as inclusive and accumulating across tiers (the last tier's `upTo` is an unbounded sentinel such as Infinity / a very large number). 3. **Flat base then tiers**: `{ baseFlat: { upTo: n, amount: F }, tiers: [...] }`. Charge `F` once for quantities 1..n; for quantities > n, charge `F` for the first n items and then apply tiered pricing starting from item n+1. Watch the boundaries carefully (the n+1 transition and each tier `upTo`). For an unknown destination, return null/None; for quantity <= 0, return 0.

Constraints

  • destination is a string; quantity is an integer (may be 0 or negative).
  • Exactly one of unitPrice / tiers / baseFlat+tiers is present per destination.
  • Tiers are sorted ascending by upTo; the final tier's upTo is unbounded (Infinity or a large sentinel).
  • Each upTo is inclusive; tiers are graduated (marginal), not flat-rate-of-the-whole-order.
  • Unknown destination returns null/None; quantity <= 0 returns 0.

Examples

Input: ('US', 3, {'US': {'unitPrice': 5.0}})

Expected Output: 15.0

Explanation: Flat model: unitPrice 5.0 x 3 units = 15.0.

Input: ('US', 0, {'US': {'unitPrice': 5.0}})

Expected Output: 0.0

Explanation: Edge: quantity 0 always costs 0.0 regardless of model.

Input: ('US', 7, {'US': {'tiers': [{'upTo': 5, 'unitPrice': 10.0}, {'upTo': 10, 'unitPrice': 8.0}, {'upTo': 1000000, 'unitPrice': 6.0}]}})

Expected Output: 66.0

Explanation: Tiered: items 1-5 @10 (50) + items 6-7 @8 (16) = 66.0.

Input: ('US', 5, {'US': {'tiers': [{'upTo': 5, 'unitPrice': 10.0}, {'upTo': 10, 'unitPrice': 8.0}, {'upTo': 1000000, 'unitPrice': 6.0}]}})

Expected Output: 50.0

Explanation: Boundary: exactly at first cutoff (upTo 5, inclusive) -> 5 x 10 = 50.0.

Input: ('US', 10, {'US': {'tiers': [{'upTo': 5, 'unitPrice': 10.0}, {'upTo': 10, 'unitPrice': 8.0}, {'upTo': 1000000, 'unitPrice': 6.0}]}})

Expected Output: 90.0

Explanation: Boundary: at second cutoff -> items 1-5 @10 (50) + items 6-10 @8 (40) = 90.0.

Input: ('US', 12, {'US': {'tiers': [{'upTo': 5, 'unitPrice': 10.0}, {'upTo': 10, 'unitPrice': 8.0}, {'upTo': 1000000, 'unitPrice': 6.0}]}})

Expected Output: 102.0

Explanation: Spans all tiers: 5x10 + 5x8 + 2x6 = 50 + 40 + 12 = 102.0.

Input: ('US', 3, {'US': {'baseFlat': {'upTo': 3, 'amount': 20.0}, 'tiers': [{'upTo': 5, 'unitPrice': 10.0}, {'upTo': 10, 'unitPrice': 8.0}, {'upTo': 1000000, 'unitPrice': 6.0}]}})

Expected Output: 20.0

Explanation: baseFlat: quantity 3 == n, so just the flat fee F = 20.0 (no per-item tiering yet).

Input: ('US', 4, {'US': {'baseFlat': {'upTo': 3, 'amount': 20.0}, 'tiers': [{'upTo': 5, 'unitPrice': 10.0}, {'upTo': 10, 'unitPrice': 8.0}, {'upTo': 1000000, 'unitPrice': 6.0}]}})

Expected Output: 30.0

Explanation: Off-by-one boundary: F=20 for the first 3 items, then item 4 falls in tier1 @10 -> 20 + 10 = 30.0.

Input: ('US', 6, {'US': {'baseFlat': {'upTo': 3, 'amount': 20.0}, 'tiers': [{'upTo': 5, 'unitPrice': 10.0}, {'upTo': 10, 'unitPrice': 8.0}, {'upTo': 1000000, 'unitPrice': 6.0}]}})

Expected Output: 48.0

Explanation: F=20 + items 4-5 @10 (20) + item 6 @8 (8) = 48.0.

Input: ('US', 12, {'US': {'baseFlat': {'upTo': 3, 'amount': 20.0}, 'tiers': [{'upTo': 5, 'unitPrice': 10.0}, {'upTo': 10, 'unitPrice': 8.0}, {'upTo': 1000000, 'unitPrice': 6.0}]}})

Expected Output: 92.0

Explanation: F=20 + items 4-5 @10 (20) + items 6-10 @8 (40) + items 11-12 @6 (12) = 92.0.

Input: ('MARS', 5, {'US': {'unitPrice': 5.0}})

Expected Output: None

Explanation: Unknown destination is not present in config -> return None (null).

Input: ('US', -2, {'US': {'unitPrice': 5.0}})

Expected Output: 0.0

Explanation: Edge: negative quantity is non-positive -> 0.0.

Hints

  1. Dispatch on the shape of config[destination]: presence of unitPrice vs baseFlat vs tiers.
  2. For graduated tiers, track the previous tier's upTo; the current tier covers items (prev+1 .. min(upTo, quantity)).
  3. For the baseFlat model, charge F once, then start tier accounting at item n+1 — the classic off-by-one is charging item n twice or skipping item n+1.
  4. Handle quantity 0 / negative and unknown destination before touching tiers.
Last updated: Jun 26, 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
  • AI Coding 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)