Compute islands in a binary grid
Company: Snapchat
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: This question evaluates a candidate's competency in modeling grids as graphs, detecting connected components, applying graph traversal algorithms (DFS/BFS) and disjoint-set (Union-Find) data structures, and analyzing algorithmic time and space complexity.
Number of Islands
Constraints
- 1 <= m, n <= 300
- grid[i][j] is '0' or '1'
- The grid may be empty (treat as 0 islands)
Examples
Input: ([['1','1','1','1','0'],['1','1','0','1','0'],['1','1','0','0','0'],['0','0','0','0','0']],)
Expected Output: 1
Explanation: All land cells are connected 4-directionally into a single island.
Input: ([['1','1','0','0','0'],['1','1','0','0','0'],['0','0','1','0','0'],['0','0','0','1','1']],)
Expected Output: 3
Explanation: A 2x2 block, a single cell, and a horizontal pair form three separate islands.
Input: ([['0','0','0'],['0','0','0']],)
Expected Output: 0
Explanation: All water means no islands.
Input: ([['1']],)
Expected Output: 1
Explanation: A single land cell is one island.
Input: ([],)
Expected Output: 0
Explanation: Empty grid has no islands.
Input: ([['1','0','1','0','1']],)
Expected Output: 3
Explanation: Three isolated land cells separated by water are three islands.
Hints
- Scan the grid cell by cell. The first time you hit an unvisited '1', you've discovered a new island.
- From that cell, flood-fill (DFS or BFS) to all 4-directionally connected '1' cells and mark them visited so the same island is never re-counted.
- Three standard approaches: DFS (recursive or stack), BFS (queue), or Union-Find. All are effectively O(m·n); Union-Find shines when edges/cells stream in dynamically.
Island Sizes in Descending Order
Constraints
- 1 <= m, n <= 300
- grid[i][j] is '0' or '1'
- The grid may be empty (return an empty list)
- Return sizes sorted in non-increasing order
Examples
Input: ([['1','1','1','1','0'],['1','1','0','1','0'],['1','1','0','0','0'],['0','0','0','0','0']],)
Expected Output: [9]
Explanation: One island made of 9 connected land cells.
Input: ([['1','1','0','0','0'],['1','1','0','0','0'],['0','0','1','0','0'],['0','0','0','1','1']],)
Expected Output: [4, 2, 1]
Explanation: Sizes 4 (2x2 block), 2 (horizontal pair), and 1 (lone cell), sorted descending.
Input: ([['0','0','0'],['0','0','0']],)
Expected Output: []
Explanation: No land means no islands and an empty list.
Input: ([['1']],)
Expected Output: [1]
Explanation: A single land cell forms one island of size 1.
Input: ([],)
Expected Output: []
Explanation: Empty grid yields an empty list.
Input: ([['1','0','1','0','1']],)
Expected Output: [1, 1, 1]
Explanation: Three isolated cells, each an island of size 1.
Hints
- This is the count-islands flood-fill, but track how many cells each component contains instead of just whether it exists.
- Each time you discover a new unvisited '1', run a DFS/BFS and count every land cell you mark; append that count to a sizes list.
- After collecting all sizes, sort the list in descending order before returning it.