Your question is Top-K Frequent With PriorityQueue. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Qualys telemetry pipelines may need to identify the most frequent event codes in a batch for prioritization. Given an integer array of event codes, return the k codes with the highest frequencies using a frequency map and a bounded priority queue.
Implement top_k_frequent(nums, k), where nums is a list of integers and k is the number of results to return. Return a list containing exactly k distinct integers, ordered by decreasing frequency. If two codes have the same frequency, order the smaller code first.
Your solution should maintain a min-heap of at most k entries while processing the frequency map. The heap should remove the least useful candidate whenever it grows beyond k.
def top_k_frequent(nums, k):