FastPrepTrace Every Water Drop to Its Resting Destination
Problems/Trace Every Water Drop to Its Resting Destination

Trace Every Water Drop to Its Resting Destination

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives a two-dimensional water simulation of steepest descent, height zero outside the grid, and a request for the final location of every drop. It does not say which neighbors count, how to break ties between equal heights, how to write the output, the input limits, or the function signature. We made those clear while keeping the reported task.

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]]
heights
012054514-142545
→
return
010-101112-1231141151162-1711823

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]]
heights
005
→
return
010-10

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]]
heights
01000100
→
return
01000101210311

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

Interview reports that mention this question

  • Google Intern Grid and Tree Rounds: 7 Experiences (Part 2 of 8)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int[][] waterDestinations(int[][] heights) {
      // Write your code here.
    }
}
Checking account…