Your question is Top 10 Frequent Error Codes. 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.
Affirm services emit pipe-delimited log records. Given a stream of valid log strings, identify the 10 most frequent error codes without storing every log record.
Each log has the format timestamp|level|error_code|message. The error code is the third field, at index 2. Count every record, including repeated codes. If fewer than 10 distinct error codes appear, return all of them.
Return a list of objects ordered by descending frequency. When two codes have the same frequency, order them lexicographically by error code. Each object must contain code and count.
Implement top_error_codes(logs), where logs is an iterable of strings. Return a list of dictionaries with the shape {"code": string, "count": integer}.
The input iterable may be consumed only once. Do not sort all distinct codes to select the result unless using the alternative approach described in your explanation.
def top_error_codes(logs):