Calculate a cart total with buy-X-get-Y-free rules and threshold percentage discounts
Company: Shopify
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
Given a list of item `prices` in a shopping cart and a list of `discounts` rules, calculate the final total price after applying all applicable tiered discounts.
Each discount rule is one of two types:
- Type 1 (buy X, get Y free) is written `[1, X, Y]`. Sort all items by price ascending. For every group of `X + Y` consecutive items, the cheapest `Y` items in each group are free.
- Type 2 (percentage off above a threshold) is written `[2, threshold, percentage]`. If the subtotal exceeds `threshold`, apply the `percentage` discount to the amount above the threshold.
First, apply all Type 1 rules to determine which items are free. Then compute the subtotal from the remaining (non-free) items. Finally, apply the Type 2 rules sequentially in the order they appear. Return the final price rounded to 2 decimal places.
### Function Signature
```python
def calculate_final_price(prices: list[float], discounts: list[list[float]]) -> float:
```
### Rules
- Each rule is a list of three numbers whose first entry is `1` or `2`. Type 1 and Type 2 rules may be mixed in any order in `discounts`.
- For a Type 1 rule, split the ascending cart into consecutive groups of `X + Y` items starting from the cheapest item. In every complete group, the first `Y` items (the cheapest of that group) are free. Items at the end that do not fill a complete group pay full price.
- Every Type 1 rule is evaluated on the same sorted cart. An item is free when at least one Type 1 rule makes it free, and a free item is never charged.
- Type 2 rules run in their input order, each on the current running total. When `total > threshold`, the new total is `total - (total - threshold) * percentage / 100`. Otherwise the total is unchanged.
- Treat every input number as the exact decimal it is written as, do not round intermediate values, and round only the final total to the nearest cent, with exactly half a cent rounding up. Return it as a float, for example `154.0` or `84.09`.
### Constraints
- `1 <= len(prices) <= 1000`
- `0.01 <= prices[i] <= 10000.00`
- `0 <= len(discounts) <= 10`
- Each discount is either `[1, X, Y]` with `1 <= Y < X + Y <= len(prices)`, or `[2, threshold, percentage]` with `0 <= threshold <= 10^6` and `0 < percentage <= 100`
### Examples
```text
Input: prices = [10, 20, 30, 40, 50, 60], discounts = [[1, 2, 1], [2, 100, 10]]
Output: 154.0
```
Sorted, the cart splits into the groups [10, 20, 30] and [40, 50, 60], and the cheapest item of each (10 and 40) is free, leaving 20 + 30 + 50 + 60 = 160. The Type 2 rule takes 10% off the 60 above 100, a discount of 6, so the total is 154.
```text
Input: prices = [5, 5, 5], discounts = [[1, 2, 1]]
Output: 10.0
```
The three items form one group of 3 and one 5 is free, so the total is 5 + 5 = 10.
```text
Input: prices = [40, 10, 30, 20.1], discounts = [[1, 2, 1], [2, 50, 15]]
Output: 84.09
```
Sorted, the cart is [10, 20.1, 30, 40]. Only [10, 20.1, 30] is a complete group, so 10 is free and 40 pays full price, giving 90.1. The Type 2 rule takes 15% of the 40.1 above 50, which is 6.015, so the exact total is 84.085 and the half cent rounds up to 84.09.
Overview: Compute a shopping cart total after buy-X-get-Y-free rules on the price-sorted cart and percentage discounts on the amount above spending thresholds, applied in order. Tests careful simulation of grouped discounts, rule ordering, incomplete groups and exact rounding of money to the cent.