Surrounded Regions
Problem Statement
You are given a board of 'X' and 'O' letters. A region is a group of 'O' cells joined up, down, left or right. A region is surrounded when none of its 'O' cells is on the edge of the board. Turn every surrounded 'O' into 'X', in place. Regions that touch the edge stay as they are.
board, an m × n grid of 'X' and 'O' characters. You change it in place.
Nothing is returned. The board itself is changed.
The three middle 'O's are joined to each other and never touch the edge, so they are captured. The 'O' in the bottom row is on the edge, so it stays.
There is no 'O', so nothing changes.
- • 1 ≤ m, n ≤ 200, where m is the number of rows and n is the number of columns
- • board[i][j] is 'X' or 'O'
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each 'O', search its region; if no cell of it is on the edge, flip the whole region
Mark every 'O' connected to the edge with '#', flip the other 'O's to 'X', then turn '#' back into 'O'
For each 'O' that is still 'O', search its whole region with a fresh visited array. If none of its cells is on the edge, flip the whole region to 'X'. A region that touches the edge is searched again from each of its cells, so the same answer is found many times.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Graph BFS / DFS translate into production architectures and low-level memory mechanics.