Skip to content
DeepForgeby svx
All problems
MediumNLPnlp-335

Top K Frequent Tokens

Problem

Return the k most frequent tokens sorted by count descending, with ties broken lexicographically ascending.

Given the tokens and k, return the list of up to k tokens.

Hint

Negate the count for descending order and sort the vocabulary keys alphabetically for ties.

Test cases

case 1
in: [["b","a","b","c","a","a"],2]
expected: ["a","b"]
case 2
in: [["x","y","z"],5]
expected: ["x","y","z"]
case 3
in: [["q"],0]
expected: []
Starter codePython
def top_k_frequent_tokens(tokens, k):
    # Your code here
    pass

3 test cases · run your code against them in the editor