Question Bank

Grid Search Patterns

Difficulty: Medium

Implement and trace classic grid problems: counting islands, flood fill, and multi-source distance maps. Code stems are mostly Python.

Question Bank
/

Grid Search Patterns

Grid Search Patterns

Implement and trace classic grid problems: counting islands, flood fill, and multi-source distance maps. Code stems are mostly Python.

Question Bank
Medium
Python
5 questions
islands
bfs
algorithms
quiz

900 views

24

Implement numIslands(grid) where grid[r][c] is "1" for land and "0" for water. Two cells are connected if they share an edge (4-directional).

Examples

Example 1:

Input: grid = [['1', '1', '0'], ['1', '0', '0'], ['0', '0', '1']]
Output: 2
Explanation: Scan every cell; on each unseen '1', launch DFS / BFS that sinks the whole component by flipping land to water. Two distinct components: top-left L-shape and bottom-right singleton. O(R * C) time.