Pick One Infected Machine to Clean to Minimize Malware Spread

Last updated: November 29, 2026

Quick Overview

A network is given as an n by n adjacency matrix and a list of initially infected machines. Malware spreads to every machine in an infected machine's component. You may clean exactly one initially infected machine before spread. Return the one whose cleaning minimizes final infections, breaking ties by smallest index.

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

0

0

0 solved


A network is given as an n by n adjacency matrix and a list of initially infected machines. Malware spreads to every machine in an infected machine's component. You may clean exactly one initially infected machine before spread. Return the one whose cleaning minimizes final infections, breaking ties by smallest index.

Fleet security matters across Google's data centers, and this onsite question checks whether you reason at the component level instead of simulating the spread for every choice. The key insight is that cleaning one machine only helps if it is the sole infection in its component.

What the Interviewer Expects
  • Computes components and their sizes with union-find
  • Counts initial infections per component
  • Credits a machine with its component size only if it is the lone infection
  • Returns the smallest index on ties, including when no choice saves anything
  • Avoids re-simulating spread per candidate
Key Topics to Cover
Union-Find
Connected Components
Counting
Greedy Choice
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
  • Now cleaning removes the machine and its links entirely. How does the solution change?
  • Clean two machines instead of one.
  • What if spread happens over time and you can clean at any step?
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: symmetric 0/1 matrix graph with graph[i][i] = 1, and a list of infected indices.
  • A cleaned machine stays in the network and can be reinfected by others in its component.
  • Output: the i...
High-Level Design

Since cleaned machines remain connected, if a component has two or more initial infections, cleaning one changes nothing: the other still infects everyone. If a component has exactly one infection, cl...


Submit Your Answer
Markdown supported