Report Island Areas in Discovery Order
Updated
Problem statement
Given a rectangular binary matrix grid, return the area of every island.
An island is a maximal group of cells containing 1 connected horizontally or vertically. Its area is its number of cells.
Scan cells in row-major order, from top to bottom and left to right. When an unvisited land cell starts a new island, append that island's area to the result. Return an empty array when the grid contains no land.
Function
islandAreas(grid: int[][]) → int[]Examples
Example 1
grid = [[1, 1, 0, 0], [1, 0, 0, 1], [0, 0, 1, 1]]return = [3, 3]The scan first reaches the three-cell island in the upper-left, then the three-cell island in the lower-right.
Example 2
grid = [[1, 0, 1], [0, 0, 0], [1, 1, 1]]return = [1, 1, 3]The two one-cell islands are discovered before the three-cell island in the bottom row.
Example 3
grid = [[0, 0], [0, 0]]return = []The grid contains no land, so there are no island areas to report.
Constraints
1 <= grid.length <= 3001 <= grid[i].length <= 300- Every row has the same length.
grid[i][j]is either0or1.
More Snap Inc. interview questions
- ID Allocator with Assign and ReleasePHONE SCREEN · Seen Nov 2026
- Basic Calculator IIPHONE SCREEN · Seen Oct 2026
- Stream Cluster Max and MedianPHONE SCREEN · Seen Oct 2026
- LRU Cache with Thread-Safety Follow-UpPHONE SCREEN · Seen Sep 2026
- Smallest Range Covering Elements from K ListsPHONE SCREEN · Seen Sep 2026
- Most Stones Removed with Same Row or ColumnPHONE SCREEN · Seen Sep 2026
- Render an Org Chart and Find Skip-Level PairsONSITE INTERVIEW · Seen Nov 2026
- Peak of an Increasing-Then-Decreasing ArrayONSITE INTERVIEW · Seen Oct 2026
- Number of Closed IslandsONSITE INTERVIEW · Seen Oct 2026
- Radar Barrier CrossingONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
- Pacific Atlantic Water FlowAnduril · PHONE SCREENAnduril interview questions
- Maximum Damage PatchGecko Robotics · OA · PHONE SCREEN
- Maximum Area of IslandSkydio · PHONE SCREENSkydio interview questions
- Number of Islands with Diagonal ConnectivityGoogle · ONSITE INTERVIEWGoogle interview questions
- Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
- Query Whether an Island Size ExistsMeta · ONSITE INTERVIEWMeta interview questions
- Max Area of IslandGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Max Area of IslandRoku · ONSITE INTERVIEWRoku interview questions
- Max Area of IslandBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions