Water Cells Adjacent to Land
Updated
Problem statement
You are given a non-empty rectangular grid called grid. Each cell is either land (1) or water (0).
Find every water cell that touches a land cell. Cells can touch along a side or at a corner. All eight neighboring directions count. Include land from every island, and only consider cells inside the grid.
Return the water cells as an array of [row, column] pairs:
- Row and column numbers start at
0. - Include each water cell only once, even if it touches several land cells.
- Sort by row from smallest to largest. Within the same row, sort by column from smallest to largest.
Return an empty array if no water cells touch land. Do not change grid.
Function
waterCellsAdjacentToLand(grid: int[][]) → int[][]Examples
Example 1
grid = [[1,0,0],[0,0,1],[0,0,0]]return = [[0,1],[0,2],[1,0],[1,1],[2,1],[2,2]]Six water cells touch land. Cell [2, 0] does not. Cells [0, 2] and [2, 2] touch the land at [1, 2].
Example 2
grid = [[1,0],[0,0]]return = [[0,1],[1,0],[1,1]]Cell [1, 1] qualifies through diagonal contact. Every coordinate appears once.
Example 3
grid = [[0,0,0]]return = []The grid has no land, so no water cell qualifies.
Constraints
1 <= grid.length <= 501 <= grid[0].length <= 50- Every row has the same length.
- Every cell is
0or1.
More Google Onsite Interview questions
- 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
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Filesystem Entity Total SizeONSITE INTERVIEW · Seen Nov 2026
- Longest Subarray Within an Absolute-Difference LimitONSITE INTERVIEW · Seen Nov 2026
- Reach a Destination Through Scheduled FlightsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Rotate a Matrix by 90 DegreesIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
- Matrix MultiplicationeBay · ONSITE INTERVIEWeBay interview questions
- Validate a Two-Color ChessboardAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Traverse Interior Matrix Cells in Row-Major OrderCitadel · ONSITE INTERVIEWCitadel interview questions
- Detect a Winner on a 3x3 Tic-Tac-Toe BoardHebbia · ONSITE INTERVIEW
- Trace of a Square MatrixAkuna Capital · PHONE SCREEN · ONSITE INTERVIEWAkuna Capital interview questions
- Unique Rows of a Binary MatrixExpedia · ONSITE INTERVIEWExpedia interview questions
- Iterate Ragged Integer Lists VerticallyZipRecruiter · ONSITE INTERVIEWZipRecruiter interview questions
- Rotting OrangesSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEWByteDance interview questions