KatInterview

Compute Usage and Quality Metrics from Workplace Search Logs

Arrays & HashingAuto-graded · 17 test cases
EasyAuto-gradedArrays & Hashinghash-mapaggregationsimulation

Last updated

Aggregate a list of search log records into funnel, quality, latency and repeat-usage metrics, handling empty input and even-length medians.

You are given the event log of a workplace search tool. Each entry describes one search performed by an employee. Your job is to condense the log into a small set of health metrics.

Input

search_log is a list of dictionaries, one per search. Every dictionary has these keys:

  • user_id (string): who ran the search.
  • day (integer): the day the search happened.
  • results (nonnegative integer): how many results were returned.
  • clicked (boolean): whether the user clicked any result.
  • satisfied (boolean): whether the search counts as successful.
  • latency_ms (nonnegative number): response time in milliseconds.

Output

Return a dictionary with exactly these keys, and every value is a float:

KeyMeaning
total_searchesNumber of entries in the log.
unique_usersNumber of distinct user_id values.
search_success_rateFraction of searches with satisfied true.
click_through_rateFraction of searches with clicked true.
zero_result_rateFraction of searches where results equals 0.
median_latency_msMedian of all latency_ms values.
repeat_user_rateFraction of distinct users who searched on at least 2 different days.

Rules

  • All rates are computed over the total number of searches, except repeat_user_rate, which is computed over the number of distinct users.
  • The median of an odd number of values is the middle value after sorting. For an even number, it is the average of the two middle values.
  • A user counts as a repeat user only if they have searches on two or more distinct days; several searches on the same day do not count.
  • If the log is empty, return 0.0 for every key.

Constraints

  • 0 <= len(search_log) <= 10^5
  • Every entry contains all six keys with the types described above.
  • day values are integers and may be any value, not necessarily consecutive.
  • Values should be returned as floats.

Examples

Example 1

Input: summarize_search_log([])

Output: {'total_searches': 0, 'unique_users': 0, 'search_success_rate': 0, 'click_through_rate': 0, 'zero_result_rate': 0, 'median_latency_ms': 0, 'repeat_user_rate': 0}

Example 2

Input: summarize_search_log([{'user_id': 'alice', 'day': 1, 'results': 5, 'clicked': True, 'satisfied': True, 'latency_ms': 120}, {'user_id': 'bob', 'day': 1, 'results': 0, 'clicked': False, 'satisfied': False, 'latency_ms': 300}, {'user_id': 'alice', 'day': 2, 'results': 2, 'clicked': True, 'satisfied': True, 'latency_ms': 180}])

Output: {'total_searches': 3, 'unique_users': 2, 'search_success_rate': 0.6666666666666666, 'click_through_rate': 0.6666666666666666, 'zero_result_rate': 0.3333333333333333, 'median_latency_ms': 180, 'repeat_user_rate': 0.5}

Example 3

Input: summarize_search_log([{'user_id': 'alice', 'day': 1, 'results': 1, 'clicked': False, 'satisfied': False, 'latency_ms': 100}, {'user_id': 'bob', 'day': 1, 'results': 4, 'clicked': True, 'satisfied': False, 'latency_ms': 200}, {'user_id': 'carol', 'day': 1, 'results': 3, 'clicked': True, 'satisfied': True, 'latency_ms': 400}, {'user_id': 'dave', 'day': 2, 'results': 0, 'clicked': False, 'satisfied': False, 'latency_ms': 500}])

Output: {'total_searches': 4, 'unique_users': 4, 'search_success_rate': 0.25, 'click_through_rate': 0.5, 'zero_result_rate': 0.25, 'median_latency_ms': 300, 'repeat_user_rate': 0}

Hints

Hint 1

Make a single pass over the log, keeping counters for successes, clicks and zero-result searches, and a dictionary mapping each user to the set of days they were active.

Hint 2

For the median, collect all latencies, sort them, and pick the middle element or average the two middle ones depending on the parity of the count.

Hint 3

Handle the empty log up front so you never divide by zero; the repeat rate divides by distinct users, which is nonzero whenever the log is nonempty.