Code RoomTop k frequent tags
MediumPrep Room Coding #437

Top k frequent tags

CodingAlgorithms & data structuresEntry–Mid~15 min

A photo library shows its most-used tags first. Given the list of every tag application (repeats meaningful) and an integer k (k >= 1), return the k most frequently used distinct tags. Order the result by frequency, highest first; break frequency ties alphabetically (lexicographically ascending). If fewer than k distinct tags exist, return them all under the same ordering. For example, tags ["pop", "rock", "pop", "jazz", "rock", "pop"] with k = 2 give ["pop", "rock"], and ["t", "t", "s", "s", "r"] with k = 1 gives ["s"].

Implement
top_tags(tags: list[str], k: int) → list[str]
Examples
in[["pop","rock","pop","jazz","rock","pop"],2]out["pop","rock"]
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 15 min
InputExpectedGot
[["pop","rock","pop","jazz","rock","pop"],2]["pop","rock"]not run yetsample