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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

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
union find
union by size
running maximum
incremental connectivity
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: n users and edges, a list of [a, b] in arrival order.
  • Output: list where entry i is the largest cluster size after the first i + 1 edges....
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 ...


Submit Your Answer
Markdown supported