KatInterview

Build a POS cart with live receipt printing and combo deals

Data Structure DesignModel answer
MediumModel answerData Structure Designdata-structuressimulationgreedy

Last updated

Design a point-of-sale cart that prints an itemized receipt on every tap and then extends to apply non-overlapping combo discounts after each update.

You are implementing the core logic for a quick-service restaurant ordering screen. The framework gives you several class stubs and a callback to implement:

  • MenuItem(id, name, price) — a menu entry with a unique identifier, display name, and unit price.
  • Menu — exposes getItemById(id) for O(1) lookup.
  • Cart — supports add(itemId), remove(itemId), and tracks per-item quantities.
  • Screen — exposes render(text) to push a receipt string to the UI.
  • onTapItem(itemId) — invoked by the framework whenever the user taps an item button.

Part A — Live receipt. Every time onTapItem(itemId) fires, add one unit of the item to the Cart, update its quantity, and produce a receipt string that you pass to Screen.render. The receipt must:

  1. Show one line per distinct item in the format "name x qty @ unitPrice = lineTotal".
  2. Include a subtotal line and a grand total line at the bottom.

Repeated taps of the same item should aggregate into one line with an increasing quantity. Tapping different items should each produce their own line. Build whatever data structures and formatting helpers you need.

Part B — Combo deals. Now extend the system to support promotional combos. Each Combo is defined by:

  • k — the minimum number of qualifying items needed to trigger one application,
  • eligibleIds — the set of item IDs that can participate (you may also think of these as categories), and
  • a discount described by a type (flatOff for a fixed currency amount, or pctOff for a percentage) and a value.

After every item tap, determine some valid collection of non-overlapping combos that apply to the current cart, compute the combined discount, and re-render the receipt so it shows which combos fired and the final discounted total. You do not have to maximize savings or the number of combos — any valid application is acceptable.

In your answer, describe your data model for menu items, the cart, and combos. Provide pseudocode or working code for onTapItem. Explain the algorithm you use to select a valid set of non-overlapping combos (e.g., a single greedy pass with a note on when backtracking would be needed). Analyze time and space complexity. State your assumptions about unique IDs, integer quantities, and rounding rules for percentage discounts.

Hints

Hint 1

Store all monetary values as integer cents to avoid floating-point rounding errors.

Hint 2

Recompute combos from scratch on every tap rather than mutating cart state to remember discounts; this keeps add and remove symmetric and avoids stale-state bugs.

Hint 3

A single greedy pass over combos in priority order, consuming units from a mutable copy of quantities, is enough for a maximal valid set; full backtracking is only needed if the requirement changes to maximizing total discount.