KatInterview

Tally Orders per Customer by Status Column

Arrays & HashingAuto-graded · 13 test cases
MediumAuto-gradedArrays & Hashinghash-mapgroupingcounting

Last updated

Group order records by customer and count how many fall into each requested status, producing one sorted, zero-filled row per customer.

You are given a list of order records and a list of status names. Each order record is a dictionary that contains at least the keys "shopper_id" and "status", and may also contain other keys such as "order_id" or "order_date". Those other keys play no role in this task, so you never need to parse dates.

Build a summary table with one row per shopper. Each row reports how many of that shopper's orders have each of the requested statuses.

Output format

Return a list of dictionaries. Each dictionary has this shape:

{"shopper_id": <id>, <status_1>: <count>, <status_2>: <count>, ...}

Rules

  • Every shopper who appears at least once in orders gets exactly one row, even if none of their orders has a requested status. In that case all of their counts are 0.
  • Every status in statuses appears as a key in every row. A status with no matching orders for that shopper has the value 0.
  • Orders whose status is not in statuses are not counted and do not create new columns.
  • Status matching is exact and case-sensitive ("delivered" is not "DELIVERED").
  • Rows are sorted by shopper_id in ascending order using plain string comparison (so "10" comes before "9").
  • Within each row, the key order is "shopper_id" first, followed by the statuses in the order they appear in statuses.
  • If statuses contains duplicates, the repeated name yields only a single column.
  • If orders is empty, return an empty list. If statuses is empty, each row contains only "shopper_id".

Constraints

  • 0 <= len(orders) <= 10^5
  • 0 <= len(statuses) <= 100
  • Every order has string values for "shopper_id" and "status".
  • Status names and shopper ids are case-sensitive strings.

Examples

Example 1

Input: tally_orders_by_status([{'order_id': 'o1', 'shopper_id': 's2', 'order_date': '2024-01-01', 'status': 'DELIVERED'}, {'order_id': 'o2', 'shopper_id': 's1', 'order_date': '2024-01-02', 'status': 'CANCELED'}, {'order_id': 'o3', 'shopper_id': 's2', 'order_date': '2024-01-03', 'status': 'DELIVERED'}, {'order_id': 'o4', 'shopper_id': 's2', 'order_date': '2024-01-04', 'status': 'RETURNED'}], ['DELIVERED', 'CANCELED', 'RETURNED'])

Output: [{'shopper_id': 's1', 'DELIVERED': 0, 'CANCELED': 1, 'RETURNED': 0}, {'shopper_id': 's2', 'DELIVERED': 2, 'CANCELED': 0, 'RETURNED': 1}]

Hints

Hint 1

Think of this as a GROUP BY with conditional counting: one pass over the orders, keeping a counter table per shopper.

Hint 2

The first time you see a shopper, create their counter table with every requested status set to 0. That guarantees all columns exist and that shoppers with only unrequested statuses still appear.

Hint 3

Only increment a counter if the order's status is a key in that shopper's table; sort the shopper ids once at the end and build each output row with shopper_id first.