Surrounded Regions

Medium · Graphs · about 20 minutes

Asked at Google, Amazon, Meta, Microsoft, Bloomberg, Uber.

Given an m × n board of 'X' and 'O', capture every region of 'O' cells (connected 4-directionally) that is completely surrounded by 'X': flip those 'O's to 'X'. A region touching the border is not surrounded. The board is given as strings, one per row; return the resulting board in the same form.

Related guides