Uber CodingFree previewFree
Increasing Grid Path Queries
Last asked
Problem
For repeated find(target) queries on an integer grid, return a shortest strictly increasing path from the upper-left cell to the target value, moving in four directions.
Return the path as a list of values, or an empty list when the target is unreachable; efficient repeated queries and memory trade-offs are central requirements.
- Given a two-dimensional integer grid, support repeated
find(target)calls on the same grid. - Each path starts at the upper-left cell. Move up, down, left, or right, and require each next value to be strictly greater than the current value.
- Return a shortest valid path to the target number as a list of cell values, including the start and target. Return
[]if no valid path exists. - Make repeated queries efficient. Dynamic programming is an explicit interview requirement.
- Discuss the trade-off between memory use and query speed, including alternative dynamic-programming representations.
Examples
Example 1
Input
Grid: 1 2 3 4 5 8 7 9 6
find(5)
Output
[1, 2, 5] or [1, 4, 5]
Example 2
Input
Grid: 1 2 3 4 5 8 7 9 6
find(6)
Output
[]
Loading the solution…
