Graph / DFS•Pattern: Multi-Source DFS / BFS•Medium

Pacific Atlantic water flow

Problem Intuition & Objective

Find cells from which water can flow to both Pacific and Atlantic oceans.

Time ComplexityO(m×n)
Optimal
Space ComplexityO(m×n)
Memory Bounds
Optimal Algorithmic Approach

Two BFS/DFS passes from each ocean boundary; intersect reachable sets.

Core Invariants & Edge Cases

  • Reverse-flow BFS/DFS from ocean boundaries inward
  • Pacific: top row + left col; Atlantic: bottom row + right col
  • Cells reachable from both = answer
Previous ProblemFlood fill algorithmNext Problem Merge sort — divide and conquer sort

System Design & Architecture Roadmap

Coding interviews are only 50% of the bar. Master High-Level Design, distributed systems, and Object-Oriented LLD.