Design a Burger Shop Order System Using the Builder Pattern
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a Burger Shop that lets customers construct customized burgers using the **Builder pattern**. Each burger has a required `bun` type and optional components: a `patty`, `cheese`, and a list of `toppings`.
The shop maintains a fixed price list for each component. When an order is placed, the shop totals the price of all components in the burger, adds it to the shop's revenue, and returns a textual receipt describing the burger.
Construction is step by step: a builder is started with `startOrder`, components are added one at a time, and the burger is finalized and billed with `placeOrder`. Toppings are stored in the order they were added; duplicates are allowed.
Implement the following methods:
- `startOrder(bun: str)`: Begin building a new burger with the given bun type. Resets any in-progress builder.
- `addPatty(patty: str)`: Add a patty to the current burger. No-op if no order is in progress.
- `addCheese(cheese: str)`: Add cheese to the current burger. No-op if no order is in progress.
- `addTopping(topping: str)`: Append a topping to the current burger. No-op if no order is in progress.
- `placeOrder() -> str`: Finalize the current burger, charge the customer, and return a receipt string formatted as `"bun|patty|cheese|topping1,topping2|$price"`. Returns an empty string if no order is in progress.
- `getRevenue() -> int`: Return the total revenue accumulated from all placed orders.
Example 1:
```text
Input:
["BurgerShop", "startOrder", "addPatty", "addCheese", "addTopping", "addTopping", "placeOrder", "getRevenue"]
[[], ["sesame"], ["beef"], ["cheddar"], ["lettuce"], ["tomato"], [], []]
Output: [null, null, null, null, null, null, "sesame|beef|cheddar|lettuce,tomato|$12", 12]
Explanation:
shop = BurgerShop() # revenue = 0
shop.startOrder("sesame") # builder: bun=sesame
shop.addPatty("beef") # builder: +beef patty
shop.addCheese("cheddar") # builder: +cheddar
shop.addTopping("lettuce") # toppings: [lettuce]
shop.addTopping("tomato") # toppings: [lettuce, tomato]
shop.placeOrder() # -> "sesame|beef|cheddar|lettuce,tomato|$12" (3+5+2+1+1)
shop.getRevenue() # -> 12
```
Example 2:
```text
Input:
["BurgerShop", "addPatty", "startOrder", "placeOrder", "startOrder", "addCheese", "placeOrder", "getRevenue"]
[[], ["beef"], ["brioche"], [], ["plain"], ["swiss"], [], []]
Output: [null, null, null, "brioche||||$3", null, null, "plain||swiss||$5", 8]
Explanation:
shop = BurgerShop() # revenue = 0
shop.addPatty("beef") # no-op (no order in progress)
shop.startOrder("brioche") # builder: bun=brioche
shop.placeOrder() # -> "brioche||||$3" (bun only)
shop.startOrder("plain") # builder: bun=plain
shop.addCheese("swiss") # builder: +swiss
shop.placeOrder() # -> "plain||swiss||$5" (3+2)
shop.getRevenue() # -> 8
```
```hint Separate the jobs
Ask which object should know how a burger is put together and which should know what each part costs. Those two things change for different reasons.
```
```hint State between orders
Decide exactly what "an order is in progress" means in your state, then check that state right after `placeOrder` and after a second `startOrder`.
```
### Constraints & Assumptions
- `1 <= len(bun), len(patty), len(cheese), len(topping) <= 20`
- Prices for each component are fixed: bun \$3, patty \$5, cheese \$2, each topping \$1.
- At most `10^4` total operations will be performed.
- At most one order is in progress at any time; calling `startOrder` resets any in-progress builder.
- Component-adding methods called with no order in progress are silently ignored.
### Clarifying Questions to Ask
- If `addPatty` or `addCheese` is called twice for the same burger, should the second call replace the first, or add a second patty or cheese and charge for it?
- When `startOrder` discards an unfinished burger, should anything be charged for it?
- Can prices change while the shop is running, or vary by component name (for example, a pricier patty)?
- Does the shop need to keep a history of placed burgers, or only the revenue total?
### What a Strong Answer Covers
- A separate builder that takes the required bun up front, collects the optional parts one step at a time, and produces a finished burger.
- A clear split between assembly (the builder) and pricing and revenue (the shop), with revenue updated only when an order is placed.
- Correct in-progress state: component calls ignored with no order, a reset on `startOrder`, and a cleared builder after `placeOrder`.
- The exact receipt format, including empty fields for missing components and toppings joined in insertion order with duplicates kept.
- The cost of each operation, with `placeOrder` linear in the number of toppings.
### Follow-up Questions
- How would you change the design so several customers can build burgers at the same time?
- Suppose a wagyu patty costs more than a beef patty. Where would per-item prices live, and what code would change?
- How would you add preset menu burgers with fixed components without repeating the assembly steps for each one?
Overview: Design a burger ordering system where customers assemble a burger step by step with the Builder pattern, and the shop prices each order, tracks revenue and returns a fixed-format receipt. It tests separating assembly from pricing, state handling between orders and exact output formatting.