Quick Overview

A grid coding problem: find the length of the longest walk of strictly increasing values in an integer matrix, moving only up, down, left or right. It tests modeling a grid as a graph, handling equal neighboring values correctly, and an efficient search over grids of up to 40,000 cells.

Longest Strictly Rising Four-Direction Walk in an Integer Grid

Company: Visa

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Given a grid of non-negative integers with `m` rows and `n` columns, return the number of cells on the longest walk whose values strictly increase from each cell to the next. A walk may start at any cell. From each cell it moves to one of the four orthogonally adjacent cells: up, down, left or right. Diagonal moves are not allowed, and a walk may not leave the grid or wrap around its edges. ### Function Signature ```python def longest_rising_walk(matrix: list[list[int]]) -> int: ``` ### Rules - Every step must go to a cell whose value is strictly greater than the current cell's value. A step between equal values is not allowed. - The length of a walk is the number of cells it visits. A single cell is a walk of length 1, so the answer is always at least 1. - Because values strictly increase along a walk, no cell can be visited twice. - Return only the maximum length. Several walks may share that length; the returned integer is the same either way. ### Constraints - `m == len(matrix)` and `n == len(matrix[i])` for every row `i` - `1 <= m <= 200` and `1 <= n <= 200` - `0 <= matrix[i][j] <= 2**31 - 1`; every value fits in a 32-bit signed integer - The answer is at most `m * n`, which is at most 40,000. ### Examples **Example 1** ```text Input: matrix = [[3, 1, 2, 5, 4]] Output: 3 ``` The walk 1 → 2 → 5 through columns 1, 2 and 3 visits three cells. Every other walk is shorter; for example, 1 → 3 and 4 → 5 visit two cells each. **Example 2** ```text Input: matrix = [[7, 7], [7, 7]] Output: 1 ``` Every neighbor has an equal value, so no step is allowed and every walk is a single cell. **Example 3** ```text Input: matrix = [ [5, 1, 8], [4, 2, 7], [9, 3, 6] ] Output: 6 ``` Starting at the 1 in row 0, column 1, the walk 1 → 2 → 3 → 6 → 7 → 8 moves down, down, right, up and up, ending at row 0, column 2.

Overview: A grid coding problem: find the length of the longest walk of strictly increasing values in an integer matrix, moving only up, down, left or right. It tests modeling a grid as a graph, handling equal neighboring values correctly, and an efficient search over grids of up to 40,000 cells.

Read the full Visa Software Engineer interview experience this question came from

Given a grid `matrix` of non-negative integers with `m` rows and `n` columns, return the number of cells on the longest walk whose values strictly increase from each cell to the next. A walk may start at any cell. From each cell it moves to one of the four orthogonally adjacent cells: up, down, left or right. Diagonal moves are not allowed, and a walk may not leave the grid or wrap around its edges. Implement `longest_rising_walk(matrix)`, which returns this length as an integer. Rules: - Every step must go to a cell whose value is strictly greater than the current cell's value. A step between equal values is not allowed. - The length of a walk is the number of cells it visits. A single cell is a walk of length 1, so the answer is always at least 1. - Because values strictly increase along a walk, no cell can be visited twice. - Return only the maximum length. Several walks may share that length; the returned integer is the same either way. Constraints: - m == len(matrix) and n == len(matrix[i]) for every row i - 1 <= m <= 200 and 1 <= n <= 200 - 0 <= matrix[i][j] <= 2**31 - 1; every value fits in a 32-bit signed integer - The answer is at most m * n, which is at most 40,000. No value in this problem exceeds 2^31 - 1: grid values fit a 32-bit signed int (int in Java and C++), and the returned length is at most 40,000. Example 1: Input: matrix = [[3, 1, 2, 5, 4]] Output: 3 Explanation: The walk 1 -> 2 -> 5 through columns 1, 2 and 3 visits three cells. Every other walk is shorter; for example, 1 -> 3 and 4 -> 5 visit two cells each. Example 2: Input: matrix = [[5, 1, 8], [4, 2, 7], [9, 3, 6]] Output: 6 Explanation: Starting at the 1 in row 0, column 1, the walk 1 -> 2 -> 3 -> 6 -> 7 -> 8 moves down, down, right, up and up, ending at row 0, column 2.

Constraints

  • m == len(matrix) and n == len(matrix[i]) for every row i
  • 1 <= m <= 200 and 1 <= n <= 200
  • 0 <= matrix[i][j] <= 2**31 - 1; every value fits in a 32-bit signed integer
  • The answer is at most m * n, which is at most 40,000.

Examples

Input: ([[0]],)

Expected Output: 1

Explanation: Minimum 1x1 grid: the single cell is a walk of one cell, so cells are counted (1), not steps (0).

Input: ([[3, 1, 2, 5, 4]],)

Expected Output: 3

Explanation: Source example 1, a single row: 1 -> 2 -> 5 visits three cells; every other walk is shorter.

Hints

  1. A step only ever goes from a smaller value to a strictly larger one, so no walk can revisit a cell or loop back on itself.
  2. Equal neighbors block a step in both directions, diagonal cells are never adjacent, and cells on opposite edges of the grid are not neighbors.
  3. The answer can reach m * n = 40,000 cells, so avoid re-exploring the same cell's continuations over and over, and make sure a very long walk cannot exhaust the call stack.

Loading coding console...

Show the approach

Approach

Treat every allowed step as a directed edge from a cell to an orthogonal neighbor holding a strictly larger value. Values strictly increase along every edge, so this graph has no cycles, and the answer is the number of cells on its longest path. The reference peels the grid in layers. A cell's in-degree is the number of orthogonal neighbors with a strictly smaller value. Layer 1 holds every cell with no smaller neighbor. Processing a layer decrements the in-degree of each strictly larger neighbor of its cells, and a cell joins the next layer when its in-degree reaches zero. Invariant: a cell is placed in layer k exactly when the longest rising walk ending at that cell has k cells. By induction, a cell is placed one layer after the latest-placed of its smaller neighbors, and that neighbor's layer is the maximum walk length ending at any smaller neighbor, so the cell's layer is one more than that maximum, which is exactly the longest walk ending there. Every cell is eventually placed because the graph is acyclic, so the number of layers equals the length of the longest walk. Edge cases: a 1x1 grid or an all-equal grid forms a single layer and returns 1, because cells are counted rather than steps; equal neighbors never create an edge; only the four orthogonal neighbors inside the grid are examined, so there are no diagonal moves and no wrap-around; values up to 2**31 - 1 are only compared, never added, so 32-bit integers suffice; the loop is iterative, so walks of up to 40,000 cells cause no recursion-depth problems.

Time complexity:
O(m * n)
Space complexity:
O(m * n)