Find Reciprocal Instagram Follow Pairs from a Directed Edge List
Last updated: November 29, 2026
Quick Overview
Given a directed follows table (follower_id, followee_id), possibly with duplicate rows and self-follows, return each mutual follow pair exactly once as (user_a, user_b) with user_a < user_b, plus each user's count of mutual follows.
Meta
November 29, 20260
0
0 solved
Given a directed follows table (follower_id, followee_id), possibly with duplicate rows and self-follows, return each mutual follow pair exactly once as (user_a, user_b) with user_a < user_b, plus each user's count of mutual follows.
Instagram's graph is directed, and many Meta analyses (close friends, DM likelihood) start from mutual follows. Interviewers like this because a self-merge is the obvious approach, and the details (dedupe, self-loops, emitting each pair once, users with zero mutuals) separate careful candidates.
What the Interviewer Expects
- Clean duplicates and self-follows first
- Self-merge the edge list with swapped columns to find reciprocal edges
- Emit each unordered pair once using a < b
- Count mutuals per user by stacking both sides of each pair
- Include users with zero mutuals if a user table is given
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 scale this to billions of edges in Spark?
- How would you find triangles of mutual follows?
- How would you compute the reciprocity rate per user cohort?
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
Input follows(follower_id, followee_id). Logging can repeat an edge, and a buggy client can create self-follows; both should be removed. Output 1: pairs(user_a, user_b). Output 2: `mutual_counts(u...
Approach
Dedupe edges and drop self-loops. Merge the frame with itself where the left's (follower, followee) equals the right's (followee, follower): an inner merge on two keys with columns swapped. Every mutu...