Highest Cells That Can Flow to Every Target
Updated
Problem statement
An integer grid gives the elevation of every cell. Water may move from a cell to a cell directly above, below, left, or right of it whose elevation is less than or equal to the current elevation.
Given one or more target coordinates, find every cell from which water can reach every target. Of those cells, return only the ones with the highest elevation.
Return their coordinates sorted by row and then column. Return an empty array when no cell can reach every target. Duplicate target coordinates have no additional effect.
Function
highestCommonFlowCells(heights: int[][], targets: int[][]) → int[][]Examples
Example 1
heights = [[5,4,3],[6,1,2],[7,6,1]]targets = [[0,2],[2,2]]return = [[2,0]]Cell [2,0] has elevation 7 and has non-increasing routes to both targets.
Example 2
heights = [[3,3],[3,3]]targets = [[0,0],[1,1]]return = [[0,0],[0,1],[1,0],[1,1]]Every cell can reach both targets and all share the maximum feasible elevation.
Example 3
heights = [[1,2,1]]targets = [[0,0],[0,2]]return = [[0,1]]The middle cell flows downhill to either endpoint and is the unique highest feasible cell.
Constraints
1 <= rows, columns <= 200.-10^9 <= heights[r][c] <= 10^9.1 <= targets.length <= 20.- Every target is a valid grid coordinate.
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
- Number of IslandsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Explore an Unknown Grid and Find a PathLinkedIn · ONSITE INTERVIEW
- Nearest Charging Station on a GridSpotify · ONSITE INTERVIEW
- Island Count and Maximum AreaOracle · ONSITE INTERVIEW
- Shortest Increasing Path to a TargetUber · ONSITE INTERVIEW
- Mouse and Cheese ReachabilityMeta · ONSITE INTERVIEW
- Number of IslandsTripjack · ONSITE INTERVIEW
- Number of IslandsTekion · ONSITE INTERVIEW
- Grid Pathfinding with Obstacles (DFS)Roblox · PHONE SCREEN · ONSITE INTERVIEW
- Number of IslandsGoldman Sachs · OA · PHONE SCREEN · ONSITE INTERVIEW