Track the Largest Contact Cluster as Connections Arrive
Last updated: November 29, 2026
Quick Overview
There are n Google Contacts users and a stream of connection events (a, b). After each event, report the size of the largest group of users linked directly or indirectly by connections.
0
0
0 solved
There are n Google Contacts users and a stream of connection events (a, b). After each event, report the size of the largest group of users linked directly or indirectly by connections.
An incremental connectivity question common in Google technical screens. The interviewer checks that you track component sizes inside the union-find instead of recounting, and that you notice the running maximum can only grow, so no heap is needed.
What the Interviewer Expects
- Maintains a size array alongside the parent array
- Uses union by size so the size lives at the root
- Keeps a running maximum updated only on merges
- Appends an answer for every event, including no-op events
- Avoids recomputing component sizes from scratch
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
- Report the second-largest cluster too. What structure do you need?
- What if connections can also be removed?
- How would you report the number of clusters of each size?
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 and an ordered list of connection pairs. - Output: list with one integer per event, the largest cluster size after that event.
- Initially every user is a cluster of size 1....
High-Level Design
Union by size already stores the size of every component at its root, so merging two roots gives the new size for free. Component sizes only grow under unions, so the global maximum is monotone: after...