Implement island counting with BFS and DFS
Company: Bloomberg
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: A Bloomberg Software Engineer onsite coding question: count the number of islands (connected land regions) in an m x n grid under four-directional connectivity. It asks for a linear-time O(m·n) flood-fill plus a second, explicit breadth-first-search implementation, with correctness and complexity analysis, edge-case handling, tests, and a diagonal-adjacency follow-up.
Part 1: Count Islands in a Grid Using Any O(m*n) Approach
Constraints
- grid may be None.
- 0 <= m <= 1000
- 0 <= n <= 1000
- If m > 0 and n > 0, grid is rectangular.
- Each cell is either '0' or '1'.
- The intended time complexity is O(m*n).
Examples
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: The top-left block, the middle cell, and the bottom-right pair are three separate 4-directional islands.
Input: ([],)
Expected Output: 0
Explanation: An empty grid contains no land.
Input: ([['1','0','1'],['0','1','0'],['1','0','1']],)
Expected Output: 5
Explanation: All five land cells touch only diagonally, so each is its own island.
Input: ([['1','1','1'],['1','1','1']],)
Expected Output: 1
Explanation: All land cells are connected into one island.
Input: ([['0','0'],['0','0']],)
Expected Output: 0
Explanation: An all-water grid has no islands.
Hints
- Scan every cell. When you find unvisited land, you have discovered a new island.
- After discovering an island, traverse from that cell to mark every land cell in the same connected component.
Part 2: Count Islands Using Explicit Iterative BFS
Constraints
- grid may be None.
- 0 <= m <= 1000
- 0 <= n <= 1000
- If m > 0 and n > 0, grid is rectangular.
- Each cell is either '0' or '1'.
- Use an iterative queue-based BFS, not recursion.
Examples
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: There are three 4-directionally connected land components.
Input: ([[]],)
Expected Output: 0
Explanation: A grid with zero columns contains no cells and therefore no islands.
Input: ([['1']],)
Expected Output: 1
Explanation: A single land cell forms one island.
Input: ([['1','0'],['0','1']],)
Expected Output: 2
Explanation: The two land cells touch diagonally only, so they are separate islands.
Input: ([['1','1','0','1'],['0','1','0','1'],['0','0','1','0'],['1','0','0','1']],)
Expected Output: 5
Explanation: The grid contains five separate 4-directional connected components of land.
Hints
- A queue processes cells layer by layer, but for counting islands, the important part is that it eventually visits the whole connected component.
- Mark a land cell as visited when it is enqueued, not when it is dequeued, to avoid adding it multiple times.
Part 3: Count Islands with 8-Directional Diagonal Adjacency
Constraints
- grid may be None.
- 0 <= m <= 1000
- 0 <= n <= 1000
- If m > 0 and n > 0, grid is rectangular.
- Each cell is either '0' or '1'.
- The intended time complexity is O(m*n).
Examples
Input: ([['1','1','0','0','0'],['1','1','0','0','0'],['0','0','1','0','0'],['0','0','0','1','1']],)
Expected Output: 1
Explanation: With diagonal adjacency, the top-left block connects diagonally to the middle cell, which connects diagonally to the bottom-right land.
Input: (None,)
Expected Output: 0
Explanation: A null grid contains no islands.
Input: ([['1','0','1'],['0','1','0'],['1','0','1']],)
Expected Output: 1
Explanation: Every corner land cell is diagonally connected to the center land cell.
Input: ([['0']],)
Expected Output: 0
Explanation: A single water cell does not form an island.
Input: ([['1','0','0','0'],['0','1','0','0'],['0','0','0','1'],['0','0','0','1']],)
Expected Output: 2
Explanation: The first two land cells form one diagonal island, and the two rightmost land cells form another island.
Hints
- The overall connected-component strategy is the same as 4-directional island counting.
- The key change is the neighbor list: include all eight offsets around a cell.