Group orders by customer and status

Python data engineering interview problem. Difficulty: intermediate. Pattern: Dictionaries. About 15 minutes. Part of the Pro drill bank.

Ops wants open vs shipped orders grouped by customer so a dashboard can show each customer's workload by status.

A support dashboard wants to see, for each customer, which orders are in each status. Group the order ids by customer and then by status. Return a dictionary like {customer: {status: [order ids]}}. The order ids in each list keep the order of the input. Write group_orders(orders). Customer U1 has paid orders A and B, and customer U2 has a pending order C. The result is {'U1': {'paid': ['A', 'B']}, 'U2': {'pending': ['C']}}. The starter file already has the function name and its print lines. Keep them and fill in the body.

Requirements

  • Group by customer_id first, then by status.
  • Collect the order_id values in a list.
  • Keep the input order inside each list.
  • Return {} for an empty list.

Constraints

  • Each order is a dictionary with customer_id, status and order_id.
  • Only groups that have orders appear in the result.

Examples

Input: group_orders([{'customer_id': 'U1', 'status': 'paid', 'order_id': 'A'}, {'customer_id': 'U1', 'status': 'paid', 'order_id': 'B'}, {'customer_id': 'U2', 'status': 'pending', 'order_id': 'C'}]) Output: {'U1': {'paid': ['A', 'B']}, 'U2': {'pending': ['C']}} This input follows the stated rules and produces this output.

Topics: lakebench, python, grouping.

More Python interview questions · All interview problems · Learn data engineering

intermediate

Group orders by customer and status

Interview-style drill: Group a flat list of orders into a nested dictionary keyed by customer, then status.

Group orders by customer and status.