FastPrepLongest Increasing Path in a Matrix
Problems/Longest Increasing Path in a Matrix

Longest Increasing Path in a Matrix

DoorDash logoDoorDash● HardNEW GRADONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names the standard increasing-matrix-path exercise and asks for a second algorithm. The judged core matches at about 95%; input limits are specified for practice.

Return the number of cells in the longest strictly increasing path in matrix. A path may start at any cell. Each move goes one cell up, down, left, or right, and the next value must be strictly larger. Diagonal moves and wrapping across an edge are not allowed.

For this exercise, the matrix is nonempty and rectangular. Equal values cannot be consecutive on the path.

Function

longestIncreasingPath(matrix: int[][]) → int

Examples

Example 1

matrix = [[9,9,4],[6,6,8],[2,1,1]]return = 4

One longest path visits (2,1), (2,0), (1,0), and (0,0), with values 1, 2, 6, and 9. No increasing path contains more than four cells.

Example 2

matrix = [[3,4,5],[3,2,6],[2,2,1]]return = 4

The path (1,1) to (0,1) to (0,2) to (1,2) has values 2, 4, 5, and 6. Its four cells form a longest path.

Example 3

matrix = [[1]]return = 1

The only cell is a path of length 1.

Constraints

  • 1 <= matrix.length <= 100
  • 1 <= matrix[i].length <= 100
  • 0 <= matrix[i][j] <= 10^9
  • All rows have the same length.

More DoorDash problems

  • Customer Bootstrap APIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Dasher Pay with Store Waits and Peak HoursPHONE SCREEN · Seen Nov 2026
  • Random Dasher Set OperationsONSITE INTERVIEW · Seen Nov 2026
All DoorDash interview questionsSee DoorDash hiring insights
Saved
public int longestIncreasingPath(int[][] matrix) {
    // Write your code here.
}
Checking account…