Compute Per-Warehouse Reorder Alerts from Inventory Logs
SQL & DataAuto-graded · 15 test casesLast updated
Tests group-by aggregation over a list-of-dict records, computing per-warehouse stock summaries and flagging items that need reordering.
Inventory Reorder Alerts
You are given a list of inventory log records. Each record is a dictionary with the following fields:
| Field | Type | Description |
|---|---|---|
warehouse_id | str | Identifier for the warehouse |
item_id | str | Identifier for the item |
quantity | int | Current quantity of that item in that warehouse (≥ 0) |
reorder_threshold | int | Minimum quantity before a reorder is needed (≥ 0) |
There may be multiple records for the same (warehouse_id, item_id) pair (e.g., from different log timestamps). You must sum the quantity values across all records for a given (warehouse_id, item_id) pair. The reorder_threshold for a pair is the maximum value seen across all records for that pair.
Your task is to return a list of reorder alert dictionaries — one per (warehouse_id, item_id) pair whose total quantity is strictly less than its reorder threshold — sorted first by warehouse_id ascending (lexicographic), then by item_id ascending (lexicographic).
Each alert dictionary must have exactly these keys:
| Key | Type | Description |
|---|---|---|
warehouse_id | str | The warehouse |
item_id | str | The item |
total_quantity | int | Sum of all quantity values for this pair |
reorder_threshold | int | Max reorder_threshold seen for this pair |
shortfall | int | reorder_threshold - total_quantity (always ≥ 1 for alerts) |
If no pair triggers an alert, return an empty list.
Constraints
0 ≤ len(records) ≤ 2000- Each record has exactly the four fields described above.
warehouse_idanditem_idare non-empty strings containing only alphanumeric characters and underscores.0 ≤ quantity ≤ 10_0000 ≤ reorder_threshold ≤ 10_000- A pair with
reorder_threshold == 0will never trigger an alert (sincetotal_quantity ≥ 0is never strictly less than 0).
Examples
Example 1
Input: compute_reorder_alerts([{'warehouse_id': 'WH_A', 'item_id': 'bolt', 'quantity': 50, 'reorder_threshold': 100}, {'warehouse_id': 'WH_A', 'item_id': 'bolt', 'quantity': 30, 'reorder_threshold': 90}, {'warehouse_id': 'WH_A', 'item_id': 'nut', 'quantity': 200, 'reorder_threshold': 150}, {'warehouse_id': 'WH_B', 'item_id': 'bolt', 'quantity': 5, 'reorder_threshold': 80}])
Output: [{'warehouse_id': 'WH_A', 'item_id': 'bolt', 'total_quantity': 80, 'reorder_threshold': 100, 'shortfall': 20}, {'warehouse_id': 'WH_B', 'item_id': 'bolt', 'total_quantity': 5, 'reorder_threshold': 80, 'shortfall': 75}]
Example 2
Input: compute_reorder_alerts([{'warehouse_id': 'WH_X', 'item_id': 'widget', 'quantity': 0, 'reorder_threshold': 0}, {'warehouse_id': 'WH_X', 'item_id': 'gadget', 'quantity': 3, 'reorder_threshold': 3}])
Output: []
Example 3
Input: compute_reorder_alerts([])
Output: []
Hints
Hint 1
First group all records by (warehouse_id, item_id) using a dictionary of accumulators — track running sum of quantity and running max of reorder_threshold.
Hint 2
After aggregating, filter to only pairs where total_quantity < reorder_threshold, then compute shortfall = reorder_threshold - total_quantity.
Hint 3
Sort the resulting alerts by (warehouse_id, item_id) lexicographically before returning.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem is a group-by aggregation exercise — the Python analog of a SQL query like:
SELECT warehouse_id, item_id,
SUM(quantity) AS total_quantity,
MAX(reorder_threshold) AS reorder_threshold,
MAX(reorder_threshold) - SUM(quantity) AS shortfall
FROM inventory_logs
GROUP BY warehouse_id, item_id
HAVING SUM(quantity) < MAX(reorder_threshold)
ORDER BY warehouse_id, item_id;
Steps
-
Aggregate — iterate over every record and accumulate into a dictionary keyed by
(warehouse_id, item_id). For each key maintain:- a running sum of
quantity - a running maximum of
reorder_threshold
- a running sum of
-
Filter — after processing all records, keep only those keys where
total_quantity < reorder_threshold. -
Build output dicts — for each surviving key, construct the alert dict with
shortfall = reorder_threshold - total_quantity. -
Sort — sort the alert list by
(warehouse_id, item_id)lexicographically.
Why it is correct
- Summing
quantityacross multiple records for the same pair is the correct aggregation because multiple log rows for a pair represent independent stock counts that should be combined. - Taking the max
reorder_thresholdis the conservative (safe) choice: if any log entry sets a high threshold, we honour it. - The strict inequality
<means a pair at exactly the threshold is not flagged, matching the problem statement.
Complexity
| Value | |
|---|---|
| Time | O(N log N) — N = number of records; dominated by sorting the output |
| Space | O(K) — K = number of distinct (warehouse_id, item_id) pairs ≤ N |
Reference Solution
def compute_reorder_alerts(records: list) -> list:
from collections import defaultdict
# accumulators: key -> [sum_quantity, max_threshold]
agg = {}
for rec in records:
key = (rec["warehouse_id"], rec["item_id"])
if key not in agg:
agg[key] = [0, 0]
agg[key][0] += rec["quantity"]
agg[key][1] = max(agg[key][1], rec["reorder_threshold"])
alerts = []
for (warehouse_id, item_id), (total_qty, threshold) in agg.items():
if total_qty < threshold:
alerts.append({
"warehouse_id": warehouse_id,
"item_id": item_id,
"total_quantity": total_qty,
"reorder_threshold": threshold,
"shortfall": threshold - total_qty,
})
alerts.sort(key=lambda x: (x["warehouse_id"], x["item_id"]))
return alerts