Count Distinct Members After Merging Duplicate Facebook Groups
Last updated: November 29, 2026
Quick Overview
When duplicate Facebook Groups are merged, a mapping table points old group ids to the surviving group id. Report member counts per surviving group, counting each person once even if they belonged to several of the merged groups.
Meta
November 29, 20260
0
0 solved
When duplicate Facebook Groups are merged, a mapping table points old group ids to the surviving group id. Report member counts per surviving group, counting each person once even if they belonged to several of the merged groups.
Groups is a big Meta surface and admins regularly merge duplicate communities. The interviewer wants you to remap ids with a LEFT JOIN and COALESCE, then deduplicate people across the merged groups.
What the Interviewer Expects
- Remaps group ids with LEFT JOIN to the merge table and COALESCE
- Counts COUNT(DISTINCT user_id) after remapping, not before
- Handles groups that were never merged
- Considers chained merges (A into B, B into C)
- Excludes members who left
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 resolve chained merges of arbitrary depth?
- What if the merge table has two targets for one old group?
- How would you show how much the dedupe reduced counts?
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
group_members(group_id, user_id, joined_at, left_at) and group_merges(old_group_id, new_group_id, merged_at). Members with non-null left_at are not current. I will first assume a single level of...
Approach
Replace each membership's group id with the surviving id: LEFT JOIN on old_group_id and COALESCE(m.new_group_id, gm.group_id). Then count distinct users per surviving id. Summing per-old-group cou...