← Questions
SQL: each user's first share and closest collaborator
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Figma's technical phone screen, last asked Jan 2026. It tests sql, window functions and joins.
You have data about people opening design files. A file is "shared" when someone other than its creator views it. Write two queries: the first time each creator shared one of their files, and each creator's closest collaborator.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
files(file_id, creator_id)—creator_idis aVARCHAR. -
file_views(file_id, user_id, viewed_at)—user_idis anINTEGER. One row per view. - Comparing a creator with a viewer needs a cast, since the two id columns have different types.
Queries:
-
First share per creator:
creator_id, first_shared_at— the earliestviewed_atat which someone other than the creator viewed any of the creator's files. A creator viewing their own file doesn't count. -
Closest collaborator: for each file, the first person it was shared with is its earliest non-creator viewer. For each creator, return
creator_id, closest_collaborator_user_id, collaborator_value. The collaborator is the user who was the first person shared with on the most of that creator's files, andcollaborator_valueis that number of files. Ties go to the earliest first-share time, then the smallest user id.
Example
files: f1, f2, f3 by "10"; f4 by "20"
file_views: f1: 10 at 03-01, 11 at 03-02, 12 at 03-03
f2: 12 at 03-04, 11 at 03-05
f3: 11 at 03-06
f4: 20 at 03-01, 10 at 03-07
Query 1: ("10", 03-02), ("20", 03-07)
first non-creator viewer: f1 -> 11, f2 -> 12, f3 -> 11, f4 -> 10
Query 2: ("10", 11, 2), ("20", 10, 1)
Constraints
-
file_viewshas hundreds of millions of rows. Two views of one file can share a timestamp.
Clarifying questions
- Is every non-creator view a share, or only views after an explicit share action?
- How should two viewers at the same timestamp be ordered when picking a file's first viewer?
- Should creators who never shared appear in query 1 with a null?
- Can
creator_idhold values that don't cast to an integer?
What a strong answer covers
- Query 1: join on
file_id, filterCAST(v.user_id AS VARCHAR) <> f.creator_id(or cast the other way, safely),GROUP BY creator_idwithMIN(viewed_at). - Query 2 in steps with CTEs:
ROW_NUMBER() OVER (PARTITION BY file_id ORDER BY viewed_at, user_id)to get each file's first viewer, count per (creator, user), then rank per creator by count desc, earliest time, smallest id, and keep rank 1. - Why
ROW_NUMBERrather thanRANKat each step, and how ties are made deterministic. - Checking both queries on a small hand-built table, and indexes or partitioning that make them fast on the real one.
Follow-ups
- Return the top 3 collaborators per creator.
- Count collaboration in both directions (A first opened B's file, or B first opened A's) and find mutual closest collaborators.
- Turn query 2 into a daily incremental model instead of a full recompute.
Where it's asked
- Figma · Technical phone screen · Last asked Jan 202660 min · Pair programming in your own language against tests the interviewer provides
What it tests
sql · window functions · joins
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
