Track the Largest Friend Cluster as Friendships Arrive
Last updated: November 29, 2026
Quick Overview
Friendship edges between n users arrive one at a time. After each edge, report the size of the largest connected cluster of users. Return the list of sizes after every edge.
Meta
November 29, 20260
0
0 solved
Friendship edges between n users arrive one at a time. After each edge, report the size of the largest connected cluster of users. Return the list of sizes after every edge.
A Meta onsite style question on the social graph, where a growth dashboard tracks the giant component as new friendships form. The interviewer checks that you augment union find with component sizes and maintain a running maximum, rather than recomputing component sizes after each edge.
What the Interviewer Expects
- Store size at each root in the DSU
- Update a running maximum on each successful union
- Avoid recomputing all component sizes per edge
- Explain why the maximum never decreases in this setting
- Handle repeated or self edges
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you also report the number of clusters of each size?
- What if edges can be deleted, so the max can shrink?
- How would you report the k largest clusters after each edge?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
nusers andedges, a list of[a, b]in arrival order. - Output: list where entry
iis the largest cluster size after the firsti + 1edges....
High-Level Design
Union by size already tracks component sizes at the root. Since edges only merge clusters, the largest size can only grow, and the only cluster that can become the new max is the one just formed by a ...