PPractick

DSA interview question

Surrounded Regions

Medium · Data structures · Topics: graph, dfs, bfs, matrix

The problem

In a Go-like board game, a region of 'O' cells is captured (flipped to 'X') if it's completely surrounded by 'X', meaning it can't reach the border of the board through 'O' cells. Checking each region's escape route separately is wasteful. Flip the logic: every 'O' region that touches the border is safe, so flood-fill from all border 'O's and mark them. Every 'O' left unmarked is captured.

Example: XXXX / XOOX / XXOX / XOXX → XXXX / XXXX / XXXX / XOXX.

Function signature

void solve(vector<string>& board);  // in place

Test cases

Input (rows)Output (rows)Explanation
XXXX XOOX XXOX XOXXXXXX XXXX XXXX XOXXThe bottom O touches the border
XXX XOX XXXXXX XXX XXXA single enclosed O
OOO OXO OOOOOO OXO OOOEverything touches the border
XX
OOA border cell is safe

(Confirm: connectivity is 4-directional; O cells on the border, or connected to one, are never captured.)

How interviewers phrase it

Clarifying questions to ask

  1. 4-directional? (Yes.)
  2. Are border O's ever captured? (No.)
  3. In place? (Yes.)

Follow-ups to expect

  1. "Why start from the border?"
  2. "How do you mark without extra memory?"
  3. "DSU alternative?"
  4. "Why BFS instead of recursive DFS?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in