KatInterview

Identify the Most Common Function Call in Application Logs

Arrays & HashingModel answer
EasyModel answerArrays & Hashinghash-mapfrequency-countingiteration

Last updated

Determine which function name occurs most often in a list of log entries and report its occurrence count.

You're analyzing application logs where each entry records a function name that was invoked. Your task is to identify which function appears most frequently.

Write a function that accepts a list of function names (strings) and returns a tuple containing the most common function name and how many times it occurred.

Example:

Input: ["login", "search", "login", "checkout", "search", "login"]
Output: ("login", 3)

Requirements:

  • If the input is empty, return None
  • If multiple functions tie for the highest frequency, return the one that appeared earliest in the log sequence
  • Treat function names as case-sensitive exact matches

Follow-up questions you should be prepared to discuss:

  • How would you extend this to return the top k most frequent functions?
  • How would you handle logs arriving as a continuous stream?
  • What if the log data is distributed across multiple machines?

Hints

Hint 1

Use a hash map to track how many times each function name appears as you iterate through the logs

Hint 2

After counting, scan through your frequency map to find the maximum count; remember to handle the tie-breaking rule

Hint 3

Python dictionaries maintain insertion order (since 3.7), which helps implement the earliest-appearance tie-breaker naturally