KatInterview

Shared-Document Analytics: Sources, Partners, and Country Rates

SQL & DataModel answer
MediumModel answerSQL & Datasqlpandasself-join

Last updated

Use deduplicated user-document pairs to analyze how collaborators arrive, who works most with a given user, and which country has the highest share of users who collaborate.

You have two CSV exports from a document-editing product and need to answer three questions about how people work together.

document_activity.csv (one row per view event)

ColumnTypeMeaning
page_idSTRINGDocument identifier
user_idSTRINGPerson who opened the document
viewed_atTIMESTAMP (UTC)When the view happened
is_creatorBOOLEANtrue if the viewer created the document
collaborator_sourceSTRING, nullableHow a non-creator reached the document (e.g. invite link, email invite, search, direct). May be NULL, especially for creators

users.csv

ColumnTypeMeaning
user_idSTRINGUnique user identifier
countrySTRINGUser's country

Ground rules

  • Join on document_activity.user_id = users.user_id.
  • Use all rows; no date filtering.
  • The same person may open a document many times. Unless a task asks for event counts, reduce the data to distinct (page_id, user_id) pairs first.
  • A document has one creator and may have many collaborators.

Tasks

  1. Source breakdown. Looking only at non-creators (is_creator = false) and counting distinct (page_id, user_id) pairs, report how collaborators arrived. Output: collaborator_source, collaborator_count, collaborator_share.

  2. Closest partners of one user. Given a user_id, find the other users who shared the most documents with them. Two users share a document if both viewed that page_id at least once. Count distinct shared documents, not views, and exclude the target. Output: target_user_id, co_user_id, shared_page_count. Return every user tied for the maximum.

  3. Country collaboration rate. A user "collaborated" if they appear on at least one document that has two or more distinct users in total. For each country compute collaborating_users, total_users (distinct users of that country in users.csv), and collaboration_rate = collaborating_users / total_users. Rank countries from highest to lowest rate. Output: country, collaborating_users, total_users, collaboration_rate.

Write the solution in SQL or Python (pandas). Explain the assumptions you make about messy data, state the complexity, and mention edge cases you would test.

Hints

Hint 1

Build one deduplicated table of distinct (page_id, user_id) pairs first and derive all three answers from it, so view counts never leak into the metrics.

Hint 2

For task 1, a pair can show up with different sources on different views, and sources can be NULL. Decide on a rule (for example, the earliest view's source) and keep NULL as its own bucket so shares sum to 1.

Hint 3

For task 2, restrict to the target's pages before self-joining on page_id, then count distinct pages per co-user and keep all ties at the maximum. For task 3, take the denominator from users.csv and left join so countries with zero collaborators appear with rate 0.