Largest Island After One Land Conversion
Updated
Problem statement
You are given an n x n binary matrix grid. You may change at most one 0 into 1.
An island is a group of 1 cells connected by moving up, down, left, or right through other 1 cells. Return the largest island area obtainable after the optional conversion.
Function
largestIsland(grid: int[][]) → intExamples
Example 1
grid = [[1,0],[0,1]]return = 3Changing either zero joins the two diagonal land cells through the converted cell.
Example 2
grid = [[1,1],[1,0]]return = 4Changing the only zero makes the whole grid one island.
Example 3
grid = [[1,1],[1,1]]return = 4The grid is already one island, so no conversion is needed.
Constraints
1 <= n <= 500.n == grid.length == grid[i].length.grid[i][j]is0or1.- Four-directional adjacency defines an island.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Making a Large IslandMeta · ONSITE INTERVIEWMeta interview questions
- Connected GroupsFivetran · ONSITE INTERVIEWFivetran interview questions
- Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
- Connected GroupsVisa · OAVisa interview questions
- Count Islands in a GridDoorDash · PHONE SCREENDoorDash interview questions
- Count Islands from Streaming RowsAmazon · ONSITE INTERVIEW
- Longest Decreasing Matrix Path with Limited RelaxationsDuolingo · ONSITE INTERVIEWDuolingo interview questions
- Largest Island After One Land ConversionBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Number of IslandsFleetx · ONSITE INTERVIEW
- Number of IslandsIntuit · ONSITE INTERVIEWIntuit interview questions