What is the Pacific Atlantic Water Flow problem and how do you solve it?
This problem asks to find cells from which water can flow to both Pacific and Atlantic oceans. It tests multi-source BFS/DFS.
Why Interviewers Ask This
Interviewers use this to test reverse graph traversal. They want to see if you can start from the ocean and flow uphill. It demonstrates bidirectional thinking.
How to Answer This Question
Explain running BFS/DFS from the Pacific coast (top and left) and Atlantic coast (bottom and right). Mark reachable cells for each ocean. Find the intersection of reachable cells. Discuss why reverse flow is easier than forward flow.
Key Points to Cover
- Reverse flow logic
- Multi-source BFS
- Intersection of sets
- Coastal initialization
Sample Answer
Instead of checking every cell to see if it can reach both oceans, I run BFS from the oceans themselves. I start from all cells adjacent to the Pacific (top row and left column) and mark all reachable cells where water cā¦
Common Mistakes to Avoid
- Simulating flow from every cell
- Incorrect boundary checks
- Not marking visited cells properly
Sound confident on this question in 5 minutes
Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free ā no signup needed.