Code RoomTop k items per category
HardPrep Room Coding #1006

Top k items per category

CodingML systemsSenior–Staff~35 min

Given `events`, a list of [category, item, score] triples (category/item are strings, score is an int), return the top `k` items per category by total score. For each category, sum scores per item, then keep the k items with the highest totals; break ties by item name ascending. Output a list of [category, item, total] sorted by category ascending, then total descending, then item ascending. If a category has fewer than k distinct items, return all of them.

Implement
top_k_per_category(events: list[list], k: int) → list[list]
Examples
in[[["food","pizza",5],["food","sushi",9],["food","pizza",5],["toys","lego",3]],1]out[["food","pizza",10],["toys","lego",3]]
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 35 min
InputExpectedGot
[[["food","pizza",5],["food","sushi",9],["food","pizza",5],["toys","lego",3]],1][["food","pizza",10],["toys","lego",3]]not run yetsample