Write SQL for first share and closest collaborator
FigmaData Engineer · Technical ScreenLast updated
This question evaluates SQL proficiency—specifically joins, filtering, type casting, temporal aggregation, window functions or deduplication, and deterministic tie-breaking—by requiring derivation of first non-creator view times and per-creator collaborator counts from event tables.
You are given event data about users viewing design files.
Assume the following tables:
files(file_id, creator_id)creator_idis a VARCHAR user identifier.file_views(file_id, user_id, viewed_at)user_idis an INTEGER user identifier.- Each row means
user_idviewedfile_idat timeviewed_at.
A “share” is inferred when someone other than the creator views a file.
- First share time per creator
Write a SQL query that returns, for each creator, the first time they ever shared a file they created (i.e., the earliest viewed_at where a non-creator viewed one of their files).
Output columns:
creator_idfirst_shared_at
Notes:
- A creator viewing their own file should not count.
creator_id(VARCHAR) anduser_id(INTEGER) are different types; handle casting appropriately.
- Closest collaborator model
For each file, define the “first person it was shared with” as the earliest non-creator viewer of that file (based on viewed_at).
Build a query/model that, for each creator, finds their closest collaborator, defined as the user who was the “first person shared with” for the creator’s files the most times.
Output columns:
creator_idclosest_collaborator_user_idcollaborator_value(the number of files for which this collaborator was the first non-creator viewer)
Tie-breaking: if multiple collaborators have the same collaborator_value, choose the one with the earliest first-share timestamp across those tied records (and if still tied, the smallest user id).
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.