Compute Usage and Quality Metrics from Workplace Search Logs
Arrays & HashingAuto-graded · 17 test casesLast 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:
| Key | Meaning |
|---|---|
total_searches | Number of entries in the log. |
unique_users | Number of distinct user_id values. |
search_success_rate | Fraction of searches with satisfied true. |
click_through_rate | Fraction of searches with clicked true. |
zero_result_rate | Fraction of searches where results equals 0. |
median_latency_ms | Median of all latency_ms values. |
repeat_user_rate | Fraction 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.0for every key.
Constraints
0 <= len(search_log) <= 10^5- Every entry contains all six keys with the types described above.
dayvalues 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.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Do one pass over the log while maintaining:
- a dictionary from each user to the set of distinct days on which they searched,
- a list of all latencies,
- running counts of satisfied searches, clicked searches and zero-result searches.
After the pass:
- The total number of searches is the length of the log, and the number of unique users is the number of keys in the dictionary.
- The three per-search rates are the respective counts divided by the total number of searches.
- The median latency comes from sorting the latency list: the middle element for odd length, or the mean of the two middle elements for even length.
- The repeat user rate is the number of users whose day set has size at least 2, divided by the number of unique users.
An empty log is handled first by returning zeros for all keys, which avoids division by zero.
Why it is correct
Each metric is computed directly from its definition. Using a set of days per user ensures that multiple searches on the same day do not make someone a repeat user. Dividing by the right denominator (searches for the three rates, users for the repeat rate) matches the rules. Since the log is nonempty after the guard, both denominators are positive.
Complexity
- Time: O(n log n), dominated by sorting the latencies; the aggregation pass is O(n).
- Space: O(n) for the latency list and the per-user day sets.
Reference solution
from typing import List, Dict
def summarize_search_log(search_log: List[Dict]) -> Dict[str, float]:
if not search_log:
return {
"total_searches": 0.0,
"unique_users": 0.0,
"search_success_rate": 0.0,
"click_through_rate": 0.0,
"zero_result_rate": 0.0,
"median_latency_ms": 0.0,
"repeat_user_rate": 0.0,
}
days_by_user = {}
latencies = []
successes = 0
clicks = 0
zero_results = 0
for entry in search_log:
uid = entry["user_id"]
days_by_user.setdefault(uid, set()).add(entry["day"])
latencies.append(entry["latency_ms"])
successes += 1 if entry["satisfied"] else 0
clicks += 1 if entry["clicked"] else 0
zero_results += 1 if entry["results"] == 0 else 0
latencies.sort()
n = len(latencies)
if n % 2 == 1:
median = float(latencies[n // 2])
else:
median = (latencies[n // 2 - 1] + latencies[n // 2]) / 2.0
user_count = len(days_by_user)
repeaters = sum(1 for days in days_by_user.values() if len(days) >= 2)
return {
"total_searches": float(n),
"unique_users": float(user_count),
"search_success_rate": successes / n,
"click_through_rate": clicks / n,
"zero_result_rate": zero_results / n,
"median_latency_ms": median,
"repeat_user_rate": repeaters / user_count,
}