Finding connected components in a grid — a classic graph-traversal problem
This is an easy coding problem that tests your ability to explore connected regions in a 2D grid. Microsoft and other tech firms use grid-traversal problems to assess whether candidates can implement depth-first or breadth-first search correctly, handle boundary conditions, and avoid common pitfalls like revisiting cells.
The core task is to identify all distinct regions of one type (land) and measure their size. A clean solution marks visited cells as you explore, uses either recursion or an explicit stack/queue to traverse connected neighbours (up, down, left, right), and tracks the maximum extent seen across all regions. Pay attention to grid dimensions that are not square, and ensure your boundary checks are watertight.
- Depth-first search (DFS) vs. breadth-first search (BFS) trade-offs
- In-place marking of visited cells to avoid re-traversal
- Handling rectangular (non-square) grids and edge wraparound