DSA Tracker

Problem

Medium

Pacific Atlantic Water Flow

A medium Graph interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Graph
Sheets
2
Core for
12 roles

The problem

Picture a grid of land heights sitting between two oceans: the Pacific touches the top and left edges, the Atlantic the bottom and right edges. Rain can run from a cell into a neighbouring cell (up, down, left, right) whose height is the same or lower. List every cell from which rain can reach both oceans.

Example 1

Input
matrix = [[2,1,3,2],[4,3,2,1],[3,4,1,2],[1,2,4,3]]
Output
[[0,2],[0,3],[1,0],[1,1],[2,0],[2,1],[3,0],[3,1],[3,2]]
Why
These cells can route water to both oceans via non-increasing paths.

Example 2

Input
matrix = [[2,1],[1,2]]
Output
[[0,0],[0,1],[1,0],[1,1]]
Why
All four cells can reach both oceans in this small grid.

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 200
  • 0 <= matrix[i][j] <= 10^5

How to think about it

Updated 2026-09-29

Instead of simulating water trickling down from all m x n cells independently, reverse the physical flow: imagine ocean water flooding uphill. Run one flood fill from the Pacific borders and another from the Atlantic borders, moving only to equal or taller neighbors. The cells reached by both floods are precisely the answer.

Approaches, worst first

  1. Individual cell flood simulations

    time O((m * n)^2) · space O(m * n)

    From every coordinate (r, c), run a search downwards to see if water can reach both borders. Suffers from immense redundant re-traversal of shared downward paths.

  2. Dual reverse ocean flood fillsWrite this one

    time O(m * n) · space O(m * n)

    Maintain two boolean matrices, pacific and atlantic. Flood inwards from the top/left edges into pacific, and from bottom/right edges into atlantic, stepping only onto cells with height >= current. Intersect the two matrices to collect all valid coordinates.

Where people lose marks · 3
  • Water can flow across flat terrain; the search condition must allow neighbor height >= current height, not strictly greater.
  • A 1x1 matrix reaches both oceans simultaneously because its single cell borders all four edges.
  • Using a single visited matrix for both ocean searches causes the second flood fill to terminate early upon encountering cells visited by the first.

The theory behind it

Graph — the ground this problem stands on. All Graph problems

What Graph is

A graph is a network of individual points, called vertices or nodes, connected by lines called edges. Think of a subway transit map, an electrical circuit, or a web of social friends. Unlike a tree, a graph has no designated top node and no parent-child hierarchy. Connections can run one-way or both ways, and paths can loop back on themselves to form closed cycles.

When to reach for it

Reach for graph algorithms when inputs describe relationships, networks, flights between cities, course prerequisites, or clone networks. Signals include finding the shortest route across unweighted connections, ordering tasks that depend on earlier tasks, counting isolated clusters, or checking whether a path contains an infinite loop. Whenever problems present pairs of related entities and ask for reachability, distances, or dependencies, graph representations apply.

How the pattern works

First convert edge lists into an adjacency list, mapping each node to an array of its neighbors. Choose your exploration strategy based on the goal: use a queue and breadth-first search to find the shortest path in unweighted networks, or use recursion and depth-first search to explore full paths and detect cycles. Because graphs can have loops, always track visited nodes in a set or boolean array. Add nodes to the visited set at the moment they enter the queue so they are never visited twice.

More Graph problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .