Count Baskets Within a Budget While Product Prices Change
Company: DRW
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
A store sells M products, each with an integer price, and offers N fixed baskets, each a list of distinct products. A basket's total price is the sum of the current prices of its products. Prices change over time, and you need to answer how many baskets currently cost at most a given budget.
Process a sequence of operations and return the answer to every query, in order. Each operation is one of:
- `["update", prod_id, new_price]`: set the price of product `prod_id` to `new_price`. Every basket containing that product changes its total.
- `["query", x]`: count the baskets whose current total price is less than or equal to `x`.
### Function Signature
```python
def basket_queries(prices: dict[str, int], baskets: list[list[str]], operations: list[list[str | int]]) -> list[int]:
```
### Rules
- `prices` gives the initial price of every product; `baskets[b]` lists the product IDs in basket `b`.
- An update replaces the price; it does not add to it. Updating a product that is in no basket changes no total, and setting a price to its current value is allowed.
- A query compares each basket's total with `x` inclusively and does not change any state.
- Return a list with one integer per `"query"` operation, in the order the queries appear; return an empty list if there are no queries.
### Constraints
- `1 <= M <= 1000`, where `M = len(prices)`; product IDs are distinct non-empty strings of at most 10 uppercase letters or digits
- `1 <= N <= 1000`, where `N = len(baskets)`; each basket holds between 1 and M distinct product IDs, all present in `prices`
- The total number of product entries across all baskets is at most `10^4`
- `1 <= len(operations) <= 10^4`; every updated `prod_id` is present in `prices`
- `0 <= price, new_price <= 10^6` for every initial and updated price, so every basket total lies in `[0, 10^9]`
- `0 <= x <= 2 * 10^9`
### Examples
**Example 1**
```text
Input:
prices = {"GOOG": 10, "AAPL": 20, "SPCX": 30, "QQQM": 40}
baskets = [["GOOG", "AAPL"], ["AAPL", "SPCX", "QQQM"], ["QQQM", "GOOG"]]
operations = [["query", 50], ["update", "AAPL", 5], ["update", "QQQM", 45],
["query", 50], ["query", 80], ["query", 14]]
Output: [2, 1, 3, 0]
```
The totals start at 30, 90 and 50, so two baskets cost at most 50. After AAPL drops to 5 and QQQM rises to 45, the totals are 15, 80 and 55: one basket is within 50, all three are within 80, and none is within 14.
**Example 2**
```text
Input:
prices = {"A": 1, "B": 2, "C": 100}
baskets = [["A"], ["A", "B"]]
operations = [["update", "C", 1], ["query", 2], ["update", "A", 3], ["query", 4], ["query", 5]]
Output: [1, 1, 2]
```
Product C is in no basket, so its update changes nothing and the totals stay 1 and 3. After A becomes 3 the totals are 3 and 5.
Overview: Given product prices and a fixed set of baskets, process price updates and queries that ask how many baskets currently cost at most a budget. Tests choosing data structures that keep basket totals current when a shared product's price changes and that answer budget-count queries quickly.
Read the full DRW Software Engineer interview experience this question came from
A store sells `M` products, each with an integer price, and offers `N` fixed baskets. Each basket is a list of distinct product IDs, and a basket's total price is the sum of the **current** prices of its products. Prices change over time, and you need to answer how many baskets currently cost at most a given budget.
Implement `basket_queries(prices, baskets, operations)`, which processes `operations` in order and returns the answer to every query.
- `prices` maps every product ID to its initial price.
- `baskets[b]` lists the product IDs in basket `b`.
- Each operation is one of:
- `["update", prod_id, new_price]`: set the price of product `prod_id` to `new_price`. Every basket containing that product changes its total.
- `["query", x]`: count the baskets whose current total price is less than or equal to `x`.
### Rules
- An update replaces the price; it does not add to it. Updating a product that is in no basket changes no total, and setting a price to its current value is allowed.
- A query compares each basket's total with `x` inclusively (`total <= x`) and does not change any state.
- Return a list with one integer per `"query"` operation, in the order the queries appear; return an empty list if there are no queries.
### Constraints
- `1 <= M <= 1000`, where `M = len(prices)`; product IDs are distinct non-empty strings of at most 10 uppercase letters or digits
- `1 <= N <= 1000`, where `N = len(baskets)`; each basket holds between 1 and `M` distinct product IDs, all present in `prices`
- The total number of product entries across all baskets is at most `10^4`
- `1 <= len(operations) <= 10^4`; every updated `prod_id` is present in `prices`
- `0 <= price, new_price <= 10^6` for every initial and updated price, so every basket total lies in `[0, 10^9]`
- `0 <= x <= 2 * 10^9`
No value exceeds `2^31 - 1` (the largest, `x = 2 * 10^9`, is below it), but the Java starter types prices as `Long` and the C++ starter uses `long long` for prices.
### Language notes
- Java: `prices` is a `java.util.Map<String, Long>` and each operation is a `java.util.List<Object>` whose first entry is a `String`; read its numeric entries through `java.lang.Number`, for example `((Number) op.get(1)).longValue()`.
- C++: each operation is a `std::vector<std::string>` and its numeric entries arrive as decimal text, so `["update", "A", 7]` arrives as `{"update", "A", "7"}` and `["query", 5]` as `{"query", "5"}`; parse the numbers with `std::stoll`.
### Example 1
```text
Input:
prices = {"GOOG": 10, "AAPL": 20, "SPCX": 30, "QQQM": 40}
baskets = [["GOOG", "AAPL"], ["AAPL", "SPCX", "QQQM"], ["QQQM", "GOOG"]]
operations = [["query", 50], ["update", "AAPL", 5], ["update", "QQQM", 45],
["query", 50], ["query", 80], ["query", 14]]
Output: [2, 1, 3, 0]
```
The totals start at 30, 90 and 50, so two baskets cost at most 50. After AAPL drops to 5 and QQQM rises to 45, the totals are 15, 80 and 55: one basket is within 50, all three are within 80, and none is within 14.
### Example 2
```text
Input:
prices = {"A": 1, "B": 2, "C": 100}
baskets = [["A"], ["A", "B"]]
operations = [["update", "C", 1], ["query", 2], ["update", "A", 3], ["query", 4], ["query", 5]]
Output: [1, 1, 2]
```
Product C is in no basket, so its update changes nothing and the totals stay 1 and 3. After A becomes 3 the totals are 3 and 5.
Constraints
- 1 <= M <= 1000, where M = len(prices); product IDs are distinct non-empty strings of at most 10 uppercase letters or digits
- 1 <= N <= 1000, where N = len(baskets); each basket holds between 1 and M distinct product IDs, all present in prices
- The total number of product entries across all baskets is at most 10^4
- 1 <= len(operations) <= 10^4; every updated prod_id is present in prices
- 0 <= price, new_price <= 10^6 for every initial and updated price, so every basket total lies in [0, 10^9]
- 0 <= x <= 2 * 10^9
- Each operation is either ["update", prod_id, new_price] or ["query", x]
Examples
Input: ({'A': 5}, [['A']], [['query', 5]])
Expected Output: [1]
Explanation: Minimum valid input (M=1, N=1, one single-product basket, a lone query); the total 5 equals x and the comparison is inclusive.
Input: ({'A': 5}, [['A']], [['query', 4]])
Expected Output: [0]
Explanation: Minimum input with x one below the only total, so nothing is counted; catches a strict-versus-inclusive off-by-one.
Hints
- An update replaces a price rather than adding to it; think about how far each affected basket's total actually moves.
- Only baskets that contain the updated product change, so there is no need to recompute every basket from scratch after each update.
- Queries compare inclusively (total <= x), never change state, and must be answered in the order they appear.