Find each file's first share recipient and its owner's closest collaborator by edits

Quick Overview

Write a SQL query that returns, for every file, the first user it was shared with and the owner's closest collaborator, ranked by edits on files the owner also edited. It tests picking one row per group with tiebreakers, keeping unmatched rows with outer joins, and counting edits without double counting.

Find each file's first share recipient and its owner's closest collaborator by edits

Company: Figma

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

For every file in `files`, return `file_id`, `owner_id`, `first_shared_with` and `closest_collaborator`, one row per file ordered by `file_id` ascending. - `first_shared_with` is the `shared_with_id` of the file's earliest share by `shared_at` (ties: smaller `share_id`), or `NULL` if the file was never shared. - `closest_collaborator` is computed for the file's owner: among users other than `owner_id` who edited any file the owner also edited, the one with the most edits on those shared files (ties: smaller `user_id`). - `closest_collaborator` is `NULL` if no other user edited any of the files the owner edited. ```hint Count carefully Work out what your edit count for a collaborator becomes when the owner edited the same file several times. ``` ### Input Table(s) `files` | Column | Type | Meaning | |---|---|---| | file_id | INT | File identifier, primary key | | owner_id | INT | User who owns the file, NOT NULL | `file_shares` | Column | Type | Meaning | |---|---|---| | share_id | INT | Share identifier, primary key | | file_id | INT | The shared file, foreign key to `files.file_id` | | shared_with_id | INT | User the file was shared with, NOT NULL | | shared_at | TIMESTAMP | When the share happened, NOT NULL | `file_edits` | Column | Type | Meaning | |---|---|---| | edit_id | INT | Edit identifier, primary key | | file_id | INT | The edited file, foreign key to `files.file_id` | | user_id | INT | User who made the edit, NOT NULL | Keys: `files.file_id`, `file_shares.share_id` and `file_edits.edit_id` are primary keys. `file_edits` has one row per edit, so the same user can appear many times for the same file. ### Output Contract | Column | Type | Meaning | |---|---|---| | file_id | INT | The file | | owner_id | INT | The file's owner | | first_shared_with | INT | Recipient of the file's earliest share, or `NULL` | | closest_collaborator | INT | The owner's closest collaborator, or `NULL` | Return exactly one row per row of `files`, ordered by `file_id` ascending. `file_id` is unique, so the order is fully determined. No rounding applies. ### Example `files` | file_id | owner_id | |---|---| | 100 | 1 | | 101 | 1 | | 102 | 2 | | 103 | 3 | | 104 | 6 | `file_shares` | share_id | file_id | shared_with_id | shared_at | |---|---|---|---| | 1 | 100 | 4 | 2024-05-01 10:00:00 | | 2 | 100 | 2 | 2024-05-01 09:00:00 | | 3 | 102 | 5 | 2024-05-02 12:00:00 | | 4 | 102 | 1 | 2024-05-02 12:00:00 | | 5 | 103 | 4 | 2024-05-03 08:00:00 | `file_edits` | edit_id | file_id | user_id | |---|---|---| | 1 | 100 | 1 | | 2 | 100 | 1 | | 3 | 102 | 1 | | 4 | 100 | 2 | | 5 | 102 | 2 | | 6 | 102 | 2 | | 7 | 100 | 3 | | 8 | 100 | 3 | | 9 | 100 | 3 | | 10 | 103 | 4 | | 11 | 103 | 4 | | 12 | 104 | 6 | Expected output: | file_id | owner_id | first_shared_with | closest_collaborator | |---|---|---|---| | 100 | 1 | 2 | 2 | | 101 | 1 | NULL | 2 | | 102 | 2 | 5 | 1 | | 103 | 3 | 4 | 1 | | 104 | 6 | NULL | NULL | File 100's earliest share is share 2 at 09:00, so it was first shared with user 2. File 102's two shares have the same time, and the smaller `share_id` (3) wins. Owner 1 edited files 100 and 102. User 2 has 3 edits on them and user 3 also has 3, so the tie goes to user 2. Owner 3 edited only file 100, where user 1 has 2 edits and user 2 has 1. User 4's edits on file 103 do not count because owner 3 never edited that file. Nobody but owner 6 edited file 104. ### Constraints and Clarifications - `files` has 1 to ${10^4}$ rows, `file_shares` has 0 to ${10^5}$ rows and `file_edits` has 0 to ${10^5}$ rows. - All `*_id` columns are positive integers, and no column holds NULL. - Every file appears in the output, including files that were never shared and files whose owner has no collaborator. - The closest collaborator depends only on the owner, so all files with the same owner get the same `closest_collaborator`. - "Files the owner edited" means files with at least one `file_edits` row by the owner, whoever owns them. A file the owner owns but never edited does not count. - A collaborator's count is the number of their `file_edits` rows on those files. Each of their edits counts once, however many times the owner edited the same file.

Overview: Write a SQL query that returns, for every file, the first user it was shared with and the owner's closest collaborator, ranked by edits on files the owner also edited. It tests picking one row per group with tiebreakers, keeping unmatched rows with outer joins, and counting edits without double counting.

Figma logo
Figma
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

For every file in files, return file_id, owner_id, first_shared_with and closest_collaborator, one row per file ordered by file_id ascending.

  • first_shared_with is the shared_with_id of the file's earliest share by shared_at (ties: smaller share_id ), or NULL if the file was never shared.
  • closest_collaborator is computed for the file's owner: among users other than owner_id who edited any file the owner also edited, the one with the most edits on those shared files (ties: smaller user_id ).
  • closest_collaborator is NULL if no other user edited any of the files the owner edited.

Input Table(s)

files

ColumnTypeMeaning
file_idINTFile identifier, primary key
owner_idINTUser who owns the file, NOT NULL

file_shares

ColumnTypeMeaning
share_idINTShare identifier, primary key
file_idINTThe shared file, foreign key to files.file_id
shared_with_idINTUser the file was shared with, NOT NULL
shared_atTIMESTAMPWhen the share happened, NOT NULL

file_edits

ColumnTypeMeaning
edit_idINTEdit identifier, primary key
file_idINTThe edited file, foreign key to files.file_id
user_idINTUser who made the edit, NOT NULL

Keys: files.file_id, file_shares.share_id and file_edits.edit_id are primary keys. file_edits has one row per edit, so the same user can appear many times for the same file.

Output Contract

ColumnTypeMeaning
file_idINTThe file
owner_idINTThe file's owner
first_shared_withINTRecipient of the file's earliest share, or NULL
closest_collaboratorINTThe owner's closest collaborator, or NULL

Return exactly one row per row of files, ordered by file_id ascending. file_id is unique, so the order is fully determined. No rounding applies.

Example

files

file_idowner_id
1001
1011
1022
1033
1046

file_shares

share_idfile_idshared_with_idshared_at
110042024-05-01 10:00:00
210022024-05-01 09:00:00
310252024-05-02 12:00:00
410212024-05-02 12:00:00
510342024-05-03 08:00:00

file_edits

edit_idfile_iduser_id
11001
21001
31021
41002
51022
61022
71003
81003
91003
101034
111034
121046

Expected output:

file_idowner_idfirst_shared_withclosest_collaborator
100122
1011NULL2
102251
103341
1046NULLNULL

File 100's earliest share is share 2 at 09:00, so it was first shared with user 2. File 102's two shares have the same time, and the smaller share_id (3) wins. Owner 1 edited files 100 and 102. User 2 has 3 edits on them and user 3 also has 3, so the tie goes to user 2. Owner 3 edited only file 100, where user 1 has 2 edits and user 2 has 1. User 4's edits on file 103 do not count because owner 3 never edited that file. Nobody but owner 6 edited file 104.

Constraints and Clarifications

  • files has 1 to 104{10^4} rows, file_shares has 0 to 105{10^5} rows and file_edits has 0 to 105{10^5} rows.
  • All *_id columns are positive integers, and no column holds NULL.
  • Every file appears in the output, including files that were never shared and files whose owner has no collaborator.
  • The closest collaborator depends only on the owner, so all files with the same owner get the same closest_collaborator .
  • "Files the owner edited" means files with at least one file_edits row by the owner, whoever owns them. A file the owner owns but never edited does not count.
  • A collaborator's count is the number of their file_edits rows on those files. Each of their edits counts once, however many times the owner edited the same file.