Count Clusters After Each Server Decommission in a Planned Order

Last updated: November 29, 2026

Quick Overview

A network of n servers has undirected links. Servers are decommissioned one at a time in a given order, removing them and their links. After each removal, return the number of connected clusters among the remaining servers.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


A network of n servers has undirected links. Servers are decommissioned one at a time in a given order, removing them and their links. After each removal, return the number of connected clusters among the remaining servers.

Union-find cannot delete, and Google onsites like to test whether you know the reverse-time trick. Framed as decommissioning machines in a data center, the interviewer checks that you start from the final state, add servers back in reverse, and record counts in the right order.

What the Interviewer Expects
  • States that union-find supports merges but not splits
  • Reverses the removal order so removals become additions
  • Builds the final state from servers never removed
  • Records the count before re-adding each server
  • Reverses the collected answers at the end
Key Topics to Cover
Union-Find
Offline Processing
Reverse Time
Graph 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
  • What if removals and additions are interleaved and online?
  • Track the size of the largest cluster after each removal.
  • How would you handle link removals instead of server removals?
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, list of links, and removal_order, a list of distinct servers.
  • Removing a server deletes it and all its links.
  • Output: list where entry i is the cluster count right after the i-th r...
High-Level Design

Process time backwards. Start with only the servers that are never removed, activating them one by one and unioning with active neighbors, while keeping a running component count. That count is the an...


Submit Your Answer
Markdown supported