Back to Practice
#0298

Surrounded Regions

MediumDSA28 min20 XP

Problem

Given a board of "X" and "O", return a new board where every O not connected to the border is changed to X.

Why This Matters

This grid problem teaches the reverse perspective: mark safe regions from the border, then flip the rest.

Function Signature

def solve_surrounded(board):

Examples

Example 1
Inputboard = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
Output[["X", "X", "X", "X"], ["X", "X", "X", "X"], ["X", "X", "X", "X"], ["X", "O", "X", "X"]]

The bottom O touches the border, so it survives. The inner O region is captured.

Constraints

  • Return the exact requested value.
  • Handle edge cases cleanly.
  • Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Capture middle
Inputclassic board
Expectedonly border-connected O survives

Border-connected O cells are marked safe.

All border safe
Input[["O","O"],["O","O"]]
Expectedunchanged

Every O touches the border.

Hidden Test Categories
empty boardsingle rowno O cells