Menu Price Resolution: Resolve Final Item Prices After Applying Cascading Discounts
Question Details
Problem
You are given a menu where each item has a base price. A list of discount rules is provided as (item_id, discount_type, value). Discount types are "percent" (e.g., 10 means 10% off) and "fixed" (e.g., 2.00 means $2 off). Multiple discounts can apply to one item -- apply them in order.
Return the final price for each item (minimum price is $0.00).
python
def resolve_prices(
menu: dict[str, float], # {item_id: base_price}
rules: list[tuple[str, str, float]] # (item_id, type, value)
) -> dict[str, float]:
pass
Example:
menu = {"burger": 10.00, "fries": 4.00, "soda": 2.50}
rules = [
("burger", "percent", 10), # 10% off -> $9.00
("burger", "fixed", 1.00), # then $1 off -> $8.00
("fries", "percent", 50), # 50% off -> $2.00
]
-> {"burger": 8.00, "fries": 2.00, "soda": 2.50}
Follow-ups
- Does the order of applying percent vs. fixed discounts matter mathematically? Give a proof or counterexample.
- How would you handle a "buy one get one free" rule that depends on another item being in the cart?
- What is the best data structure for storing and quickly querying discount rules when there are thousands of items?
- How would you audit the discount pipeline to log each step's intermediate price for debugging?
Full Details
Problem
You are given a menu where each item has a base price. A list of discount rules is provided as (item_id, discount_type, value). Discount types are "percent" (e.g., 10 means 10% off) and "fixed" (e.g., 2.00 means $2 off). Multiple discounts can apply to one item -- apply them in order.
Return the final price for each item (minimum price is $0.00).
python
def resolve_prices(
menu: dict[str, float], # {item_id: base_price}
rules: list[tuple[str, str, float]] # (item_id, type, value)
) -> dict[str, float]:
pass
Example:
menu = {"burger": 10.00, "fries": 4.00, "soda": 2.50}
rules = [
("burger", "percent", 10), # 10% off -> $9.00
("burger", "fixed", 1.00), # then $1 off -> $8.00
("fries", "percent", 50), # 50% off -> $2.00
]
-> {"burger": 8.00, "fries": 2.00, "soda": 2.50}
Follow-ups
- Does the order of applying percent vs. fixed discounts matter mathematically? Give a proof or counterexample.
- How would you handle a "buy one get one free" rule that depends on another item being in the cart?
- What is the best data structure for storing and quickly querying discount rules when there are thousands of items?
- How would you audit the discount pipeline to log each step's intermediate price for debugging?
About This Question
This is a reported interview question from a toast interview during the phone round.