

A

At Meta, you need to analyze a large log file from a backend service such as Messenger infrastructure and count how many times specific error codes appear. Implement an efficient Python function that processes the log line by line without loading the entire file into memory.
Given a list of log lines and a list of target error codes, return a dictionary mapping each target code to its total number of occurrences. A log line may contain zero or more error codes. Count only exact matches of codes that appear in the form ERROR_<ALPHANUMERIC_OR_UNDERSCORE>.
log_lines: a list of stringstarget_codes: a list of stringsExample 1
log_lines = ["INFO start", "WARN retry ERROR_TIMEOUT", "ERROR_TIMEOUT ERROR_DB_FAIL"], target_codes = ["ERROR_TIMEOUT", "ERROR_DB_FAIL"]{"ERROR_TIMEOUT": 2, "ERROR_DB_FAIL": 1}ERROR_TIMEOUT appears once in the second line and once in the third line; ERROR_DB_FAIL appears once.Example 2
log_lines = ["ERROR_500 ERROR_500 ok", "noop", "ERROR_AUTH"], target_codes = ["ERROR_500", "ERROR_AUTH", "ERROR_MISSING"]{"ERROR_500": 2, "ERROR_AUTH": 1, "ERROR_MISSING": 0}0.1 <= len(log_lines) <= 10^5<= 10^71 <= len(target_codes) <= 10^4log_lines = ["INFO start", "WARN retry ERROR_TIMEOUT", "ERROR_TIMEOUT ERROR_DB_FAIL"], target_codes = ["ERROR_TIMEOUT", "ERROR_DB_FAIL"]Output{"ERROR_TIMEOUT": 2, "ERROR_DB_FAIL": 1}WhyThe parser finds two valid occurrences of `ERROR_TIMEOUT` and one of `ERROR_DB_FAIL`.log_lines = ["ERROR_500 ERROR_500 ok", "noop", "ERROR_AUTH"], target_codes = ["ERROR_500", "ERROR_AUTH", "ERROR_MISSING"]Output{"ERROR_500": 2, "ERROR_AUTH": 1, "ERROR_MISSING": 0}WhyRepeated codes are counted multiple times, and unseen target codes remain in the output with value `0`.1 <= len(log_lines) <= 10^5Total characters across all log lines <= 10^71 <= len(target_codes) <= 10^4Each target code is uniqueValid error tokens match the pattern `ERROR_[A-Z0-9_]+`def count_error_codes(log_lines, target_codes):