Making a Large Island
Updated
Problem statement
You are given an n by n binary grid. A value of 1 is land and 0 is water. An island is a group of land cells connected horizontally or vertically; diagonal contact does not connect cells.
You may change at most one water cell from 0 to 1. Return the largest island area that can exist after this change, or after making no change. An island's area is its number of cells.
Function
largestIsland(grid: int[][]) → intExamples
Example 1
grid = [[1,0],[0,1]]return = 3Turning either water cell into land joins the two original islands and the new cell, for area 3.
Example 2
grid = [[1,1],[1,0]]return = 4Flip the remaining water cell to create area 4.
Example 3
grid = [[1,1],[1,1]]return = 4There is no water to flip; the existing island already has area 4.
Example 4
grid = [[0,0,0],[0,0,0],[0,0,0]]return = 1A single flipped cell creates an island of area 1.
Constraints
1 <= n <= 500grid.length == nand each row has lengthn.- Each cell is either
0or1.
More Meta Onsite Interview questions
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
- Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
- Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
- Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Mutual-Friend RecommendationsONSITE INTERVIEW · Seen Sep 2026
- Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
- Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026
- Maximum Stock Profit with Transaction LimitsONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Largest Island After One Land ConversionGoogle · ONSITE INTERVIEWGoogle 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
- Evaluate DivisionPlaud · ONSITE INTERVIEW
- Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions