KatInterview

Compute Per-Warehouse Reorder Alerts from Inventory Logs

SQL & DataAuto-graded · 15 test cases
HardAuto-gradedSQL & Datadata processinggroup-by aggregationdictionaries

Last 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:

FieldTypeDescription
warehouse_idstrIdentifier for the warehouse
item_idstrIdentifier for the item
quantityintCurrent quantity of that item in that warehouse (≥ 0)
reorder_thresholdintMinimum 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:

KeyTypeDescription
warehouse_idstrThe warehouse
item_idstrThe item
total_quantityintSum of all quantity values for this pair
reorder_thresholdintMax reorder_threshold seen for this pair
shortfallintreorder_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_id and item_id are non-empty strings containing only alphanumeric characters and underscores.
  • 0 ≤ quantity ≤ 10_000
  • 0 ≤ reorder_threshold ≤ 10_000
  • A pair with reorder_threshold == 0 will never trigger an alert (since total_quantity ≥ 0 is 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.