Hi👋SpeedAlgo • Deliberate Practice & Cognitive Ergonomics for Software Engineers

An interactive algorithm mastery and technical interview preparation platform published by Hi👋WebEnterprise. Built for senior and staff software engineers preparing for rigorous coding screens at top tech companies (FAANG/MAMAA).

13 Core Algorithmic Patterns & 225 Practice Problems

  • 1. Two Pointers (16 Paradigms, 43 Problems): Opposite ends, sorted pair sums, palindrome verification, trapping rain water, 3Sum, container with most water — includes the Sliding Window and Fast & Slow Pointers paradigms (contiguous subarray invariants, longest substrings, Floyd's cycle detection, linked list middle).
  • 2. Binary Search (9 Paradigms, 14 Problems): Monotonic predicate partitioning (feasible(x)), boundary search, rotated sorted arrays, square roots, minimum ship capacity, median of two sorted arrays, matrix median on value range.
  • 3. Bit Manipulation (5 Paradigms, 11 Problems): Counting set bits, XOR cancellation, bit reversal, bitmask pairing, maximum XOR with a bitwise trie.
  • 4. Math & Geometry (8 Paradigms, 14 Problems): Sieve of Eratosthenes, integer/roman conversions, modular arithmetic, geometric simulation.
  • 5. Tree/Graph Depth-First Search (15 Paradigms, 24 Problems): Path sums, lowest common ancestor, tree diameter, validating BSTs, tree DP (House Robber III, Binary Tree Cameras).
  • 6. Tree/Graph Breadth-First Search (5 Paradigms, 12 Problems): Level-order traversals, shortest path in unweighted graphs, zig-zag traversals, rotting oranges, word ladders.
  • 7. Graphs (12 Paradigms, 22 Problems): Topological sort, cycle detection, Dijkstra's algorithm, bipartite graph validation, network delay.
  • 8. Backtracking (6 Paradigms, 14 Problems): Subsets, permutations, combinations, constraint satisfaction, pruning, N-Queens, Sudoku solver.
  • 9. Dynamic Programming (13 Paradigms, 28 Problems): Memoization vs tabulation, 0/1 knapsack, unbounded knapsack, coin change, edit distance, longest common subsequence.
  • 10. Heap / Priority Queue (9 Paradigms, 11 Problems): Running medians, top-k elements, interval scheduling, IPO, k-way merges.
  • 11. Advanced Data Structures (7 Paradigms, 17 Problems): Trie (prefix tree), Union-Find (Disjoint Set Union), LRU Cache, LFU Cache, range-sum queries with updates.
  • 12. Stack (5 Paradigms, 9 Problems): Next greater element, largest rectangle in histogram, remove k digits, decode string, min stack.
  • 13. Intervals & Greedy (2 Paradigms, 6 Problems): Merge intervals, interval scheduling, meeting rooms by sweep line, jump game.

4-Stage Deliberate Practice Framework

  1. Stage 1 (Compare & Learn): Read the pattern's mental model, loop invariant and Big-O proof, compare the pattern's template with the problem's solution side by side, and use the step-by-step debugger. The solution can also be shown in C#, Java, TypeScript, C++, Go or Rust for almost every problem.
  2. Stage 2 (Active Recall): Rebuild the solution from memory in a code editor, with an invariant checklist and optional progressive hints, and check it against a judged test suite.
  3. Stage 3 (AI Interview Coach, coming soon): A mock-interview stage still in development; for now, students go straight from Stage 2 to Stage 4.
  4. Stage 4 (Solve on Your Own): Solve a new problem on your own against a 15-minute timer; your code is graded against a test suite. Python and TypeScript run in your browser and C# runs on our server (design problems grade in Python and TypeScript).

Equipped with a spaced-repetition review hub on a fixed 7-step schedule (1 day up to 90 days), a two-pane Studio workspace, and study notes.

Pricing, Access & Commercial Terms

  • Core Curriculum: 100% Free. No credit card required.
  • Coins: New accounts get 40 free coins once, the daily claim adds +20 coins once a day, and inviting a friend earns you +25 coins when they complete their first practice step. These are the only ways to earn coins.
  • AI Coach: Each AI coach question costs 1-5 coins, priced by the server according to its size.
  • BYOK (Bring Your Own Key): AI coaching is free with your own Google Gemini, OpenAI or OpenRouter API key (BYOK).
  • Extras: Coins also unlock extras for 24 hours: a Recall hint level (1 coin), a Solve solution reveal (2), a LeetCode problem card's solutions (1) and a pattern's extra canonical problems (1).
  • Payments & Refunds: Nothing on the site is sold for money: coins can only be earned, never bought. There are no subscriptions or charges, so there is nothing to refund.
  • Platform Operator: Hi👋WebEnterprise. Support & policies at hispeedalgo.com.
Skip to main content
Hi👋SpeedAlgo

Invariant-First Algorithmic Mastery

Arena
238Items
Theory Context•Graph Algorithms
MediumLC 417

Pacific Atlantic Water Flow (LeetCode 417)

Target Frequency:GoogleAppleNetflix

An m x n rectangular island sits between two oceans: the Pacific along its top and left edges, and the Atlantic along its bottom and right edges. You're given the island's height map, an m x n matrix heights where heights[r][c] is the elevation of cell (r, c).

Rain falling on any cell flows to a north/south/east/west neighbor only if that neighbor's height is no greater than the current cell's, and water reaches an ocean as soon as it flows off the cell bordering it.

Return the list of coordinates [r, c] from which rain can reach both oceans.

Worked Examples

Example 1
Input:heights = [[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]]
1223532344245316714551124
Explanation: Each listed cell has a non-increasing downhill path to both coasts, for example (1,3) steps up to (0,3) for the Pacific and sideways to (1,4) for the Atlantic, and (2,2) reaches the Pacific via (1,2)→(0,2) and the Atlantic via (2,3)→(2,4). Other cells only drain to one ocean, or (like the peak at (1,4) height 4) sit high enough to reach both directly. Several of these cells have more than one valid downhill route; only one needs to exist.
Example 2
Input:heights = [[1]]
Output:[[0,0]]
Explanation: A single cell borders every edge of the island at once, so it already touches both oceans.

⚖️Formal Constraints & Bounds

  • m == heights.length

  • n == heights[r].length

  • 1 <= m, n <= 200

  • 0 <= heights[r][c] <= 105

Deep-Dive & Conceptual Insights

Core Insight

Reversing the flow direction transforms an expensive all-pairs problem into two linear graph traversals.

Real-World Scenario & Production Applications

Topographical watershed modeling, hydrological drainage divide analysis.

Step-by-Step Execution Trace Table

CellHeightPacific Reached?Atlantic Reached?Both?
(0, 4)5Yes (top coast)Yes (right coast)YES
(1, 3)4Yes (via (0,3))Yes (via (1,4))YES
(2, 2)5Yes (via (1,2))Yes (via (2,3))YES
(0, 0)1Yes (Pacific corner)No (Atlantic blocked)No
Trace Inputheights = [[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]]
Expected[[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
CellHeightPacific Reached?Atlantic Reached?Both?
(0, 4)5Yes (top coast)Yes (right coast)YES
(1, 3)4Yes (via (0,3))Yes (via (1,4))YES
(2, 2)5Yes (via (1,2))Yes (via (2,3))YES
(0, 0)1Yes (Pacific corner)No (Atlantic blocked)No
Code Shape
Archetype Code Shape
Python
1pacific = set()
2atlantic = set()
3def dfs(r, c, vis):
4 vis.add((r, c))
5 for each neighbor (nr, nc) inside the grid:
6 if (nr, nc) not in vis and heights[nr][nc] >= heights[r][c]:
7 dfs(nr, nc, vis)
8dfs from all 4 boundaries into respective ocean sets
9return intersection of pacific and atlantic

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Simulating water flow forward from every cell to the ocean takes O((MN)2)O((MN)^2)O((MN)2) time. The inversion trick is to reverse the flow: start from the ocean boundaries and search uphill (neighbor≥currentneighbor \ge currentneighbor≥current). By performing two reverse traversals (one from the Pacific, one from the Atlantic), the target cells are simply the intersection of cells reachable by both ocean tides in O(MN)O(MN)O(MN) time.

🔄 The Analogy: Instead of Asking Where Rain Goes, Ask Where the Tide Reaches

Rather than dropping a raindrop on every cell and simulating where it flows, stand at the ocean's edge and ask a different question: "if I let water climb uphill from here, which inland cells could it reach?" Water naturally flows from high ground to low or equal ground, so reversing that — climbing from an ocean cell to any neighbor at an equal or greater height — retraces every path that could have flowed down into that ocean cell in the forward direction. Do this once from every Pacific-adjacent cell, and once from every Atlantic-adjacent cell; anywhere both reverse-searches reach is a cell whose water can reach both oceans.

🔑 The Key Idea in the Code

This turns an O((M·N)²) "simulate every cell's downhill journey" problem into two O(M·N) reverse floods plus a set intersection — a massive complexity win purely from asking the question backward.

  • Forward simulation from every cell O((M⋅N)2)O((M \cdot N)^2)O((M⋅N)2) TLE: Starting independent BFS/DFS searches from each interior cell to see if water reaches both oceans. The reverse perspective is required: start multi-source traversals from the Pacific border inward, and Atlantic border inward, flowing strictly uphill (neighbor_height >= curr_height).

  • Uphill flow inversion error: When traversing backward from oceans into the continent, water flows from lower heights to equal-or-higher heights. Writing neighbor_height <= curr_height inverts the logic and finds ocean-bound paths instead of uphill sources.

  • Combining visited sets with bitmasks incorrectly: Using a single visited set instead of two independent sets pacific_reachable and atlantic_reachable. The answer is the set intersection pacific_reachable & atlantic_reachable.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Grid drainage / flow reachability to multiple opposing boundaries.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: reverse flow travels uphill (heights[neighbor] >= heights[curr]).

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Simulating flow downhill from every cell: O((R·C)²) vs optimal O(R · C).

  • Shared visited state: Pacific and Atlantic traversals must use separate visited sets.

The 60-Second Interview Pitch

Say this out loud before you type a single line

"I perform reverse DFS from all 4 boundaries into pacific and atlantic sets. In reverse flow, water moves uphill (neighbor_h >= curr_h). After both ocean traversals complete, the answer is the intersection of both sets. Runs in O(R · C) time and O(R · C) space."

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(M · N)

Look at the execution steps: Reverses flow direction: runs DFS/BFS from Pacific ocean boundaries (top/left) and Atlantic ocean boundaries (bottom/right) moving uphill (height >= prev_height). Finds intersection of visited matrices.

SPACE COMPLEXITY

O(M · N) Auxiliary

Look at memory allocations: Call stack uses O(M · N) Recursion stack depth. Two boolean visited matrices (pacific, atlantic) of size M x N.

Formal Recurrence Relation

T(M, N) = 2 · O(M · N) = O(M · N)

Derivation Progression

Boundary Initialization

2M + 2N boundary origins

Seeds searches from ocean shorelines.

Uphill DFS Propagation

if H[nr][nc] ≥ H[r][c]

Water flows downhill, so search climbs uphill from ocean inward.

Matrix Intersection

pacific[r][c] ∧ atlantic[r][c]

Finds mutual reachability in O(M · N) pass.

Variable Definitions

MMM

Total number of rows in the elevation grid

NNN

Total number of columns in the elevation grid

Memory Architecture & Bounds

🟣 Call Stack

O(M · N) Recursion stack depth

🔵 Auxiliary Heap

O(M · N) Two boolean visited matrices

🟢 Output Space

O(M · N) Coordinate list

Boundary Best / Worst Cases

Best Case

O(M⋅N)O(M · N)O(M⋅N)

Average Case

O(M⋅N)O(M · N)O(M⋅N)

Worst Case

O(M⋅N)O(M · N)O(M⋅N)

Graph & Tree Traversal Frontier

Graph & Tree Traversal Frontier
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "water flows to both Pacific and Atlantic", "rain water flows downhill". Simulating downward flow from each interior cell takes O((MN)2)O((MN)^2)O((MN)2) (10810^8108 ops, TLE). A Senior SWE inverts the search: Reverse Boundary Flow climbing UPWARD from the Oceans in strictly O(MN)O(MN)O(MN) time!

CONSTRAINTS & BOUNDS

m,n≤200  ⟹  MN≤4×104m, n \le 200 \implies MN \le 4 \times 10^4m,n≤200⟹MN≤4×104. Time budget: O(MN)O(MN)O(MN) linear.

FAANG PRODUCTION TRAPS & EDGE CASES

Reversed elevation check: when walking from ocean inward, water "climbs" uphill: the neighbor height must be ≥\ge≥ current cell height!

Theory Context•Graph Algorithms
MediumLC 417

Pacific Atlantic Water Flow (LeetCode 417)

Target Frequency:GoogleAppleNetflix

An m x n rectangular island sits between two oceans: the Pacific along its top and left edges, and the Atlantic along its bottom and right edges. You're given the island's height map, an m x n matrix heights where heights[r][c] is the elevation of cell (r, c).

Rain falling on any cell flows to a north/south/east/west neighbor only if that neighbor's height is no greater than the current cell's, and water reaches an ocean as soon as it flows off the cell bordering it.

Return the list of coordinates [r, c] from which rain can reach both oceans.

Worked Examples

Example 1
Input:heights = [[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]]
1223532344245316714551124
Explanation: Each listed cell has a non-increasing downhill path to both coasts, for example (1,3) steps up to (0,3) for the Pacific and sideways to (1,4) for the Atlantic, and (2,2) reaches the Pacific via (1,2)→(0,2) and the Atlantic via (2,3)→(2,4). Other cells only drain to one ocean, or (like the peak at (1,4) height 4) sit high enough to reach both directly. Several of these cells have more than one valid downhill route; only one needs to exist.
Example 2
Input:heights = [[1]]
Output:[[0,0]]
Explanation: A single cell borders every edge of the island at once, so it already touches both oceans.

⚖️Formal Constraints & Bounds

  • m == heights.length

  • n == heights[r].length

  • 1 <= m, n <= 200

  • 0 <= heights[r][c] <= 105

Deep-Dive & Conceptual Insights

Core Insight

Reversing the flow direction transforms an expensive all-pairs problem into two linear graph traversals.

Real-World Scenario & Production Applications

Topographical watershed modeling, hydrological drainage divide analysis.

Step-by-Step Execution Trace Table

CellHeightPacific Reached?Atlantic Reached?Both?
(0, 4)5Yes (top coast)Yes (right coast)YES
(1, 3)4Yes (via (0,3))Yes (via (1,4))YES
(2, 2)5Yes (via (1,2))Yes (via (2,3))YES
(0, 0)1Yes (Pacific corner)No (Atlantic blocked)No
Trace Inputheights = [[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]]
Expected[[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
CellHeightPacific Reached?Atlantic Reached?Both?
(0, 4)5Yes (top coast)Yes (right coast)YES
(1, 3)4Yes (via (0,3))Yes (via (1,4))YES
(2, 2)5Yes (via (1,2))Yes (via (2,3))YES
(0, 0)1Yes (Pacific corner)No (Atlantic blocked)No
Code Shape
Archetype Code Shape
Python
1pacific = set()
2atlantic = set()
3def dfs(r, c, vis):
4 vis.add((r, c))
5 for each neighbor (nr, nc) inside the grid:
6 if (nr, nc) not in vis and heights[nr][nc] >= heights[r][c]:
7 dfs(nr, nc, vis)
8dfs from all 4 boundaries into respective ocean sets
9return intersection of pacific and atlantic

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Simulating water flow forward from every cell to the ocean takes O((MN)2)O((MN)^2)O((MN)2) time. The inversion trick is to reverse the flow: start from the ocean boundaries and search uphill (neighbor≥currentneighbor \ge currentneighbor≥current). By performing two reverse traversals (one from the Pacific, one from the Atlantic), the target cells are simply the intersection of cells reachable by both ocean tides in O(MN)O(MN)O(MN) time.

🔄 The Analogy: Instead of Asking Where Rain Goes, Ask Where the Tide Reaches

Rather than dropping a raindrop on every cell and simulating where it flows, stand at the ocean's edge and ask a different question: "if I let water climb uphill from here, which inland cells could it reach?" Water naturally flows from high ground to low or equal ground, so reversing that — climbing from an ocean cell to any neighbor at an equal or greater height — retraces every path that could have flowed down into that ocean cell in the forward direction. Do this once from every Pacific-adjacent cell, and once from every Atlantic-adjacent cell; anywhere both reverse-searches reach is a cell whose water can reach both oceans.

🔑 The Key Idea in the Code

This turns an O((M·N)²) "simulate every cell's downhill journey" problem into two O(M·N) reverse floods plus a set intersection — a massive complexity win purely from asking the question backward.

  • Forward simulation from every cell O((M⋅N)2)O((M \cdot N)^2)O((M⋅N)2) TLE: Starting independent BFS/DFS searches from each interior cell to see if water reaches both oceans. The reverse perspective is required: start multi-source traversals from the Pacific border inward, and Atlantic border inward, flowing strictly uphill (neighbor_height >= curr_height).

  • Uphill flow inversion error: When traversing backward from oceans into the continent, water flows from lower heights to equal-or-higher heights. Writing neighbor_height <= curr_height inverts the logic and finds ocean-bound paths instead of uphill sources.

  • Combining visited sets with bitmasks incorrectly: Using a single visited set instead of two independent sets pacific_reachable and atlantic_reachable. The answer is the set intersection pacific_reachable & atlantic_reachable.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Grid drainage / flow reachability to multiple opposing boundaries.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: reverse flow travels uphill (heights[neighbor] >= heights[curr]).

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Simulating flow downhill from every cell: O((R·C)²) vs optimal O(R · C).

  • Shared visited state: Pacific and Atlantic traversals must use separate visited sets.

The 60-Second Interview Pitch

Say this out loud before you type a single line

"I perform reverse DFS from all 4 boundaries into pacific and atlantic sets. In reverse flow, water moves uphill (neighbor_h >= curr_h). After both ocean traversals complete, the answer is the intersection of both sets. Runs in O(R · C) time and O(R · C) space."

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(M · N)

Look at the execution steps: Reverses flow direction: runs DFS/BFS from Pacific ocean boundaries (top/left) and Atlantic ocean boundaries (bottom/right) moving uphill (height >= prev_height). Finds intersection of visited matrices.

SPACE COMPLEXITY

O(M · N) Auxiliary

Look at memory allocations: Call stack uses O(M · N) Recursion stack depth. Two boolean visited matrices (pacific, atlantic) of size M x N.

Formal Recurrence Relation

T(M, N) = 2 · O(M · N) = O(M · N)

Derivation Progression

Boundary Initialization

2M + 2N boundary origins

Seeds searches from ocean shorelines.

Uphill DFS Propagation

if H[nr][nc] ≥ H[r][c]

Water flows downhill, so search climbs uphill from ocean inward.

Matrix Intersection

pacific[r][c] ∧ atlantic[r][c]

Finds mutual reachability in O(M · N) pass.

Variable Definitions

MMM

Total number of rows in the elevation grid

NNN

Total number of columns in the elevation grid

Memory Architecture & Bounds

🟣 Call Stack

O(M · N) Recursion stack depth

🔵 Auxiliary Heap

O(M · N) Two boolean visited matrices

🟢 Output Space

O(M · N) Coordinate list

Boundary Best / Worst Cases

Best Case

O(M⋅N)O(M · N)O(M⋅N)

Average Case

O(M⋅N)O(M · N)O(M⋅N)

Worst Case

O(M⋅N)O(M · N)O(M⋅N)

Graph & Tree Traversal Frontier

Graph & Tree Traversal Frontier
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "water flows to both Pacific and Atlantic", "rain water flows downhill". Simulating downward flow from each interior cell takes O((MN)2)O((MN)^2)O((MN)2) (10810^8108 ops, TLE). A Senior SWE inverts the search: Reverse Boundary Flow climbing UPWARD from the Oceans in strictly O(MN)O(MN)O(MN) time!

CONSTRAINTS & BOUNDS

m,n≤200  ⟹  MN≤4×104m, n \le 200 \implies MN \le 4 \times 10^4m,n≤200⟹MN≤4×104. Time budget: O(MN)O(MN)O(MN) linear.

FAANG PRODUCTION TRAPS & EDGE CASES

Reversed elevation check: when walking from ocean inward, water "climbs" uphill: the neighbor height must be ≥\ge≥ current cell height!

© 2026 Hi👋WebEnterprise. All rights reserved.
Sitemap•llms.txt•