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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Easy

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
Union-Find
Union by Size
Online Queries
Streaming
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
  • 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 Problems
Sample Answer
Requirements
  • Input: n users 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...


Submit Your Answer
Markdown supported