cswork / libraryOpen practice workspace

Coding questions · Notion

Analyze document collaboration from CSV logs

This question evaluates data manipulation and analytical competencies—particularly deduplication, joins, aggregation, co-occurrence counting, and metric definition—applied to CSV access logs and user metadata.

You are given two CSV files exported from a document collaboration product.

Data

1) doc_access.csv

Each row represents a user accessing (viewing/editing) a document page.

columntypedescription
page_idSTRING/INTDocument/page identifier
user_idSTRING/INTUser who accessed the document
event_tsTIMESTAMPWhen the access happened (assume UTC)
is_creatorBOOLEANTRUE if this user is the creator/owner of the document; otherwise the user is a collaborator
collab_sourceSTRINGFor collaborators only (is_creator=FALSE): how the user became a collaborator (e.g., invited, link share, org default, etc.)

Notes/assumptions:

  • A “collaborator” is any record with is_creator = FALSE.
  • A user may access the same page_id multiple times; unless otherwise specified, treat collaboration at the document level (i.e., dedupe by (page_id, user_id) where appropriate).

2) users.csv

columntypedescription
user_idSTRING/INTUser identifier
countrySTRINGUser’s country

Tasks

Q1) Distribution of collaborator sources

Compute the distribution of how collaborators got onto documents.

Output:

  • collab_source
  • num_collaborators (count of collaborator users; specify whether you count unique users overall or unique (page_id, user_id) pairs)
  • pct_of_collaborators

Q2) For a given user, who did they collaborate with most?

Given an input parameter target_user_id, find the other user they collaborated with the most.

Define “collaborated with” as: the two users both appear on the same page_id (after deduping to unique (page_id, user_id) memberships).

Output:

  • target_user_id
  • top_collaborator_user_id
  • num_shared_pages (number of distinct page_id where both users appear)

Also describe how you would break ties (e.g., smallest user_id, most recent shared collaboration, etc.).

Q3) Which country collaborates the most?

Determine which country’s users are most likely to collaborate with others.

Because “collaboration rate” can be defined multiple ways, do the following:

  1. Propose a clear numerator and denominator for a per-country collaboration rate.
    • Example definition (acceptable):
      • Numerator: # of users in the country who have collaborated with at least one other distinct user on at least one page.
      • Denominator: total # of users in the country.
  2. Compute the collaboration rate by country and return the top country.

Output:

  • country
  • collaboration_rate
  • any intermediate counts you used (e.g., num_users_in_country, num_users_who_collaborated).

Distribution of how collaborators were added to documents

You are given a CSV-style activity log that records users viewing/working on documents. Each row includes the document (page_id), the user (user_id), whether they are the creator of that document (is_creator), and if not the creator, how they became a collaborator (added_via).

Compute the distribution of collaborator acquisition sources: count how many distinct (page_id, user_id) collaborator relationships came from each added_via source. Treat NULL added_via as 'unknown'.

Return: added_via, collaborator_count.

Table document_activity

columntypeconstraints
page_idINTNOT NULL
user_idINTNOT NULL
activity_tsTIMESTAMPNOT NULL
is_creatorSMALLINTNOT NULL
added_viaVARCHAR(30)

Sample data:

activity_tsadded_viais_creatorpage_iduser_id
2025-05-01 09:00:00NULL11001
2025-05-01 09:10:00invite01002
2025-05-02 10:00:00invite01002
2025-05-01 09:20:00share_link01003
2025-05-03 08:00:00invite01004
2025-05-05 12:00:00NULL11012
2025-05-05 12:05:00team_drive01011
2025-05-06 14:00:00share_link01013
2025-05-07 09:00:00NULL11023
2025-05-10 10:00:00NULL11034
2025-05-10 10:30:00invite01035
2025-05-11 11:00:00share_link01036
2025-05-12 08:00:00NULL11045
2025-05-12 08:15:00invite01041
2025-05-12 08:20:00invite01042
2025-05-12 09:00:00NULL01046
2025-05-20 13:00:00NULL11058
2025-05-25 09:00:00NULL11061
2025-05-25 09:05:00invite01063
2025-05-26 09:05:00invite01063

Table users

columntypeconstraints
user_idINTPRIMARY KEY, NOT NULL
countryVARCHAR(50)

Sample data:

countryuser_id
US1
US2
CN3
IN4
DE5
NULL6
US7
JP8

Expected output

One row per collaborator acquisition source (including 'unknown'), counting distinct (page_id, user_id) collaborator relationships.

added_viacollaborator_count
invite6
share_link3
team_drive1
unknown1

Hints

  1. Count distinct (page_id, user_id) pairs so repeated activity events don't inflate the totals.
  2. Use COALESCE to bucket NULL sources into an 'unknown' category.

For a given user, find who they collaborated with the most

Using the same document activity log, define two users as having collaborated if they both appear on the same page_id (either as creator or collaborator). For user_id = 1, find the user(s) they collaborated with on the largest number of distinct pages.

Return all tied top collaborators.

Output columns: collaborator_user_id, shared_pages.

Table document_activity

columntypeconstraints
page_idINTNOT NULL
user_idINTNOT NULL
activity_tsTIMESTAMPNOT NULL
is_creatorSMALLINTNOT NULL
added_viaVARCHAR(30)

Sample data:

activity_tsadded_viais_creatorpage_iduser_id
2025-05-01 09:00:00NULL11001
2025-05-01 09:10:00invite01002
2025-05-02 10:00:00invite01002
2025-05-01 09:20:00share_link01003
2025-05-03 08:00:00invite01004
2025-05-05 12:00:00NULL11012
2025-05-05 12:05:00team_drive01011
2025-05-06 14:00:00share_link01013
2025-05-07 09:00:00NULL11023
2025-05-10 10:00:00NULL11034
2025-05-10 10:30:00invite01035
2025-05-11 11:00:00share_link01036
2025-05-12 08:00:00NULL11045
2025-05-12 08:15:00invite01041
2025-05-12 08:20:00invite01042
2025-05-12 09:00:00NULL01046
2025-05-20 13:00:00NULL11058
2025-05-25 09:00:00NULL11061
2025-05-25 09:05:00invite01063
2025-05-26 09:05:00invite01063

Table users

columntypeconstraints
user_idINTPRIMARY KEY, NOT NULL
countryVARCHAR(50)

Sample data:

countryuser_id
US1
US2
CN3
IN4
DE5
NULL6
US7
JP8

Expected output

User(s) who share the most distinct page_ids with user_id=1, including ties.

collaborator_user_idshared_pages
23
33

Hints

  1. First deduplicate to one row per (page_id, user_id) before counting shared pages.
  2. A self-join (or join via the target user's pages) plus DENSE_RANK helps handle ties.

Country with the highest collaboration rate

Using tables users(user_id, country) and document_activity, define a user as a "collaborator" if they have at least one activity row with is_creator = 0 (i.e., they collaborated on someone else's document at least once).

For each country, compute:

  • total_users: number of users in that country
  • collaborator_users: number of users in that country who are collaborators
  • collaboration_rate = collaborator_users / total_users

Treat NULL country as 'Unknown'. Return the country (or countries) with the highest collaboration_rate (include ties).

Output columns: country, total_users, collaborator_users, collaboration_rate.

Table document_activity

columntypeconstraints
page_idINTNOT NULL
user_idINTNOT NULL
activity_tsTIMESTAMPNOT NULL
is_creatorSMALLINTNOT NULL
added_viaVARCHAR(30)

Sample data:

activity_tsadded_viais_creatorpage_iduser_id
2025-05-01 09:00:00NULL11001
2025-05-01 09:10:00invite01002
2025-05-02 10:00:00invite01002
2025-05-01 09:20:00share_link01003
2025-05-03 08:00:00invite01004
2025-05-05 12:00:00NULL11012
2025-05-05 12:05:00team_drive01011
2025-05-06 14:00:00share_link01013
2025-05-07 09:00:00NULL11023
2025-05-10 10:00:00NULL11034
2025-05-10 10:30:00invite01035
2025-05-11 11:00:00share_link01036
2025-05-12 08:00:00NULL11045
2025-05-12 08:15:00invite01041
2025-05-12 08:20:00invite01042
2025-05-12 09:00:00NULL01046
2025-05-20 13:00:00NULL11058
2025-05-25 09:00:00NULL11061
2025-05-25 09:05:00invite01063
2025-05-26 09:05:00invite01063

Table users

columntypeconstraints
user_idINTPRIMARY KEY, NOT NULL
countryVARCHAR(50)

Sample data:

countryuser_id
US1
US2
CN3
IN4
DE5
NULL6
US7
JP8

Expected output

Country (or countries) with the maximum collaborator_users/total_users rate, including ties. NULL country is grouped as 'Unknown'.

collaboration_ratecollaborator_userscountrytotal_users
11CN1
11DE1
11IN1
11Unknown1

Hints

  1. Create a per-user flag: did this user ever appear with is_creator=0?
  2. Compute per-country totals and collaborator totals, then rank by the rate to keep ties.