Pizza Pricing
easy · decorator
Brief-reported interview attribution (unverified): Amazon
How to approach an LLD roundDesign 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 order
Rules. 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.
Public demo
# Public demo: run after implementing the class API above.
print(Margherita().price())
print(Farmhouse().price())
print(Pepperoni().price())
print(Margherita().description())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 compiler