Trace Every Water Drop to Its Resting Destination
Updated
Problem statement
You are given an m x n integer matrix heights. Place one drop of water on every cell. A drop repeatedly considers the four orthogonally adjacent positions and moves to the position with the lowest height, but only when that height is strictly lower than its current cell.
A position immediately outside the grid has height 0. If a drop moves to such a position, it has left the grid and stops there. When several lowest positions have the same height, choose the one with the lexicographically smallest coordinate: compare the row first, then the column.
Return an array containing one [row, column] destination for every source cell in row-major order. A destination inside the grid is the cell where the drop can no longer descend. An outside destination is the one-step-outside coordinate where the drop leaves the grid, so its row may be -1 or m, or its column may be -1 or n.
Function
waterDestinations(heights: int[][]) → int[][]Examples
Example 1
heights = [[5,4,5],[4,-1,4],[5,4,5]]return = [[-1,0],[1,1],[-1,2],[1,1],[1,1],[1,1],[2,-1],[1,1],[2,3]]The four edge-center cells descend into the basin at [1,1], as does the basin itself. Each positive corner sees two equally low outside positions of height 0 and uses the lexicographically smaller coordinate.
Example 2
heights = [[5]]return = [[-1,0]]All four adjacent positions are outside with height 0. Coordinate [-1,0] is lexicographically smallest.
Example 3
heights = [[0,0],[0,0]]return = [[0,0],[0,1],[1,0],[1,1]]Every adjacent position, including each outside position, has height 0. Because a move must be strictly downhill, every drop remains on its source cell.
Constraints
1 <= m, n.m * n <= 100000.-10^9 <= heights[row][column] <= 10^9.- Every move is to one of the four orthogonally adjacent positions.
More Google Onsite Interview questions
- 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
- Route Pattern MatcherPHONE SCREEN · ONSITE 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
- Robot Survival ProbabilityInMobi · ONSITE INTERVIEWInMobi interview questions
- Longest Border-Ending Diagonal PatternZipRecruiter · OAZipRecruiter interview questions
- Maximal SquareAmazon · ONSITE INTERVIEW
- Knight Probability in ChessboardGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Explore an Unknown Grid and Find a PathLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Spiral MatrixNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Tic-Tac-Toe Move WinnersByteDance · ONSITE INTERVIEWByteDance interview questions
- Stochastic Gradient Descent with MomentumMeta · ONSITE INTERVIEWMeta interview questions
- Cyclic Fuel Command SimulationWaymo · ONSITE INTERVIEWWaymo interview questions