Katkat

Return top-3 content per category

MetaData Engineer · Onsite

Last updated

This question evaluates the ability to perform grouped top-k selection, deterministic tie-breaking and ordering, and to reason about algorithmic efficiency when manipulating structured datasets in the Data Manipulation (SQL/Python) domain for a Data Engineer role.

Given a collection of items with fields (content_id, category, rating), implement top_k_by_category(items, k= 3) that returns, for each category, the top 3 items by rating. Break ties by higher rating first, then by lexicographically smaller content_id, and keep results sorted by descending rating. Aim for O(n log k) per category using heaps or partial selection.