Code RoomTop k frequent tokens
MediumPrep Room Coding #1011

Top k frequent tokens

CodingDistributed systemsAlgorithms & data structuresMid–Senior~30 min

You consume a stream of string tokens (given as a list) and must report the k most frequent. Return the top-k tokens ordered by descending count; break ties by token ascending (lexicographic). If fewer than k distinct tokens exist, return all of them. k is a positive integer. Return just the list of tokens (not counts).

Implement
top_k_frequent(tokens: list[str], k: int) → list[str]
Examples
in[["a","b","a","c","b","a"],2]out["a","b"]
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 30 min
InputExpectedGot
[["a","b","a","c","b","a"],2]["a","b"]not run yetsample