PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates mastery of graph traversal, dynamic programming, and complexity analysis for computing longest increasing paths in a matrix, focusing on algorithmic correctness and efficient use of time and space.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Compute longest rising path in a grid

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given an m×n grid of integers, find the length of the longest path where each step moves up, down, left, or right to a strictly larger value. Return the maximum length over all starting cells. Design an O(mn) solution using memoized DFS or an equivalent iterative/topological approach, and analyze time and space usage. As a follow-up, discuss handling very large grids, recursion-depth limits, and variants such as allowing non-decreasing steps or diagonal moves.

Quick Answer: This question evaluates mastery of graph traversal, dynamic programming, and complexity analysis for computing longest increasing paths in a matrix, focusing on algorithmic correctness and efficient use of time and space.

Given an m×n grid of integers, find the length of the longest path where each step moves up, down, left, or right to a strictly larger value. Return the maximum length over all starting cells. Diagonal moves and wrap-around are not allowed, and you may not revisit a cell within a path. Design an O(mn) solution using memoized DFS (or an equivalent topological/iterative approach) and analyze its time and space usage. Example: matrix = [[9,9,4],[6,6,8],[2,1,1]] Answer = 4, via the path 1 → 2 → 6 → 9. The path length is the number of cells visited (a single cell counts as length 1). For an empty grid, return 0.

Constraints

  • 0 <= m, n <= 200
  • Cell values fit in a 32-bit signed integer (negatives allowed).
  • Moves are only up/down/left/right to a STRICTLY larger value.
  • A single cell is a valid path of length 1; an empty grid returns 0.

Examples

Input: ([[9,9,4],[6,6,8],[2,1,1]],)

Expected Output: 4

Explanation: Longest increasing path is 1 → 2 → 6 → 9 (length 4).

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

Expected Output: 4

Explanation: Longest increasing path is 3 → 4 → 5 → 6 (length 4).

Input: ([[1]],)

Expected Output: 1

Explanation: A single cell is a path of length 1.

Input: ([],)

Expected Output: 0

Explanation: An empty grid has no cells, so the answer is 0.

Input: ([[7,7,7],[7,7,7]],)

Expected Output: 1

Explanation: All values equal — no strictly-larger neighbor exists, so every path has length 1.

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

Expected Output: 5

Explanation: A strictly increasing row of 5 cells gives a path of length 5.

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

Expected Output: 5

Explanation: Reading the column upward, 1 → 2 → 3 → 4 → 5 forms a path of length 5.

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

Expected Output: 3

Explanation: 1 → 2 → 4 (or 1 → 3 → 4) gives the longest path of length 3.

Hints

  1. The 'strictly larger' rule means the path can never form a cycle, so the grid is implicitly a DAG — you can safely memoize the longest path starting at each cell without a visited set.
  2. Define f(r,c) = length of the longest increasing path starting at (r,c) = 1 + max(f(neighbor)) over neighbors with a strictly greater value. Cache f to make it O(mn).
  3. For an iterative O(mn) version, sort cells by value (or process them in topological order using out-degrees toward larger neighbors) so each cell is finalized after the larger cells it depends on.
  4. Follow-ups: for huge grids convert the recursion to the iterative/topological version to dodge recursion-depth limits; for non-decreasing steps the relation can create cycles, so you must group equal-value cells (e.g. union-find / level sets); diagonal moves just add four more direction offsets.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)