Pizza Pricing
easy · decorator
Brief-reported interview attribution (unverified): Amazon
How to approach an LLD roundRequirements
- Functional: base pizzas with prices, toppings that add to the price and extend the description, an order that totals its pizzas and prints a bill.
- Non-functional: a new topping must not touch any existing class; every pizza (base or topped) must work wherever a pizza is expected.
- Out of scope: delivery, discounts, inventory.
Statement
Design a pizza ordering system. There are three base pizzas —
Margherita $5, Farmhouse $7, Pepperoni $8 — and four toppings — Extra Cheese
+$1, Mushroom +$1, Jalapeno +$1, Olives +$2. A topping decorates a pizza: it
wraps it, adds its price, and extends its description. Toppings stack (the same
topping twice counts twice). An Order holds several pizzas and totals them.
class Pizza:
def price(self):
# -> total price in dollars
def description(self):
# -> e.g. "Margherita + Mushroom + Olives"
# Base pizzas: Margherita ($5), Farmhouse ($7), Pepperoni ($8)
# Toppings (each wraps a Pizza): ExtraCheese (+$1), Mushroom (+$1),
# Jalapeno (+$1), Olives (+$2)
class Order:
def __init__(self):
def add_pizza(self, pizza):
def total(self):
# -> sum of the pizzas' prices
def bill(self):
# -> [(description, price), ...] one entry per pizza, in orderRules. Every pizza — base or topped — is a Pizza: price() and
description() work on any of them. A topping's description is the inner pizza's
description plus " + " plus the topping name. An empty order totals 0 and its
bill is [].
Money units: prices are whole dollars, not cents; a plain Margherita costs 5.
Tests use explicit inputs and injected clocks/callbacks. No network, real threads or application-server execution is involved.
Class design (SOLID)
Pizza (the abstraction) with Margherita/Farmhouse/Pepperoni bases and ExtraCheese/Mushroom/Jalapeno/Olives toppings that wrap a pizza — the Decorator pattern. OCP: a new topping is a new decorator. LSP: any topping is a Pizza. SRP: each class knows one price and one name. Order totals and bills.
Public demo
# Public demo: run after implementing the class API above.
print(Margherita().price())
print(Farmhouse().price())
print(Pepperoni().price())
print(Margherita().description())Reference solution and hidden tests
Locked. Open the interactive editor and attempt the problem; the reference, hidden test descriptions, pattern explanation and complexity are available only after an explicit reveal.
Concurrency
An order is built by one customer — no shared state beyond the order. In a server version, one order per request with no shared mutation.
Trade-offs & extensions
The decorator chain is O(toppings) per price call — trivial for a pizza. Extending: a Discount decorator (same pattern) and a Menu that builds pizzas from a name.
Starter code
class Pizza:
def price(self):
"""-> total price in dollars"""
pass
def description(self):
"""-> e.g. "Margherita + Mushroom + Olives" Base pizzas: Margherita ($5), Farmhouse ($7), Pepperoni ($8) Toppings (each wraps a Pizza): ExtraCheese (+$1), Mushroom (+$1), Jalapeno (+$1), Olives (+$2)"""
pass
class Margherita(Pizza):
def price(self):
"""-> total price in dollars"""
pass
def description(self):
"""-> e.g. "Margherita + Mushroom + Olives" Base pizzas: Margherita ($5), Farmhouse ($7), Pepperoni ($8) Toppings (each wraps a Pizza): ExtraCheese (+$1), Mushroom (+$1), Jalapeno (+$1), Olives (+$2)"""
pass
class Farmhouse(Pizza):
def price(self):
"""-> total price in dollars"""
pass
def description(self):
"""-> e.g. "Margherita + Mushroom + Olives" Base pizzas: Margherita ($5), Farmhouse ($7), Pepperoni ($8) Toppings (each wraps a Pizza): ExtraCheese (+$1), Mushroom (+$1), Jalapeno (+$1), Olives (+$2)"""
pass
class Pepperoni(Pizza):
def price(self):
"""-> total price in dollars"""
pass
def description(self):
"""-> e.g. "Margherita + Mushroom + Olives" Base pizzas: Margherita ($5), Farmhouse ($7), Pepperoni ($8) Toppings (each wraps a Pizza): ExtraCheese (+$1), Mushroom (+$1), Jalapeno (+$1), Olives (+$2)"""
pass
class _Topping(Pizza):
def __init__(self, pizza, name, price):
"""Initialize the state described in the problem."""
pass
def price(self):
"""-> total price in dollars"""
pass
def description(self):
"""-> e.g. "Margherita + Mushroom + Olives" Base pizzas: Margherita ($5), Farmhouse ($7), Pepperoni ($8) Toppings (each wraps a Pizza): ExtraCheese (+$1), Mushroom (+$1), Jalapeno (+$1), Olives (+$2)"""
pass
class ExtraCheese(_Topping):
def __init__(self, pizza):
"""Initialize the state described in the problem."""
pass
class Mushroom(_Topping):
def __init__(self, pizza):
"""Initialize the state described in the problem."""
pass
class Jalapeno(_Topping):
def __init__(self, pizza):
"""Initialize the state described in the problem."""
pass
class Olives(_Topping):
def __init__(self, pizza):
"""Initialize the state described in the problem."""
pass
class Order:
def __init__(self):
"""Initialize the state described in the problem."""
pass
def add_pizza(self, pizza):
"""Implement the return value and rules in the API contract."""
pass
def total(self):
"""-> sum of the pizzas' prices"""
pass
def bill(self):
"""-> [(description, price), ...] one entry per pizza, in order"""
pass
Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.
Return to the Python interpreter