Minimize Malware Spread in a Facility Network
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
This Amazon OA, reported in September 2026, looks like a graph infection puzzle, but it really reduces to counting components and checking how many infected nodes sit in each one. You get an adjacency matrix, a list of compromised centers, and you remove exactly one. The trick is that most removals change nothing. Only components with exactly one initial infection can be saved. If you blank on the setup during the live assessment, StealthCoder runs invisibly as a safety net and gives you the approach while you type.
The problem
An undirected facility network is represented by an adjacency matrix graph. Every center in initial is compromised, and compromise spreads through connected centers. Remove exactly one center from the initial compromised set before spread begins. Return the center whose removal minimizes the final number of compromised centers. If several choices are equally good, return the smallest center ID. Function minMalwareSpread(graph: int[][], initial: int[]) → int Examples Example 1 graph = [[1,1,0],[1,1,0],[0,0,1]] initial = [0,1] return = 0 Removing either 0 or 1 leaves the same connected component infected, so the smaller ID wins. Example 2 graph = [[1,0,0],[0,1,0],[0,0,1]] initial = [0,2] return = 0 Either removal saves one isolated center, so return 0. Example 3 graph = [[1,1,1],[1,1,1],[1,1,1]] initial = [1,2] return = 1 One infection remains in the shared component, making the smaller candidate the tie-break winner. Constraints 1 ≤ graph.length ≤ 300 and graph is square. graph[i][j] is 0 or 1, symmetric, and graph[i][i] = 1. initial is non-empty and contains distinct valid center IDs.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Find connected components with BFS, DFS, or union-find on the matrix. For each component, record its size and count how many initial nodes live in it. If a component has exactly one initial node, removing that node saves the whole component, so the gain equals the component size. If it has two or more, removal saves nothing, because the others still infect it. Pick the initial node with the largest gain. Break ties by smallest ID. If no node saves anything, return the smallest ID in initial. The common pitfall is forgetting that fallback, as in Example 1 and Example 3. Another is not sorting initial or ignoring the tie-break. Matrix traversal is O(n^2) with n up to 300, so it's fast. If the component logic slips under pressure, StealthCoder is your hedge during the live OA.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Minimize Malware Spread in a Facility Network cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as minimize malware spread. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimize Malware Spread in a Facility Network FAQ
What's the trick in the Amazon minimize malware spread problem?+
Only components with exactly one initially infected node can be saved. Removing that node saves the entire component. If a component has two or more infected nodes, removal gains nothing. Compute component sizes, count initial nodes per component, and pick the largest savable size.
How do I handle ties and the no-gain case?+
If several nodes save the same amount, return the smallest ID. If no removal saves anything, every choice is equal, so return the minimum ID in initial. Examples 1 and 3 test exactly this. Sort initial first and the logic gets simpler.
Should I use BFS, DFS, or union-find?+
Any of the three works. With n up to 300 and an adjacency matrix, a simple BFS labeling each node's component is easiest to write correctly. Union-find is fine too and gives sizes directly. Pick whichever you can write without bugs in a few minutes.
What's the time complexity I should aim for?+
O(n^2) is natural, since you scan the adjacency matrix once to label components. With n at most 300, that's about 90,000 cells, trivial. You don't need anything fancier than component labeling plus a counting pass over initial.
How do I prepare for this in 48 hours?+
Practice component labeling on a matrix graph, then the counting step: infected nodes per component. Write it once from scratch and test your three examples by hand. Pay attention to the fallback return. That's the part people forget under time pressure.