Surrounded Regions
A medium Graph interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Graph
- Sheets
- 2
- Core for
- 12 roles
The problem
Given an m x n matrix board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that region, except for 'O's on the border or connected to the border.
Example 1
- Input
- board = [["X","X","X","X"],["X","O","O","X"],["X","O","O","X"],["X","X","X","X"]]
- Output
- [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","X","X","X"]]
- Why
- The 2x2 block of O's in the center is fully surrounded by X's and becomes X. No O's touch the border.
Example 2
- Input
- board = [["X"]]
- Output
- [["X"]]
- Why
- Single cell, no O to flip.
Constraints
- m == board.length
- n == board[i].length
- 1 <= m, n <= 200
- board[i][j] is 'X' or 'O'
How to think about it
Updated 2026-09-29Surrounded means having no escape path to the grid boundary. Instead of identifying which internal regions are surrounded, identify which 'O's are safe: any 'O' connected to the boundary can never be captured. Mark all boundary-connected 'O's with a temporary character, then flip everything left over.
Approaches, worst first
Per-component boundary check
time O(m * n) · space O(m * n)
Find each connected cluster of 'O's, explore its cells while tracking whether any touch the boundary, and re-traverse the cluster to flip to 'X' only if no boundary was reached.
Boundary flood with three-pass sweepWrite this one
time O(m * n) · space O(m * n)
Traverse all four perimeter edges. Whenever an 'O' is encountered, launch a flood fill changing reachable 'O's to '#'. Then scan the full board: change remaining 'O' to 'X', and revert '#' back to 'O'.
Where people lose marks · 3
- Small boards where m < 3 or n < 3 consist entirely of border cells; no 'O' can ever be captured on such boards.
- Forgetting to check all four boundaries, especially right and bottom borders when m != n.
- Deep recursion in DFS on long serpent-like chains of 'O' can trigger recursion depth errors on a 200x200 grid.
The theory behind it
Graph — the ground this problem stands on. All Graph problems
What Graph is
A graph is a network of individual points, called vertices or nodes, connected by lines called edges. Think of a subway transit map, an electrical circuit, or a web of social friends. Unlike a tree, a graph has no designated top node and no parent-child hierarchy. Connections can run one-way or both ways, and paths can loop back on themselves to form closed cycles.
When to reach for it
Reach for graph algorithms when inputs describe relationships, networks, flights between cities, course prerequisites, or clone networks. Signals include finding the shortest route across unweighted connections, ordering tasks that depend on earlier tasks, counting isolated clusters, or checking whether a path contains an infinite loop. Whenever problems present pairs of related entities and ask for reachability, distances, or dependencies, graph representations apply.
How the pattern works
First convert edge lists into an adjacency list, mapping each node to an array of its neighbors. Choose your exploration strategy based on the goal: use a queue and breadth-first search to find the shortest path in unweighted networks, or use recursion and depth-first search to explore full paths and detect cycles. Because graphs can have loops, always track visited nodes in a set or boolean array. Add nodes to the visited set at the moment they enter the queue so they are never visited twice.
More Graph problems
Target Roles
Core requirement for 12 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .