Implement a pizza pricing class with duplicate toppings and a bulk topping discount

Quick Overview

Implement a pizza pricing class that adds and removes toppings, lists them in order and takes a percentage off the topping cost once a count threshold is met. Tests class design, handling duplicate entries, and applying discounts and rounding in the right order.

Implement a pizza pricing class with duplicate toppings and a bulk topping discount

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Design a pizza pricing system that calculates the total price of a pizza with various toppings, inspired by the Decorator pattern. Each topping is layered on top of a base pizza, and the system computes the final price by combining the base cost with all topping costs. The system should support adding and removing toppings (including duplicate toppings), querying the current total price, and listing the current toppings. Duplicate topping names are allowed and each instance is tracked independently. The system also supports a bulk discount: when a bulk discount is configured with a threshold and percentage, and the number of toppings meets or exceeds the threshold, the discount percentage is applied to the total topping cost only (the base price is not discounted). The total price is always rounded to 2 decimal places. Implement the following methods: - `PizzaPricingSystem(base_price)` creates a pizza with the given base price and no toppings. - `add_topping(name, price)` adds a topping with the given name and price. Duplicate names are allowed and each instance is tracked separately. - `remove_topping(name)` removes one instance of the topping with the given name. It returns `True` if a topping was removed and `False` if no topping with that name exists. - `get_total()` returns the total price of the pizza (base price plus topping costs, with any applicable bulk discount on the toppings), rounded to 2 decimal places. - `get_toppings()` returns the names of the toppings currently on the pizza, in the order they were added. - `get_topping_count()` returns the number of toppings currently on the pizza. - `set_bulk_discount(threshold, percentage)` sets a bulk discount policy. When the number of toppings is greater than or equal to `threshold`, a discount of `percentage` percent is applied to the total topping cost (not the base price). ### Function Signature ```python class PizzaPricingSystem: def __init__(self, base_price: float) -> None: ... def add_topping(self, name: str, price: float) -> None: ... def remove_topping(self, name: str) -> bool: ... def get_total(self) -> float: ... def get_toppings(self) -> list[str]: ... def get_topping_count(self) -> int: ... def set_bulk_discount(self, threshold: int, percentage: float) -> None: ... ``` ### Rules - Toppings with the same name are separate instances and may have different prices. `remove_topping` removes the earliest-added instance that is still on the pizza. When no instance matches, it returns `False` and changes nothing. - No discount applies until `set_bulk_discount` is called. A later call replaces the earlier policy. The policy stays in force as toppings are added or removed, and it is checked on every `get_total` call. - When the current topping count (duplicates included) is at least `threshold`, the topping cost is multiplied by `1 - percentage / 100`. A threshold of 0 therefore applies the discount to every pizza. - `get_total` rounds once, at the end: `round(base_price + discounted_topping_cost, 2)`. - In the examples, the first array lists the calls, the second their arguments, and the output array holds each call's return value, with `null` for the constructor and for methods that return nothing. ### Constraints - `1 <= base_price <= 1000.0` - `0 <= price <= 100.0` for every topping - Topping names are non-empty strings. - `0 <= percentage <= 100` - `0 <= threshold <= 50` - At most 1000 method calls in total. ### Examples ```text Input: ["PizzaPricingSystem", "add_topping", "add_topping", "add_topping", "set_bulk_discount", "get_total", "remove_topping", "get_total", "get_topping_count"] [[12.0], ["cheese", 1.5], ["mushroom", 2.0], ["ham", 3.0], [3, 20], [], ["ham"], [], []] Output: [null, null, null, null, null, 17.2, true, 15.5, 2] ``` Three toppings meet the threshold of 3, so their cost of 6.5 drops by 20 percent to 5.2, and 12.0 + 5.2 = 17.2. After ham is removed only two toppings remain, below the threshold, so the total is 12.0 + 3.5 = 15.5. ```text Input: ["PizzaPricingSystem", "get_total", "add_topping", "add_topping", "remove_topping", "get_total", "remove_topping", "get_toppings"] [[10.0], [], ["olives", 0.75], ["olives", 0.75], ["olives"], [], ["pepperoni"], []] Output: [null, 10.0, null, null, true, 10.75, false, ["olives"]] ``` With no toppings the total is the base price. One of the two olives instances is removed, leaving 10.0 + 0.75 = 10.75, and removing pepperoni fails because none was added. ```text Input: ["PizzaPricingSystem", "add_topping", "add_topping", "add_topping", "remove_topping", "get_toppings", "set_bulk_discount", "get_total"] [[9.99], ["pepper", 0.5], ["onion", 0.25], ["pepper", 1.25], ["pepper"], [], [2, 10], []] Output: [null, null, null, null, true, ["onion", "pepper"], null, 11.34] ``` Removing pepper takes the first one added (price 0.5), so the 1.25 pepper stays. Two toppings meet the threshold of 2, so the total is 9.99 + (0.25 + 1.25) * 0.9 = 9.99 + 1.35 = 11.34.

Overview: Implement a pizza pricing class that adds and removes toppings, lists them in order and takes a percentage off the topping cost once a count threshold is met. Tests class design, handling duplicate entries, and applying discounts and rounding in the right order.

Amazon logo
Amazon
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Design a pizza pricing system that calculates the total price of a pizza with various toppings, inspired by the Decorator pattern. Each topping is layered on top of a base pizza, and the system computes the final price by combining the base cost with all topping costs.

The system should support adding and removing toppings (including duplicate toppings), querying the current total price, and listing the current toppings. Duplicate topping names are allowed and each instance is tracked independently.

The system also supports a bulk discount: when a bulk discount is configured with a threshold and percentage, and the number of toppings meets or exceeds the threshold, the discount percentage is applied to the total topping cost only (the base price is not discounted). The total price is always rounded to 2 decimal places.

Implement the following methods:

  • PizzaPricingSystem(base_price) creates a pizza with the given base price and no toppings.
  • add_topping(name, price) adds a topping with the given name and price. Duplicate names are allowed and each instance is tracked separately.
  • remove_topping(name) removes one instance of the topping with the given name. It returns True if a topping was removed and False if no topping with that name exists.
  • get_total() returns the total price of the pizza (base price plus topping costs, with any applicable bulk discount on the toppings), rounded to 2 decimal places.
  • get_toppings() returns the names of the toppings currently on the pizza, in the order they were added.
  • get_topping_count() returns the number of toppings currently on the pizza.
  • set_bulk_discount(threshold, percentage) sets a bulk discount policy. When the number of toppings is greater than or equal to threshold , a discount of percentage percent is applied to the total topping cost (not the base price).

Function Signature

class PizzaPricingSystem:
    def __init__(self, base_price: float) -> None: ...
    def add_topping(self, name: str, price: float) -> None: ...
    def remove_topping(self, name: str) -> bool: ...
    def get_total(self) -> float: ...
    def get_toppings(self) -> list[str]: ...
    def get_topping_count(self) -> int: ...
    def set_bulk_discount(self, threshold: int, percentage: float) -> None: ...

Rules

  • Toppings with the same name are separate instances and may have different prices. remove_topping removes the earliest-added instance that is still on the pizza. When no instance matches, it returns False and changes nothing.
  • No discount applies until set_bulk_discount is called. A later call replaces the earlier policy. The policy stays in force as toppings are added or removed, and it is checked on every get_total call.
  • When the current topping count (duplicates included) is at least threshold , the topping cost is multiplied by 1 - percentage / 100 . A threshold of 0 therefore applies the discount to every pizza.
  • get_total rounds once, at the end: round(base_price + discounted_topping_cost, 2) .
  • In the examples, the first array lists the calls, the second their arguments, and the output array holds each call's return value, with null for the constructor and for methods that return nothing.

Constraints

  • 1 <= base_price <= 1000.0
  • 0 <= price <= 100.0 for every topping
  • Topping names are non-empty strings.
  • 0 <= percentage <= 100
  • 0 <= threshold <= 50
  • At most 1000 method calls in total.

Examples

Input:
["PizzaPricingSystem", "add_topping", "add_topping", "add_topping", "set_bulk_discount", "get_total", "remove_topping", "get_total", "get_topping_count"]
[[12.0], ["cheese", 1.5], ["mushroom", 2.0], ["ham", 3.0], [3, 20], [], ["ham"], [], []]
Output: [null, null, null, null, null, 17.2, true, 15.5, 2]

Three toppings meet the threshold of 3, so their cost of 6.5 drops by 20 percent to 5.2, and 12.0 + 5.2 = 17.2. After ham is removed only two toppings remain, below the threshold, so the total is 12.0 + 3.5 = 15.5.

Input:
["PizzaPricingSystem", "get_total", "add_topping", "add_topping", "remove_topping", "get_total", "remove_topping", "get_toppings"]
[[10.0], [], ["olives", 0.75], ["olives", 0.75], ["olives"], [], ["pepperoni"], []]
Output: [null, 10.0, null, null, true, 10.75, false, ["olives"]]

With no toppings the total is the base price. One of the two olives instances is removed, leaving 10.0 + 0.75 = 10.75, and removing pepperoni fails because none was added.

Input:
["PizzaPricingSystem", "add_topping", "add_topping", "add_topping", "remove_topping", "get_toppings", "set_bulk_discount", "get_total"]
[[9.99], ["pepper", 0.5], ["onion", 0.25], ["pepper", 1.25], ["pepper"], [], [2, 10], []]
Output: [null, null, null, null, true, ["onion", "pepper"], null, 11.34]

Removing pepper takes the first one added (price 0.5), so the 1.25 pepper stays. Two toppings meet the threshold of 2, so the total is 9.99 + (0.25 + 1.25) * 0.9 = 9.99 + 1.35 = 11.34.