Design a sales tax calculator with exemptions, import duty and tax rounded up to 0.05

Quick Overview

Design a sales tax calculator for a shopping basket with a 10% basic tax, exempt categories, a 5% import duty and per-unit tax rounded up to the nearest 0.05. It tests object-oriented design for pluggable tax rules, exact money arithmetic and receipt formatting.

Design a sales tax calculator with exemptions, import duty and tax rounded up to 0.05

Company: Thoughtworks

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a sales tax calculator that computes the total cost of a shopping basket, including applicable taxes. Basic sales tax applies at a rate of 10% on all goods except books, food and medical products, which are exempt. An additional import duty of 5% applies to all imported goods, with no exemptions. Tax is computed per unit. Add up the rates that apply to an item (0%, 5%, 10% or 15%), multiply the unit price by that rate and round the result up to the nearest 0.05. The item's tax is that rounded unit tax times the quantity. The calculator supports adding items to a basket and generating a receipt with itemized prices (including tax), the total sales tax paid and the total cost. Implement the following methods: - `add_item(quantity: int, name: str, unit_price: float, is_imported: bool, category: str)`: Adds an item to the basket. `category` is one of `'book'`, `'food'`, `'medical'` or `'other'`. Imported items incur the 5% import duty. Non-exempt items incur the 10% basic sales tax. - `get_receipt() -> List[str]`: Returns the receipt as a list of strings. Each `add_item` call produces one line, in the order the items were added, of the form `<quantity> <name>: <line total with tax>`. The last two lines are `Sales Taxes: <total tax>` and `Total: <total cost>`. Every amount is shown with exactly two decimal places. - `get_total_tax() -> float`: Returns the total sales tax for all items currently in the basket, rounded to 2 decimal places. - `get_total_cost() -> float`: Returns the total cost including tax for all items in the basket, rounded to 2 decimal places. - `clear()`: Removes all items, leaving the basket empty. Example 1: ```text add_item(2, "book", 12.49, False, "book") add_item(1, "music CD", 14.99, False, "other") add_item(1, "chocolate bar", 0.85, False, "food") get_receipt() -> [ "2 book: 24.98", "1 music CD: 16.49", "1 chocolate bar: 0.85", "Sales Taxes: 1.50", "Total: 42.32" ] get_total_tax() -> 1.5 get_total_cost() -> 42.32 ``` Only the music CD is taxed. 10% of 14.99 is 1.499, which rounds up to 1.50, so its line is 16.49. The books and the chocolate bar are exempt, and two books at 12.49 come to 24.98. Example 2: ```text add_item(1, "imported bottle of perfume", 27.99, True, "other") add_item(1, "bottle of perfume", 18.99, False, "other") add_item(1, "packet of headache pills", 9.75, False, "medical") add_item(3, "imported boxes of chocolates", 11.25, True, "food") get_receipt() -> [ "1 imported bottle of perfume: 32.19", "1 bottle of perfume: 20.89", "1 packet of headache pills: 9.75", "3 imported boxes of chocolates: 35.55", "Sales Taxes: 7.90", "Total: 98.38" ] clear() get_receipt() -> ["Sales Taxes: 0.00", "Total: 0.00"] ``` The imported perfume pays 15%, and 4.1985 rounds up to 4.20. The chocolates are food, so only the 5% duty applies: 0.5625 per box rounds up to 0.60, and three boxes pay 1.80 on top of 33.75. Rounding the whole line's tax of 1.6875 would give 1.70, so the per-unit rule changes the answer. After `clear()`, the receipt holds only the two total lines. ```hint Exact amounts Check what your rounding step does when the exact tax is already a multiple of 0.05, such as an imported, non-exempt item priced at 15.00. Pick a money representation in which that value cannot drift upward before you round. ``` ```hint Where the tax rules live Each tax only needs to say whether it applies to an item and at what rate. Keep that apart from the basket and from receipt formatting, so adding a new tax does not mean editing the calculator. ``` ### Constraints & Assumptions - `1 <= quantity <= 100` - `0.01 <= unit_price <= 10000.00`, given to the cent. - Product names are non-empty strings. - `category` is one of `'book'`, `'food'`, `'medical'` or `'other'`. - At most 1000 items per basket. ### Clarifying Questions to Ask - Will more taxes or exempt categories be added later, and should adding one leave the calculator class untouched? - If the same product is added twice, should the receipt merge the two lines or keep them separate? - Do prices arrive as floats or as decimal strings, and may the calculator store them internally as integer cents? ### What a Strong Answer Covers - The tax rule as stated: the applicable rates combined into one, applied to the unit price, rounded up to the next 0.05 per unit, then multiplied by the quantity. - A money representation that keeps the round-up exact, with a reason binary floating point is risky here. - A class structure that separates tax rules, basket storage and receipt formatting, so a new tax can be added without changing existing code. - Receipt lines and totals that always agree with `get_total_tax` and `get_total_cost`, formatted to two decimals. - Edge cases such as an empty basket, `clear`, an imported exempt item and a quantity above one. ### Follow-up Questions - How would your design handle a tax that is a flat amount per unit, such as a bottle deposit, alongside the percentage taxes? - If a jurisdiction rounds tax once per receipt line or once per receipt, what changes in your code? - How would you load exemptions from configuration so that a product can be exempt in one region and taxed in another? - Which prices would you pick to test the rounding rule, and why?

Overview: Design a sales tax calculator for a shopping basket with a 10% basic tax, exempt categories, a 5% import duty and per-unit tax rounded up to the nearest 0.05. It tests object-oriented design for pluggable tax rules, exact money arithmetic and receipt formatting.

Thoughtworks logo
Thoughtworks
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a sales tax calculator that computes the total cost of a shopping basket, including applicable taxes.

Basic sales tax applies at a rate of 10% on all goods except books, food and medical products, which are exempt. An additional import duty of 5% applies to all imported goods, with no exemptions.

Tax is computed per unit. Add up the rates that apply to an item (0%, 5%, 10% or 15%), multiply the unit price by that rate and round the result up to the nearest 0.05. The item's tax is that rounded unit tax times the quantity. The calculator supports adding items to a basket and generating a receipt with itemized prices (including tax), the total sales tax paid and the total cost.

Implement the following methods:

  • add_item(quantity: int, name: str, unit_price: float, is_imported: bool, category: str) : Adds an item to the basket. category is one of 'book' , 'food' , 'medical' or 'other' . Imported items incur the 5% import duty. Non-exempt items incur the 10% basic sales tax.
  • get_receipt() -> List[str] : Returns the receipt as a list of strings. Each add_item call produces one line, in the order the items were added, of the form <quantity> <name>: <line total with tax> . The last two lines are Sales Taxes: <total tax> and Total: <total cost> . Every amount is shown with exactly two decimal places.
  • get_total_tax() -> float : Returns the total sales tax for all items currently in the basket, rounded to 2 decimal places.
  • get_total_cost() -> float : Returns the total cost including tax for all items in the basket, rounded to 2 decimal places.
  • clear() : Removes all items, leaving the basket empty.

Example 1:

add_item(2, "book", 12.49, False, "book")
add_item(1, "music CD", 14.99, False, "other")
add_item(1, "chocolate bar", 0.85, False, "food")
get_receipt() -> [
    "2 book: 24.98",
    "1 music CD: 16.49",
    "1 chocolate bar: 0.85",
    "Sales Taxes: 1.50",
    "Total: 42.32"
]
get_total_tax()  -> 1.5
get_total_cost() -> 42.32

Only the music CD is taxed. 10% of 14.99 is 1.499, which rounds up to 1.50, so its line is 16.49. The books and the chocolate bar are exempt, and two books at 12.49 come to 24.98.

Example 2:

add_item(1, "imported bottle of perfume", 27.99, True, "other")
add_item(1, "bottle of perfume", 18.99, False, "other")
add_item(1, "packet of headache pills", 9.75, False, "medical")
add_item(3, "imported boxes of chocolates", 11.25, True, "food")
get_receipt() -> [
    "1 imported bottle of perfume: 32.19",
    "1 bottle of perfume: 20.89",
    "1 packet of headache pills: 9.75",
    "3 imported boxes of chocolates: 35.55",
    "Sales Taxes: 7.90",
    "Total: 98.38"
]
clear()
get_receipt() -> ["Sales Taxes: 0.00", "Total: 0.00"]

The imported perfume pays 15%, and 4.1985 rounds up to 4.20. The chocolates are food, so only the 5% duty applies: 0.5625 per box rounds up to 0.60, and three boxes pay 1.80 on top of 33.75. Rounding the whole line's tax of 1.6875 would give 1.70, so the per-unit rule changes the answer. After clear(), the receipt holds only the two total lines.

Constraints & Assumptions

  • 1 <= quantity <= 100
  • 0.01 <= unit_price <= 10000.00 , given to the cent.
  • Product names are non-empty strings.
  • category is one of 'book' , 'food' , 'medical' or 'other' .
  • At most 1000 items per basket.

Clarifying Questions to Ask Guidance

  • Will more taxes or exempt categories be added later, and should adding one leave the calculator class untouched?
  • If the same product is added twice, should the receipt merge the two lines or keep them separate?
  • Do prices arrive as floats or as decimal strings, and may the calculator store them internally as integer cents?

What a Strong Answer Covers Guidance

  • The tax rule as stated: the applicable rates combined into one, applied to the unit price, rounded up to the next 0.05 per unit, then multiplied by the quantity.
  • A money representation that keeps the round-up exact, with a reason binary floating point is risky here.
  • A class structure that separates tax rules, basket storage and receipt formatting, so a new tax can be added without changing existing code.
  • Receipt lines and totals that always agree with get_total_tax and get_total_cost , formatted to two decimals.
  • Edge cases such as an empty basket, clear , an imported exempt item and a quantity above one.

Follow-up Questions Guidance

  • How would your design handle a tax that is a flat amount per unit, such as a bottle deposit, alongside the percentage taxes?
  • If a jurisdiction rounds tax once per receipt line or once per receipt, what changes in your code?
  • How would you load exemptions from configuration so that a product can be exempt in one region and taxed in another?
  • Which prices would you pick to test the rounding rule, and why?