Design a tax and discount price calculator with the Decorator pattern
Quick Overview
Design a price calculator that stacks state, federal and luxury taxes and percentage discounts on a base price with the Decorator pattern, rounding to cents at each step and removing layers in LIFO order. It tests object-oriented design, exact money arithmetic and the cost of each operation.
Design a tax and discount price calculator with the Decorator pattern
Company: Intuit
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a tax/discount calculator for QuickBooks that dynamically applies different taxes and discounts to a base price using the Decorator pattern.
The calculator starts with a base price and allows stacking multiple taxes (State Tax, Federal Tax, Luxury Tax) and discounts on top of each other. Each decorator wraps the previous price and applies its modification in the order it was added.
Taxes increase the price by a given percentage, and discounts decrease the price by a given percentage. Prices are rounded to 2 decimal places after each decorator application. Decorators can also be removed in LIFO order (most recently added first).
Implement a class `TaxDiscountCalculator(base_price)` with the following methods:
- `add_state_tax(percentage: float)`: Add a State Tax decorator that increases the current price by the given percentage.
- `add_federal_tax(percentage: float)`: Add a Federal Tax decorator that increases the current price by the given percentage.
- `add_luxury_tax(percentage: float)`: Add a Luxury Tax decorator that increases the current price by the given percentage.
- `add_discount(percentage: float)`: Add a Discount decorator that decreases the current price by the given percentage.
- `get_price() -> float`: Compute and return the final price after applying all stacked decorators in order, rounded to 2 decimal places at each step.
- `remove_last_decorator() -> bool`: Remove the most recently added decorator. Return True if a decorator was removed, False if no decorators exist.
- `get_breakdown() -> List[str]`: Return a list of strings describing each step of the price computation, starting with the base price and showing the price after each decorator.
- `get_decorator_count() -> int`: Return the number of decorators currently applied.
Each `get_breakdown` line uses this format, with every price shown to two decimal places:
```text
Base Price: <price>
+ <tax name> (<percentage>%): <price after this tax>
- Discount (<percentage>%): <price after this discount>
```
A whole-number percentage prints without a decimal part, so `10` prints as `10%`.
```hint Shape of the pattern
Start from the interface that the base price and a taxed or discounted price have in common. What does each wrapper need to know about the thing inside it, and what does removing the newest one do to that structure?
```
```hint Money arithmetic
Try `round(2.675, 2)` in Python before you trust binary floats with cents.
```
### Examples
Example 1:
```text
Input:
["TaxDiscountCalculator", "add_state_tax", "add_discount", "add_federal_tax", "add_luxury_tax", "get_price", "get_decorator_count", "get_breakdown", "remove_last_decorator", "get_price"]
[[100], [10], [20], [5], [3], [], [], [], [], []]
Output: [null, null, null, null, null, 95.17, 4, ["Base Price: 100.00", "+ State Tax (10%): 110.00", "- Discount (20%): 88.00", "+ Federal Tax (5%): 92.40", "+ Luxury Tax (3%): 95.17"], true, 92.4]
calc = TaxDiscountCalculator(100.0) # base price = 100.00
calc.add_state_tax(10) # 100.00 * 1.10 = 110.00
calc.add_discount(20) # 110.00 * 0.80 = 88.00
calc.add_federal_tax(5) # 88.00 * 1.05 = 92.40
calc.add_luxury_tax(3) # 92.40 * 1.03 = 95.172, rounded to 95.17
calc.get_price() # -> 95.17
calc.get_decorator_count() # -> 4
calc.get_breakdown() # -> the base price line, then one line per decorator
calc.remove_last_decorator() # -> True (luxury tax removed)
calc.get_price() # -> 92.4 (back to the price after the federal tax)
```
The luxury tax step shows the per-step rounding: 95.172 becomes 95.17 before anything else is applied.
Example 2:
```text
Input:
["TaxDiscountCalculator", "get_price", "remove_last_decorator", "add_luxury_tax", "get_price", "remove_last_decorator", "get_price", "get_decorator_count"]
[[250], [], [], [20], [], [], [], []]
Output: [null, 250, false, null, 300, true, 250, 0]
calc = TaxDiscountCalculator(250.0) # base price = 250.0
calc.get_price() # -> 250.0 (no decorators, returns base price)
calc.remove_last_decorator() # -> False (no decorators to remove)
calc.add_luxury_tax(20) # adds 20% luxury tax decorator
calc.get_price() # -> 300.0 (250.0 * 1.20 = 300.0)
calc.remove_last_decorator() # -> True (removed luxury tax)
calc.get_price() # -> 250.0 (back to base price)
calc.get_decorator_count() # -> 0 (no decorators remaining)
```
### Constraints & Assumptions
- `1 <= base_price <= 10^6`
- `0 <= percentage <= 100`
- Percentages are given as whole numbers (`10` means 10%).
- At most `1000` operations will be made.
- Prices are rounded to 2 decimal places after each decorator application.
### Clarifying Questions to Ask
- When a step lands exactly on half a cent, should it round half up or half to even?
- Does `get_price` have to return a float, or may the calculator keep an exact decimal type internally and convert only at the boundary?
- Can the same kind of tax be stacked twice (two state taxes), or should a duplicate be rejected?
- Are other adjustment types coming later, such as a fixed-amount coupon or a tax with a threshold?
### What a Strong Answer Covers
- A shared component interface implemented by the base price and by every tax or discount decorator, with each decorator holding the component it wraps
- LIFO removal by unwrapping the outermost decorator, including the case where nothing is left to remove
- Rounding to cents after every step with exact decimal arithmetic and a stated rule for half-cent ties
- The breakdown in application order, and the time and space cost of each operation
- How a new adjustment type is added without editing the existing classes
### Follow-up Questions
- How would you add a fixed-amount coupon (for example \$5 off) or a tax that applies only above a price threshold?
- Suppose the business rule is that every discount applies before any tax. How would you enforce that order whatever order the caller adds them in?
- How would you remove a specific decorator from the middle of the stack, and what does that cost in your design?
- When would you drop the Decorator pattern for a plain list of adjustment rules applied in a loop, and what do you give up?
Overview: Design a price calculator that stacks state, federal and luxury taxes and percentage discounts on a base price with the Decorator pattern, rounding to cents at each step and removing layers in LIFO order. It tests object-oriented design, exact money arithmetic and the cost of each operation.
Design a tax/discount calculator for QuickBooks that dynamically applies different taxes and discounts to a base price using the Decorator pattern.
The calculator starts with a base price and allows stacking multiple taxes (State Tax, Federal Tax, Luxury Tax) and discounts on top of each other. Each decorator wraps the previous price and applies its modification in the order it was added.
Taxes increase the price by a given percentage, and discounts decrease the price by a given percentage. Prices are rounded to 2 decimal places after each decorator application. Decorators can also be removed in LIFO order (most recently added first).
Implement a class TaxDiscountCalculator(base_price) with the following methods:
add_state_tax(percentage: float)
: Add a State Tax decorator that increases the current price by the given percentage.
add_federal_tax(percentage: float)
: Add a Federal Tax decorator that increases the current price by the given percentage.
add_luxury_tax(percentage: float)
: Add a Luxury Tax decorator that increases the current price by the given percentage.
add_discount(percentage: float)
: Add a Discount decorator that decreases the current price by the given percentage.
get_price() -> float
: Compute and return the final price after applying all stacked decorators in order, rounded to 2 decimal places at each step.
remove_last_decorator() -> bool
: Remove the most recently added decorator. Return True if a decorator was removed, False if no decorators exist.
get_breakdown() -> List[str]
: Return a list of strings describing each step of the price computation, starting with the base price and showing the price after each decorator.
get_decorator_count() -> int
: Return the number of decorators currently applied.
Each get_breakdown line uses this format, with every price shown to two decimal places:
Base Price: <price>
+ <tax name> (<percentage>%): <price after this tax>
- Discount (<percentage>%): <price after this discount>
A whole-number percentage prints without a decimal part, so 10 prints as 10%.
Examples
Example 1:
Input:
["TaxDiscountCalculator", "add_state_tax", "add_discount", "add_federal_tax", "add_luxury_tax", "get_price", "get_decorator_count", "get_breakdown", "remove_last_decorator", "get_price"]
[[100], [10], [20], [5], [3], [], [], [], [], []]
Output: [null, null, null, null, null, 95.17, 4, ["Base Price: 100.00", "+ State Tax (10%): 110.00", "- Discount (20%): 88.00", "+ Federal Tax (5%): 92.40", "+ Luxury Tax (3%): 95.17"], true, 92.4]
calc = TaxDiscountCalculator(100.0) # base price = 100.00
calc.add_state_tax(10) # 100.00 * 1.10 = 110.00
calc.add_discount(20) # 110.00 * 0.80 = 88.00
calc.add_federal_tax(5) # 88.00 * 1.05 = 92.40
calc.add_luxury_tax(3) # 92.40 * 1.03 = 95.172, rounded to 95.17
calc.get_price() # -> 95.17
calc.get_decorator_count() # -> 4
calc.get_breakdown() # -> the base price line, then one line per decorator
calc.remove_last_decorator() # -> True (luxury tax removed)
calc.get_price() # -> 92.4 (back to the price after the federal tax)
The luxury tax step shows the per-step rounding: 95.172 becomes 95.17 before anything else is applied.