Code RoomTop category per customer
MediumPrep Room Coding #1244

Top category per customer

CodingAlgorithms & data structuresMid–Senior~25 min

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.

Implement
top_category_per_customer(orders: list[dict]) → dict[str,str]
Examples
in[[{"amount":10,"category":"books","customer":"ann"},{"amount":15,"category":"toys","customer":"ann"},{"amount":5,"category":"books","customer":"bob"}]]out{"ann":"toys","bob":"books"}
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 25 min
InputExpectedGot
[[{"amount":10,"category":"books","customer":"ann"},{"amount":15,"category":"toys","customer":"ann"},{"amount":5,"category":"books","customer":"bob"}]]{"ann":"toys","bob":"books"}not run yetsample