Your question is Top-K Frequent Errors From Logs. 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.
SAP Cloud ALM receives a stream of error messages from monitored applications. Given an iterable of incoming error-message strings, return the k most frequent messages efficiently.
The result must be ordered by decreasing frequency. If two messages have the same frequency, order them by reverse lexicographic order, with the lexicographically larger message first.
Implement top_k_errors(logs, k).
logs is an iterable of strings. It may be a list, tuple, or generator.k is a positive integer.[message, frequency] pairs.k pairs if fewer than k distinct messages occur.logs.Use a frequency map to aggregate counts and a bounded min-heap to avoid sorting every distinct message when k is small.
def top_k_errors(logs, k):