Count Islands and Return the Largest Island Size
Company: Oracle
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: Count four-connected islands and find the largest island using DFS or BFS, with correct visited-state handling and all-water behavior.
Read the full Oracle Software Engineer interview experience this question came from
Constraints
- The rectangular binary grid has 1 through 500 rows and 1 through 500 columns.
- Only horizontal and vertical adjacency connects land.
- Return [island_count, largest_size], or [0,0] when there is no land.
Examples
Input: ([[1, 1, 0], [0, 1, 0], [1, 0, 1]],)
Expected Output: [3, 3]
Explanation: The upper component has three cells; the two bottom corner land cells are isolated.
Input: ([[0, 0], [0, 0]],)
Expected Output: [0, 0]
Explanation: No land starts a traversal.
Hints
- An unvisited land cell begins a new island; count the cells reached by that traversal to obtain its size.