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
Data Manipulation (SQL/Python)
Data Scientist
Meta
November 29, 2026
Data Scientist
Technical Screen
Data Manipulation (SQL/Python)
Medium

0

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
self-merge
graph edges
deduplication
unordered pairs
value_counts
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported