Top category per customer

Write codeMedium25 min

Your task

You are given a list of order records, each a dict with keys 'customer', 'category', and 'amount' (a non-negative integer). Compute, for each customer, the single category on which they spent the most in total, with ties broken by the alphabetically smallest category name. Return a dict mapping customer to that top category. Model this query without SQL.

Examples

top_category_per_customer(orders: list[dict]) -> dict[str,str]
Example 1
Inputtop_category_per_customer([{"customer": "ann", "category": "books", "amount": 10}, {"customer": "ann", "category": "toys", "amount": 15}, {"customer": "bob", "category": "books", "amount": 5}])
Output{"ann": "toys", "bob": "books"}