Quarantine the Region Threatening the Most Cells

Quick Overview

Quarantine the Region Threatening the Most Cells. A binary isInfected grid has infected cells marked 1 and susceptible cells marked 0.

Quarantine the Region Threatening the Most Cells

Company: Flipkart

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem A binary isInfected grid has infected cells marked 1 and susceptible cells marked 0. Infection spreads nightly to side-adjacent susceptible cells unless a wall blocks their shared boundary. Each day quarantine the one connected infected region threatening the largest number of distinct susceptible cells; that choice has no tie. Install all boundary walls needed to contain it, then let every other active region spread one step simultaneously. Return the total walls built until no further spread is possible, including when the world becomes fully infected. ```cpp class Solution { public: int containVirus(vector<vector<int>>& isInfected); }; ``` ### Examples **Example 1:** ```text isInfected = [[1,0,0]] Output: 1 Explanation: One boundary wall contains the only region. ``` **Example 2:** ```text isInfected = [[1,1],[1,0]] Output: 2 Explanation: The susceptible corner touches the region on two distinct sides, requiring two walls. ``` ### Constraints - The grid is rectangular, with both dimensions in [1,50]. - Initial cells contain only 0 or 1. - Whenever a spreading-region choice is needed, one region threatens strictly more susceptible cells than every other region. ### Contract considerations Follow the source's unique-largest-region policy; this is simulation, not a search over alternative quarantine strategies. ```hint Read the boundary condition Why can one threatened cell require more than one wall? ``` ### Boundary reminders Follow the source's unique-largest-region policy; this is simulation, not a search over alternative quarantine strategies.

Overview: Quarantine the Region Threatening the Most Cells. A binary isInfected grid has infected cells marked 1 and susceptible cells marked 0.

Flipkart logo
Flipkart
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

A binary isInfected grid has infected cells marked 1 and susceptible cells marked 0. Infection spreads nightly to side-adjacent susceptible cells unless a wall blocks their shared boundary. Each day quarantine the one connected infected region threatening the largest number of distinct susceptible cells; that choice has no tie. Install all boundary walls needed to contain it, then let every other active region spread one step simultaneously. Return the total walls built until no further spread is possible, including when the world becomes fully infected.

class Solution {
public:
    int containVirus(vector<vector<int>>& isInfected);
};

Examples

Example 1:

isInfected = [[1,0,0]]
Output: 1
Explanation: One boundary wall contains the only region.

Example 2:

isInfected = [[1,1],[1,0]]
Output: 2
Explanation: The susceptible corner touches the region on two distinct sides, requiring two walls.

Constraints

  • The grid is rectangular, with both dimensions in [1,50].
  • Initial cells contain only 0 or 1.
  • Whenever a spreading-region choice is needed, one region threatens strictly more susceptible cells than every other region.

Contract considerations

Follow the source's unique-largest-region policy; this is simulation, not a search over alternative quarantine strategies.

Boundary reminders

Follow the source's unique-largest-region policy; this is simulation, not a search over alternative quarantine strategies.