The Contain Virus interview question is a complex simulation problem. You are given a 2D grid where 1 represents an infected cell, 0 represents an uninfected cell, and 2 represents a firewall. Every day, the virus spreads to all adjacent uninfected cells. However, you can build a firewall around exactly one infected region per day. You must choose the region that, if left alone, would infect the most uninfected cells the next day. The goal is to return the total number of firewalls built.
This is a HARD difficulty problem often asked by Google. The Contain Virus coding problem tests a candidate's ability to coordinate multiple complex algorithms: BFS/DFS for region detection, Simulation for the daily spread, and Greedy logic for choosing which region to quarantine. It’s a test of high-level coding organization and state management.
This problem utilizes the Array, Matrix, Breadth-First Search, Depth-First Search, Simulation interview pattern.
Imagine a grid with two clusters of virus.
Break large simulation problems into helper functions like getRegions(), buildWall(), and spreadVirus(). This keeps your logic clean and makes it easier to debug during the interview.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Check Knight Tour Configuration | Medium | Solve | |
| Find All Groups of Farmland | Medium | Solve | |
| Shortest Bridge | Medium | Solve | |
| Coloring A Border | Medium | Solve | |
| Pacific Atlantic Water Flow | Medium | Solve |