The Number of Increasing Paths in a Grid problem gives you a matrix of integers. Count the number of strictly increasing paths (of any length ≥ 1) — paths where each step moves to an adjacent cell (4 directions) with a strictly larger value. Return the count modulo 10^9+7. This hard coding problem uses DFS with memoization on a DAG formed by the grid.
Microsoft and Adobe ask this because it combines grid DFS with memoization on a DAG — the grid's cells form a directed acyclic graph where edges go from smaller to larger values. The array, matrix, BFS, graph, memoization, DFS, topological sort, and dynamic programming interview pattern is comprehensively exercised.
Memoized DFS (top-down DP). dp[r][c] = number of increasing paths starting from cell (r,c). For each cell, the paths starting here = 1 (the cell itself) + sum of dp[nr][nc] for all 4 neighbors (nr,nc) where grid[nr][nc] > grid[r][c]. Use memoization to avoid recomputation. Process in increasing order via topological sort or let memoized DFS handle it naturally.
Grid:
1 2
3 4
Counting paths on a DAG (directed acyclic graph) always uses DP in topological order or memoized DFS. When the grid values define a natural topological order (smaller values come before larger), memoized DFS from each cell handles it automatically — you never revisit a cell with a larger value before computing its smaller-value predecessors. Practice "number of paths in a DAG" problems — they form a family with "longest increasing path," "number of increasing paths," and "path counting in directed graphs."
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Longest Increasing Path in a Matrix | Hard | Solve | |
| Cat and Mouse II | Hard | Solve | |
| Disconnect Path in a Binary Matrix by at Most One Flip | Medium | Solve | |
| Sliding Puzzle | Hard | Solve | |
| Alien Dictionary | Hard | Solve |