KatInterview

Co-Access Analytics on Document Usage Logs

SQL & DataAuto-graded · 15 test cases
EasyAuto-gradedSQL & Datadata-manipulationaggregationjoins

Last updated

Tests data deduplication, self-joins, metric definition, and grouped aggregation over access logs with user metadata.

You are building analytics for a document platform. You receive two lists of records representing document access events and user profiles.

Input Data

1. access_log (list of dictionaries)

Each dictionary represents one access event when a user opened or viewed a document page.

FieldTypeDescription
page_idstring or intIdentifier of the document page
user_idstring or intIdentifier of the user who accessed it
event_tsstringTimestamp of the access event (UTC)
is_creatorboolTrue if this user created/owns the document, False otherwise
collab_sourcestring or NoneFor non-creators only: how the user gained access (e.g., "invited", "link share", "org default"). May be None or empty.

Notes:

  • The same user may access the same page multiple times, producing duplicate (page_id, user_id) entries.
  • Unless otherwise stated, treat access at the page membership level: deduplicate to unique (page_id, user_id) pairs.
  • A user with is_creator=True is considered a participant on that page.

2. user_list (list of dictionaries)

Each dictionary contains profile information for a user.

FieldTypeDescription
user_idstring or intUser identifier
countrystringCountry code or name for the user

3. focus_user (string or int)

A user identifier for which you will compute a specific statistic in part 2.


Tasks

Return a dictionary with three keys: 'q1', 'q2', and 'q3', each containing the result of one task below.

Task 1: Source Distribution for Non-Creator Access

For all non-creator access (is_creator=False), compute how many unique (page_id, user_id) memberships exist for each collab_source value (ignoring None or empty sources).

Output (value of 'q1' key): A list of dictionaries, sorted alphabetically by collab_source, each containing:

  • collab_source (string): the source value
  • num_collaborators (int): count of unique (page_id, user_id) pairs with this source
  • pct_of_collaborators (float): percentage this source represents out of all non-creator memberships with a source (0–100 scale)

If there are no non-creator memberships with sources, return an empty list.


Task 2: Most Frequent Co-Participant for a Given User

Find which other user appears on the most pages in common with focus_user.

Define co-participation as: both users have at least one access record on the same page_id (after deduplication to unique memberships).

Output (value of 'q2' key): A dictionary containing:

  • target_user_id: the focus_user value passed in
  • top_collaborator_user_id: the other user with the highest count of shared pages, or None if no co-participants exist
  • num_shared_pages (int): the number of distinct pages both users appear on

Tie-breaking: If multiple users tie for the maximum number of shared pages, return the one with the smallest user_id (lexicographically for strings, numerically for integers).


Task 3: Country with Highest Co-Participation Rate

Determine which country has the highest fraction of its users who co-participated with at least one other distinct user.

Metric definition:

  • Numerator: Count of users in the country who appear on at least one page that has two or more distinct users (i.e., they shared a page with someone else).
  • Denominator: Total count of users in the country according to user_list.
  • Rate: Numerator divided by denominator (return as a decimal between 0 and 1).

Users in access_log but not in user_list have no country and are excluded. Users in user_list who never accessed any page contribute to the denominator but not the numerator.

Output (value of 'q3' key): A dictionary containing:

  • country (string or None): the country with the highest rate, or None if user_list is empty
  • collaboration_rate (float): the rate (0–1), or -1 if no country exists
  • num_users_in_country (int): total users in that country
  • num_users_who_collaborated (int): users in that country who co-participated

Tie-breaking: Highest rate first; if tied, choose the country that is smallest alphabetically.


Constraints

  • 0 ≤ len(access_log) ≤ 10⁵
  • 0 ≤ len(user_list) ≤ 10⁴
  • All page_id and user_id values are hashable (strings or integers).
  • collab_source may be None, an empty string, or a non-empty string.
  • focus_user may or may not appear in access_log.

Examples

Example 1

Input: compute_access_analytics([{'page_id': 'p1', 'user_id': 'u1', 'event_ts': '2024-01-01T00:00:00Z', 'is_creator': True, 'collab_source': None}, {'page_id': 'p1', 'user_id': 'u2', 'event_ts': '2024-01-01T01:00:00Z', 'is_creator': False, 'collab_source': 'invited'}, {'page_id': 'p1', 'user_id': 'u2', 'event_ts': '2024-01-01T02:00:00Z', 'is_creator': False, 'collab_source': 'invited'}, {'page_id': 'p2', 'user_id': 'u1', 'event_ts': '2024-01-02T00:00:00Z', 'is_creator': True, 'collab_source': None}, {'page_id': 'p2', 'user_id': 'u3', 'event_ts': '2024-01-02T01:00:00Z', 'is_creator': False, 'collab_source': 'link share'}, {'page_id': 'p2', 'user_id': 'u4', 'event_ts': '2024-01-02T02:00:00Z', 'is_creator': False, 'collab_source': 'link share'}], [{'user_id': 'u1', 'country': 'US'}, {'user_id': 'u2', 'country': 'CA'}, {'user_id': 'u3', 'country': 'CA'}, {'user_id': 'u4', 'country': 'US'}], 'u1')

Output: {'q1': [{'collab_source': 'invited', 'num_collaborators': 1, 'pct_of_collaborators': 33.33333333333333}, {'collab_source': 'link share', 'num_collaborators': 2, 'pct_of_collaborators': 66.66666666666666}], 'q2': {'target_user_id': 'u1', 'top_collaborator_user_id': 'u2', 'num_shared_pages': 1}, 'q3': {'country': 'CA', 'collaboration_rate': 1, 'num_users_in_country': 2, 'num_users_who_collaborated': 2}}

Example 2

Input: compute_access_analytics([{'page_id': 1, 'user_id': 10, 'event_ts': '2024-02-01', 'is_creator': True, 'collab_source': None}, {'page_id': 1, 'user_id': 20, 'event_ts': '2024-02-01', 'is_creator': False, 'collab_source': 'org default'}, {'page_id': 2, 'user_id': 10, 'event_ts': '2024-02-02', 'is_creator': True, 'collab_source': None}, {'page_id': 2, 'user_id': 30, 'event_ts': '2024-02-02', 'is_creator': False, 'collab_source': 'invited'}, {'page_id': 3, 'user_id': 20, 'event_ts': '2024-02-03', 'is_creator': True, 'collab_source': None}], [{'user_id': 10, 'country': 'US'}, {'user_id': 20, 'country': 'US'}, {'user_id': 30, 'country': 'GB'}, {'user_id': 40, 'country': 'GB'}], 20)

Output: {'q1': [{'collab_source': 'invited', 'num_collaborators': 1, 'pct_of_collaborators': 50}, {'collab_source': 'org default', 'num_collaborators': 1, 'pct_of_collaborators': 50}], 'q2': {'target_user_id': 20, 'top_collaborator_user_id': 10, 'num_shared_pages': 1}, 'q3': {'country': 'US', 'collaboration_rate': 1, 'num_users_in_country': 2, 'num_users_who_collaborated': 2}}

Hints

Hint 1

Deduplicate access_log to unique (page_id, user_id) pairs first; all three tasks rely on this membership set.

Hint 2

For task 2, build a mapping from each page to its set of users, then count how many of the focus_user's pages contain each other user.

Hint 3

For task 3, a user has co-participated if they belong to any page with at least two distinct members; avoid building all pairs explicitly.