FastPrepMinimize Malware Spread in a Facility Network
Problems/Minimize Malware Spread in a Facility Network

Minimize Malware Spread in a Facility Network

Amazon logoAmazon● HardFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Amazon SDE2 interview list as a facility-network version of Minimize Malware Spread. The brief report does not say whether removing a center physically deletes it from the network. This exercise follows the standard named variant: the chosen center remains in the graph but is removed only from the initially compromised set before spread begins. Problem match confidence: approximately 98%.

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.

More Amazon problems

  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
See Amazon hiring insights
Saved
class Solution {
    public int minMalwareSpread(int[][] graph, int[] initial) {
        // write your code here
    }
}
Checking account…