Max Area of Island
Updated
Problem statement
You are given a binary matrix grid. Return the area of the largest island.
Cells with 1 are land. An island is a group of land cells joined up, down, left, or right, and it takes in every land cell it can reach that way. Its area is the number of cells in it. Return 0 when the matrix has no land.
Function
maxIslandArea(grid: int[][]) → intExamples
Example 1
grid = [[0,0,1,0],[1,1,1,0],[0,1,0,1]]return = 5The land in the middle row, plus the cell above its right end and the cell below its middle, forms one island of five cells. The land cell in the bottom-right corner touches no other land, so it is a separate island. The answer is the larger area, five.
Example 2
grid = [[0,0],[0,0]]return = 0There is no land anywhere in the grid, so there is no island and the answer is zero.
Example 3
grid = [[1,0,1],[1,0,1],[1,1,1]]return = 7The left column and the right column are joined by the bottom row. All seven land cells form one island, so the answer is seven.
Constraints
1 <= grid.length <= 500.1 <= grid[i].length <= 500, and every row has equal length.- Every cell is either
0or1.
More Amazon Onsite Interview questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Optimal Account BalancingONSITE INTERVIEW · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Number of Islands with Diagonal ConnectivityGoogle · ONSITE INTERVIEWGoogle interview questions
- Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
- Mouse and Cheese ReachabilityMeta · ONSITE INTERVIEWMeta interview questions
- Number of IslandsTripjack · ONSITE INTERVIEWTripjack interview questions
- Number of IslandsTekion · ONSITE INTERVIEWTekion interview questions
- Grid Pathfinding with Obstacles (DFS)Roblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Number of IslandsGoldman Sachs · OA · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Number of IslandsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Number of IslandsCrowdstrike · ONSITE INTERVIEWCrowdstrike interview questions
- Max Area of IslandRoku · ONSITE INTERVIEWRoku interview questions
Amazon interview experiences
- Amazon Experienced SDE Onsite Questions: 1 Experience
- Amazon SDE Onsite Interview Questions: 9 Experiences
- Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
- Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
- Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
- Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)