I got the OA and worked through it in about an hour and a half. Nothing was especially hard, but watch out for rounding. I've organized everything and posted it all below.
Part 1 — Initial
Problem
You're building a discount system for Trader Yojoe's, a growing grocery chain. Their business is thriving, but they're starting a new pilot program where shoppers can bring coupons and apply them to items at checkout. Their current point-of-sale system doesn't support discounts, so you need to build this functionality from scratch.
Given a list of items and a list of discount coupons, create an algorithm that correctly applies the discounts during purchase and calculates the final total.
Requirements
- Each item has a name and price. The price is in integer cents.
- Each discount coupon has an item name it applies to and a percent off.
- Apply matching discounts to items and return a dictionary with three keys:
subtotal: the sum of all original item pricestotal_discount: the sum of all discount amountsfinal_total: the subtotal minus the total discount
Assumptions
- There will be at most one discount coupon per item name.
- Discounts that don't match any item in the cart are ignored.
Notes
- Round the final discount amount for each item to the nearest cent using
round(). Do not round intermediate calculations. - Ensure all returned values are integers. Apply the Python
int()operator to each of your final answers if needed.
Starter Code
#!/bin/python3
# Sample data
items = [
{"name": "Organic Bananas", "price": 399},
...
]
discounts = [
{"name": "Organic Bananas", "percent_off": 10},
...
]
def apply_discounts_and_calculate_total(items, discounts):
"""
Apply discounts to items and calculate the total cost after discounts.
Returns:
Dict with 'subtotal', 'total_discount', 'final_total'
Example Output:
{
'subtotal': 0,
'total_discount': 0,
'final_total': 0
}
"""
subtotal = 0
total_discount = 0
Part 2 — Extension
Problem
Great news! Your initial discount system was a success. Shoppers love the coupon program, and Trader Yojoe's wants to expand it with more discount options to give customers better deals and increase engagement.
The marketing team wants to support two new types of discounts. Extend your system to handle these new discount types while maintaining backward compatibility with the existing percentage-off coupons.
Requirements
- Category Discounts: Apply discounts to entire product categories, e.g. "20% off all Dairy products".
- Discount Stacking: Items can now have multiple discounts applied:
- Item-specific discounts and category discounts stack multiplicatively.
- Example: 10% item discount + 20% category discount gives: price × 0.90 × 0.80 = 72% of original, i.e. 28% off.
- Apply item-specific discounts before category discounts.
- Maximum total discount per item cannot exceed 80%.
- If applying a discount would push total savings over 80%, that discount will be partially applied to reach exactly 80%.
Assumptions
- There will be at most one item-specific discount per item name.
- There will be at most one category discount per category.
- Discounts that don't match any item are ignored.
Notes
- Round the final discount amount for each item to the nearest cent using
round(). Do not round intermediate calculations. - Ensure all returned values are integers. Apply the Python
int()operator to each of your final answers if needed. - Feel free to copy/paste your code from Part 1 or start from scratch.
Starter Code
#!/bin/python3
# Sample data - now with categories!
items = [
{"name": "Organic Bananas", "price": 399, "category": "Produce"},
{"name": "Almond Milk", "price": 450, "category": "Dairy"},
...
]
# Now supporting multiple discount types!
discounts = [
{"type": "item", "name": "Organic Bananas", "percent_off": 10},
{"type": "category", "name": "Produce", "percent_off": 10},
...
]
def apply_discounts_and_calculate_total(items, discounts):
"""
Apply discounts to items and calculate final totals.
Returns:
Dict with 'subtotal', 'total_discount', 'final_total'
"""
# TODO: Extend your implementation to handle new discount types
item_discounts = {}
category_discounts = {}
for discount in discounts:
Part 3 — Optimization
Problem
Trader Yojoe's is launching a gamified loyalty program! Instead of applying all available discounts automatically, customers now get "Discount Points" to spend strategically. Each customer receives 20 points per transaction, and different discounts cost different amounts of points to activate.
Customers want to maximize their savings, but they need to be strategic about which discounts to activate. Some expensive discounts might not be worth their point cost, while cheaper discounts might provide better value.
Given a shopping cart and available discounts, determine which discounts should be activated to maximize total savings while staying within the 20-point budget.
Requirements
- All discount mechanics from Part 2 still apply:
- multiplicative stacking
- 80% cap
- Item-specific percentage discounts cost 2 points each.
- Category-wide discounts cost 5 points each.
- Total points used cannot exceed 20.
Assumptions
- There will be at most one item discount available per item name.
- There will be at most one category discount available per category.
- You may activate any combination of available discounts, subject to the point budget of 20.
Notes
- This is an optimization problem — focus on getting a working solution first, then improve it.
- The auto-grader will first validate your output format, then score based on total savings achieved.
- You will have unlimited submissions for Part 3.
- Feel free to copy/paste your code from Part 2 or start from scratch.
Example
items = [
{"name": "Milk", "price": 500, "category": "Dairy"}
]
discounts = [
{"type": "item", "name": "Milk", "percent_off": 20}
]
optimize_discounts(items, discounts) returns:
{
"selected_discounts": [
{
"type": "item",
"name": "Milk",
"percent_off": 20
}
],
"total_points_used": 2
}
apply_discounts_and_calculate_total(items, selected_discounts) returns:
{
"subtotal": 500,
"total_discount": 100,
"final_total": 400
}
Starter Code
#!/bin/python3
# Sample data
items = [
{"name": "Organic Bananas", "price": 399, "category": "Produce"},
...
]
discounts = [
{"type": "item", "name": "Organic Bananas", "percent_off": 15},
{"type": "category", "name": "Produce", "percent_off": 15},
...
]
from itertools import combinations
POINT_BUDGET = 20
ITEM_DISCOUNT_PRICE = 2
CATEGORY_DISCOUNT_PRICE = 5
def get_discount_cost(discount):
if discount["type"] == "item":
return ITEM_DISCOUNT_PRICE
return CATEGORY_DISCOUNT_PRICE
def optimize_discounts(items, discounts):
"""
Determine which discounts to activate to maximize savings.
"""
Discussion
Loading comments…