DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
Graph

Pacific Atlantic Water Flow

**Problem Statement:** Given the required input arguments, write an efficient algorithm to solve the **Pacific Atlantic Water Flow** problem. Implement the required logic as specified by standard definitions for this classic algorithmic challenge. **Hint / Expected Approach:** Reverse BFS from both ocean borders **Edge Cases to Consider:** - (1) 1×1 grid (both oceans) - (2) All same height - (3) No cell reaches both

Examples

Example 1
Input: [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
Explanation: Classic
Example 2
Input: [[1]]
Output: [[0,0]]
Explanation: 1×1 cell, both oceans
Example 3
Input: [[1,1],[1,1],[1,1]]
Output: All cells reachable
Explanation: All same height

Constraints

  • ▪2 <= vertices <= 1000
  • ▪0 <= edges <= 5000
  • ▪Avoid infinite cycles during depth traversal

Watch Out For Edge Cases

  • ▪1×1 grid (both oceans)
  • ▪All same height
  • ▪No cell reaches both
Frequently Asked At
AmazonGoogleMicrosoftMeta