Trace Invite Chains Back to the Original Inviter With a Recursive CTE
Last updated: November 29, 2026
Quick Overview
Facebook signups record who invited each new user. For every organic (uninvited) user, compute how many users descend from them through invite chains of any length and the maximum chain depth.
Meta
November 29, 20260
0
0 solved
Facebook signups record who invited each new user. For every organic (uninvited) user, compute how many users descend from them through invite chains of any length and the maximum chain depth.
Growth teams at Meta study viral invite trees, and data engineering onsites use this to test recursive SQL on a self-referencing table. The interviewer checks anchor and recursive members, cycle and self-invite safety, and aggregating per root.
What the Interviewer Expects
- Writes a recursive CTE with an anchor on root users
- Carries the root id and depth through the recursion
- Guards against cycles and self invites with a depth cap or path check
- Aggregates descendants and max depth per root
- Discusses performance and when to precompute in a pipeline
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you detect actual cycles rather than just stopping at a depth cap?
- How would you compute per-level counts (how many at depth 1, 2, 3)?
- How would you do this in Spark without recursion?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
signups(user_id, invited_by, signed_up_at). invited_by is NULL for organic users. Bad data exists: a few rows where invited_by = user_id. Each user has at most one inviter, so the data forms a f...
Approach
Anchor: roots (NULL inviter or self invite) with depth 0 and root_id equal to themselves. Recursive step: join signups whose inviter is a user already in the chain, carrying root_id and depth + 1. A d...