Top K Search Terms, One Vote per User
Search queries arrive as [user, term] pairs. A term's score is the number of distinct users who searched for it — a user searching the same term ten times counts once. Return the k highest-scoring terms, highest first, breaking ties alphabetically.
topSearchTerms([["u1", "apple"], ["u1", "apple"], ["u2", "apple"], ["u1", "pear"], ["u3", "pear"], ["u4", "pear"], ["u1", "kiwi"]], 2) -> ["pear", "apple"]
Hints
Hint 1
Keep a set of users per term; its size is the score. A set makes the once-per-user rule free.
Hint 2
Sort the terms by (score descending, term ascending) and take k. A heap of size k is the refinement when there are far more terms than k.