Implement receipt updates with combo discounts
Problem Description
Implement a simple food-ordering workflow that processes user selections, updates a shopping cart, applies discounts, and generates a receipt.
You are provided with the following classes:
Item: Represents a menu item.Menu: Manages availableItems.Display: Handles printing the receipt.
And a function:
onUserUpdateSelected(itemId): This function is called each time a user adds an item to their cart.
Part 1: Basic Cart and Receipt
Each time onUserUpdateSelected(itemId) is called:
- Update the user's shopping cart with the newly selected item.
- Generate and print a receipt via the
Displayclass. - The receipt must list all items in the cart, the subtotal, any applied discounts, and the final total.
- Implement appropriate
toString()methods for relevant classes to support receipt printing.
Part 2: Combo Discounts
Introduce a Combo class. Each combo is defined by a set of k specific items that, when present in the cart, yield a discountAmount.
After each item addition (i.e., within onUserUpdateSelected):
- Identify any valid set of combos that can be formed from the current items in the cart. You do not need to maximize the total discount or the number of combos applied.
- Apply the resulting discount(s) from the identified combos.
- Print an updated receipt reflecting these discounts.
Deliverables
- Describe your chosen data structures for tracking item counts in the cart and managing combo definitions.
- Provide pseudocode or actual code for the
onUserUpdateSelectedfunction. - Explain your method for finding any valid combo set (e.g., backtracking, greedy, or another approach that guarantees a valid set if one exists).
- Analyze the time and space complexity of your solution.
AI interview prep
Core dilemma
Efficiently managing mutable cart state and applying potentially complex, overlapping discount rules, while adhering to a 'find any valid set' constraint rather than full optimization.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement receipt updates with combo discounts
Problem Description
Implement a simple food-ordering workflow that processes user selections, updates a shopping cart, applies discounts, and generates a receipt.
You are provided with the following classes:
Item: Represents a menu item.Menu: Manages availableItems.Display: Handles printing the receipt.
And a function:
onUserUpdateSelected(itemId): This function is called each time a user adds an item to their cart.
Part 1: Basic Cart and Receipt
Each time onUserUpdateSelected(itemId) is called:
- Update the user's shopping cart with the newly selected item.
- Generate and print a receipt via the
Displayclass. - The receipt must list all items in the cart, the subtotal, any applied discounts, and the final total.
- Implement appropriate
toString()methods for relevant classes to support receipt printing.
Part 2: Combo Discounts
Introduce a Combo class. Each combo is defined by a set of k specific items that, when present in the cart, yield a discountAmount.
After each item addition (i.e., within onUserUpdateSelected):
- Identify any valid set of combos that can be formed from the current items in the cart. You do not need to maximize the total discount or the number of combos applied.
- Apply the resulting discount(s) from the identified combos.
- Print an updated receipt reflecting these discounts.
Deliverables
- Describe your chosen data structures for tracking item counts in the cart and managing combo definitions.
- Provide pseudocode or actual code for the
onUserUpdateSelectedfunction. - Explain your method for finding any valid combo set (e.g., backtracking, greedy, or another approach that guarantees a valid set if one exists).
- Analyze the time and space complexity of your solution.